Git Cheat Sheet
Git Cheat Sheet
Git is a free and open-source distributed version control system, which is designed to handle everything
from small to very large projects with speed and efficiency.
There is a large project and 100 developers are working on the project.
• Developers used to submit their codes to the central server without having a copy of their own.
• Any changes made to the source code were not known to the other developers.
• There was no communication between any of the developers.
1
Now, let's analyse the scenario after using Git.
• Every developer has an entire copy of the code on their local system.
• Any change made to the source code can be tracked by others.
• There is regular communication between the developers.
Therefore, for large projects that involve thousands of developers, Git helps those developers to work
collaboratively and efficiently in a structured manner. Learn More.
2. Features of Git
• Free and open-source
• Tracks history
• Supports non-linear development
• Creates backup
• Scalable
• Supports collaboration
• Branching is easier
• Distributed development.
2
3. Git WorkFlow
• Working directory - This is the area where you modify your existing files.
• Staging area (Index) - In this, the files in your working directory are staged and snapshots are added.
• Git directory or repository - It is basically where you perform all the changes that need to be made
i.e. perform commits to branch, checkout branch, make changes etc.
5. Git Clone
The following image shows an illustration of the git clone command. Using the command, a copy of the
original repository is created.
• Open Github and navigate to the target repository which needs to be cloned.
• Under the repo name, click on the tab Clone or Download.
• An option named Clone with HTTPS appears.
• Copy the Clone URL.
• Open a command line and use the command: git clone <repo_URL>
A very useful feature of the git clone is that it allows cloning a specific branch of the target repository
without having to clone the entire repository.
To clone a specific branch, you need to use the command -b to specify the branch. The following
command is used:
4
6. Git Branch
• A branch in Git is used to keep your changes until they are ready.
• You can do your work on a branch while the main branch(main) remains stable. After you are done with
your work, you can merge it to the main branch.
For example -
This command creates a new branch named demo from the Main branch:
Using the git checkout command, we can switch from one branch to another.
Command :
Git doesn’t allow creating a new and isolated branch on a remote repository. But, you to make a branch
remote, we can push an existing local branch.
5
• Create a local branch and switch to that branch:
git checkout -b <branch_name>
• Push in the local branch:
git push -u origin <branch_name>
Note: origin is the default name of remote
Now, if someone wants to fetch some information, one can simply run:
git fetch
git checkout <branch_name>
9. Delete Branches
Once the work is done on a branch and merged with the Main branch, one can delete the branch.
The Git checkout is used to command Git on which branch changes have to be made. Checkout is simply
used to change branches in repositories. It can also be used to restore files.
The following image describes the scenario of creating different branches and switching to a brach when
needed, i.e. we can switch from the main brach to a different branch and vice versa.
6
• Git Checkout Branch
While working on a large codebase, it because easy to have some reference point. That is where
the checkout tag is used.
The following command is used to specify the tagname as well as the branch that will be checked out.
git status is mainly used to display the state of the staging area and the repository. It helps us to track all
the changes made, point out untracked files.
Command:
git status
Git commit is used to record all the changes in the repository. The git commit will commit all the changes
and make a commit-id for the same for tracking down the changes made as shown in the image below.
As shown in the image, the command git commit creates a commit-id to track down changes and commits
all the changes to the git repository.
7
Command:
git commit
• git commit -m
The -m along with the command lets us write the commit message on the command line.
Command:
The -am along with the command is to write the commit message on the command line for already staged
files.
Command:
The amend is used to edit the last commit. In case we need to change the last committed message, this
command can be used.
Command:
rm stands for remove. It is used to remove a collection of files. The git rm command is used to remove or
delete files from the working tree and index.
Command:
git rm <file_name>
Now, if you use the command git status, it would show, that the file has been deleted.
8
Git merge is a command that allows you to merge branches from Git. It preserves the complete history and
chronological order and maintains the context of the branch.
The following image demonstrates how we can create different features by branching from the main
branch and how we can merge the newly created features after the final review to the main branch.
Command :
Git Fetch only downloads the latest changes into the local repository. It downloads fresh changes that
other developers have pushed to the remote repository since the last fetch and allows you to review and
merge manually at a later time using Git Merge. As it doesn’t change the working directory or the staging
area, it is safe to use.
9
The below illustration shows the working of the command git fetch. It fetches all the latest changes that
have been made in the remote repository and lets us make changes accordingly.
You can pull in any changes that have been made from your forked remote repository to the local
repository.
As shown in the below image, using the git pull command, all the changes and content can be fetched from
the remote repository and can be immediately updated in the local repository to match the content.
10
We can simply pull a remote repository by using the git pull command. The syntax is as follows:
git pull
Use the following command to check if there has been any change:
If there is no change, it will show “Already up to date”. Else, it will simply merge those changes in the local
repository.
Sometimes in large codebases, there might be some cases when we do not want to commit our code, but
at the same time don’t want to lose the unfinished code. This is where git stash comes into play. The git
stash command is used to record the current state of the working directory and index in a stash.
It stores the unfinished code in a stash and cleans the current branch from any uncommitted changes.
Now, we can work on a clean working directory.
If in the future, we again need to visit that code, we can simply use the stash and apply those changes back
to the working repository.
As shown below, using the command git stash, we can temporarily stash the changes we have made on the
working copy and can work on something else. Later, when needed, we can git stash pop and again start
working on it.
git stash
11
Suppose, you are working on a website and the code is stored in a repository.
Now let's say, you have some files named design.css and design.js. Now you want to stash these files so that
you can again use them later, while you work on something else.
Therefore, later you can use the git stash list command to view all the changes.
Drop Stash
In case, you no longer require a stash, you can delete it with the following command:
18. Git-Ignore
At times, there are some files that we might want Git to ignore while commiting. For example, private files
or folders containing passwords, APIs etc. These files are user-specific and hence, we can ignore these
using the .gitignore.
.gitignore is generated automatically inside the project directory and ignores the files to get committed to
the repositories.
Follow the below steps to use add the files you want Git to ignore.
Now, if you check the status of your repo, you will see, all the files which were written in the .gitignore file
have been ignored.
Git rebase is used to rewrite commits from one branch to another branch. In order to combine unpublished
local changes with the published remote changes, git pull is performed.
With git pull --rebase, the unpublished changes will be again applied on the published changes and no new
commit will be added to history.
12
• git merge --squash
The squash along with git merge produces the working tree. It indexes in the same way as that of the real
merge but discards the merge history.
Command:
1. When you have merged main into your branch and resolved conflicts.
2. When you need to overwrite the original commits.
• git reflog
The reflog records every change that is made in the repository. Apart from this, if some branch is lost from
the repo, the recovery can be done using this command.
Command:
git reflog
• git revert
Revert simply means to undo the changes. Therefore, it is an undo command in Git. Unlike traditional undo
operation, the revert command does not delete any data. git revert is a commit operation, as it undo the
specified commit.
Command:
git revert
Options:
• Revert commit:
Command:
In case, we want to edit the commit message before reverting, -e is used for the same.
Command:
13
Git bisect is a git tool used for debugging. Suppose, you have a large codebase and some commit causes a
bug, but you are not sure of which commit causes it.
Git bisect goes through all the previous commits and uses binary search to find the bugged commit.
The git bisect command is used to find the bisect position as shown. It bisects (divides) your history
between the good and the bad commit range. It then moves through every commit id between this range
and at each snapshot it allows you to test the code.
It is applied as follows:
It will return the commit which causes the bug and one can debug the issue efficiently.
• git blame
git blame is used to know who/which commit is responsible for the latest changes in the repository. The
author/commit of each line is visible through this.
Command:
This command shows the commits which are responsible for changes of all lines of code.
• git cherry-pick
Choosing a commit from one branch and applying it to another is known as cherry picking in Git. Following
are the steps to cherry pick a commit:
• Visit the branch you want to apply to commit and use the following command:
git switch master
• Run the following command:
git cherry-pick <commit_id
14
Git Submodules
Submodules are a tool that allows attaching an external repository inside another repository at a specific
path. It allows us to keep a git repository as a subdirectory of another git repository.
Commands:
• Add git submodule: This takes the git URL as the parameter and clones the pointer repo as a
submodule. The syntax to add git submodule is:
git submodule add <URL_link>
• git submodule init
is to copy the mapping from .gitmodules file into ./.git/config file. git submodule init has
git submodule init
extend behavior in which it accepts a list of explicit module names.
This enables a workflow of activating only specific submodules that are needed for work on the repository.
Command:
Git Subtrees
Commands:
• add: Let’s assume that you have a local repository that you would like
to add an external vendor library to. In this case we will add the git-subtree repository as a
subdirectory of your already existing git-extensions repository in ~/git-extensions/:
git subtree add --prefix=git-subtree --squash \<Git_repo_link>
• pull : It is similar to pull from the repository with added prefix.
Command:
git subtree pull --prefix <URL_link>
15
Git Submodules Git Subtrees
Git submodule is a better fit for component-based Git subtree is more like a system-based
development, where your main project depends on a development, where your all repo contains
fixed version of another component (repo). everything at once, and you can modify any part.
Suitable for smaller repository size Suitable for bigger repository size
The following table shows the most commonly used Git Commands:
16
S. No Command Name Use
Interactively rebase current branch onto <base>. Launches editor to
enter commands for how each commit will be transferred to the new
25 git rebase -i <base> base.
git restore --staged
26 <file_name> Resetting a staged file
27 git rm -r [File_name] Remove a file (or folder)
28 git config --list List all variables set in config file, along with their values
29 git branch -d <local_branch> Delete local branch in Git
git push -d <remote_name>
30 <branch_name> Delete remote branch in Git
31 git stash pop Unstash the changes
The -am along with the command is to write the commit message on
32 git commit -am the command line for already staged files.
The amend is used to edit the last commit. Incase we need to change
33 git commit -ammend the last committed message, this command can be used.
The git rm command is used to remove or delete files from working
34 git rm tree and index.
Git rebase is used to rewrite commits from one branch to another
35 git pull --rebase branch.
The squash along with git merge produces the working tree. It indexes
in the same way as that of the real merge, but discards the merge
36 git merge --squash history.
37 git revert -e <commit_id> edit the commit mesage before reverting, -e is used for the same.
Git bisect goes through all the previous commit and uses binary search
38 git bisect to find the bugged commit.
git blame is used to know who/which commit is responsible for the
39 git blame lastest changes in the repository.
Choosing a commit from one branch and applying it to another is
40 git cherry-pick known as cherry picking in Git.
Conclusion
So, we learnt how Git makes managing large software codebases easier, how you can commit changes,
clone the repository, how branches work and many git commands that reduce the burden from the lives of
developers.
To test your understanding of Git, some MCQ are provided. Pick the correct option.
17
18