Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
2 views

git-cheatsheet

Git is a version control system that tracks changes to files, allowing users to revert to previous versions, collaborate with others, and experiment safely through branching. Key concepts include repositories, working directories, staging areas, commits, and remote repositories, along with common commands for managing changes and collaboration. The document also emphasizes the importance of frequent commits, clear commit messages, and using branches for organized development.

Uploaded by

himanimahurtiens
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

git-cheatsheet

Git is a version control system that tracks changes to files, allowing users to revert to previous versions, collaborate with others, and experiment safely through branching. Key concepts include repositories, working directories, staging areas, commits, and remote repositories, along with common commands for managing changes and collaboration. The document also emphasizes the importance of frequent commits, clear commit messages, and using branches for organized development.

Uploaded by

himanimahurtiens
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Git

Git Explained Simply


What is Git?

Git is a version control system. It's like a history book for your project. It keeps
track of every change you make to your files over time. This lets you:

Go back to older versions: If you mess something up, you can easily undo
your changes and go back to a working version.

Collaborate with others: Multiple people can work on the same project
without overwriting each other's work.

Track changes: See exactly what was changed, when, and by whom.

Experiment safely: Create branches to try out new features without breaking
the main project.

Key Git Concepts:

Repository (Repo): This is like the main folder for your project that Git is
tracking. It contains all your project files and the history of changes. Think of it
as your project's "history book."

Working Directory: This is your local folder where you actually edit your
project files. It's where you make changes.

Staging Area (Index): This is a temporary area where you prepare changes
before committing them. You "stage" the files you want to include in your next
snapshot. Think of it like a "waiting room" for your changes.

Commit: A commit is a snapshot of your project at a specific point in time. It's


like saving a version of your essay. Each commit has a message describing
the changes you made. Commits are the entries in your project's "history
book."

Branch: A branch is like a separate timeline of development. You can branch


off from the main project to work on new features or fix bugs without affecting

Git 1
the main codebase. Think of it as creating a "new chapter" in your project's
book. The main branch is often called main or master .

Remote Repository: This is a repository hosted on a server (like GitHub,


GitLab, or Bitbucket). It's where you can share your code with others and
collaborate. It's like publishing your project's "history book" online for others
to see and contribute to.

Common Git Commands - Cheat Sheet


Here's a cheat sheet table with common Git commands. Keep this handy as you
learn!

Category Command Description Example

git config --global Sets your name


Basic Setup & git config --global user.name "John
user.name "Your for commits
Info Doe"
Name" (globally, once)

git config --global Sets your email


git config --global user.email
user.email for commits
"john@email.com"
"email@example.com" (globally, once)

Initializes a new
Git repository in
git init git init
the current
directory.

Downloads a
git clone repository from a git clone
<repository_url> remote URL to <https://github.com/user/repo.git >
your computer.

Shows the status


of your working
git status git status
directory and
staging area.

Stages changes
Working with git add <filename> or in a specific file
git add index.html or git add .
Changes git add . or all files for the
next commit.
git commit -m "Your Commits staged git commit -m "Fixing a bug in login
commit message" form"
changes with a

Git 2
descriptive
message.

Shows the
difference
between your
git diff git diff
working
directory and the
staging area.

Shows the
difference
git diff --staged between the git diff --staged
staging area and
the last commit.

Unstages a file,
git restore --staged
removing it from git restore --staged index.html
<filename>
the staging area.

Discards
changes in your
git restore <filename> working git restore index.html
directory for a
specific file.

Viewing Shows the


git log git log
History commit history.

Shows a
condensed one-
git log --oneline git log --oneline
line commit
history.

Shows details of
git show
a specific git show a1b2c3d4
<commit_hash>
commit.

Lists all branches


in your
Branching git branch repository git branch
(current branch
highlighted).

git branch Creates a new


git branch feature-login
<branch_name> branch.

Git 3
git checkout Switches to a
git checkout feature-login
<branch_name> different branch.

Creates a new
git checkout -b branch and
git checkout -b develop
<new_branch_name> switches to it
immediately.

Merges changes
from the
git merge
specified branch git merge develop
<branch_name>
into the current
branch.

Deletes a branch
git branch -d
(after it's git branch -d feature-login
<branch_name>
merged).

Adds a remote
Remote git remote add origin git remote add origin
repository
Repositories <repository_url> <https://github.com/user/repo.git >
named "origin".

Shows
configured
git remote -v git remote -v
remote
repositories.

Uploads local
commits to the
git push origin remote
git push origin main
<branch_name> repository (to
"origin" remote,
branch).

Downloads
changes from
git pull origin the remote
git pull origin main
<branch_name> repository to
your local
branch.

Downloads
objects and refs
git fetch origin git fetch origin
from another
repository

Git 4
Explanation of some key commands:

git init : Use this command in your project folder to start using Git. It creates a
hidden .git folder that stores all the version history.

: Use this to download a project that's already on a remote repository


git clone

(like GitHub).

git add : Tells Git to track changes you've made to files and prepare them for
the next commit. You need to "add" files to the staging area before you can
commit them.

: Saves a snapshot of your staged changes. Always write a good


git commit

commit message to explain what you changed!

git status : A very useful command to see what's going on with your repository.
It tells you about changes you've made, files you've staged, and your current
branch.

git branch and git checkout : Essential for working with branches. Branching allows
you to work on features or fixes in isolation.

: Sends your local commits to a remote repository, making your


git push

changes available to others or as a backup.

git pull : Gets the latest changes from the remote repository and merges them
into your local branch. Use this to stay up-to-date with the work of others.

Important Notes:

Commit Frequently: Commit your changes often, whenever you reach a


logical stopping point or complete a small task. Smaller, more frequent
commits are easier to understand and manage.

Write Good Commit Messages: Your commit messages should be clear and
concise, explaining why you made the changes, not just what you changed.
This helps you and others understand the history of the project later.

Use Branches for Features and Fixes: Don't work directly on the main branch
for new features or bug fixes. Create branches to keep your work organized
and prevent accidentally breaking the main codebase.

Interview Questions (Related to Git):

Git 5
1. What is Git and why is it important in software development? (Hint: Version
control, collaboration, tracking changes)

2. Explain the difference between git add , git commit , and git push . (Hint: Staging
area, local commit, remote repository)

3. What is a branch in Git and why are branches useful? (Hint: Parallel
development, feature isolation, bug fixes)

4. How do you merge changes from one branch to another in Git? (Hint: git

merge )

5. What is a remote repository and why do we use them? (Hint: Collaboration,


backup, sharing code)

6. Describe a typical Git workflow for developing a new feature. (Hint:


Branching, coding, committing, merging, pushing)

7. What is a merge conflict and how do you resolve it? (Hint: Conflicting
changes, manual resolution)

This is a starting point for Git. There's much more to learn, but these basics and
commands will get you going! Practice using these commands in a test repository
to get comfortable with Git.

Git 6

You might also like