Frequently Used Git Commands

Configuring Git

git config is used to create and modify system/user/repo git configuration file.

Options:

  • --system: will set the config system wide. for all user’s all repos in the system. System wide git config file is stored generally in /etc/gitconfig.

  • --global: will set the config user wide. For a specific user’s all git repos. stored in ~/.config/git/config or user’s home directory.

  • --local: will set the config for a specific repo. Stored in .git/config.

  • --list: show all the list of current config variables and their values.

Examples:

1
2
3
4
5
git config --global user.name 'singsong'
git config --global user.email '[email protected]'
git config --global core.editor vim
git config user.name # will show value of user.name
git config user.email # will show value of user.email

Initializing a project

git init is used to initialize a project as git repository.

1
git init

Cloning a repository from remote

git clone is used for cloning a remote repository into local system.

1
git clone https://github.com/aagontuk/cheatsheets mycheatsheets

Showing status of the project

git status is used to see status of the project files. Untracked/Tracked/Modified/Staged.

1
2
git status	# Show details status
git status -s # Show brief status

Adding files to staging area

git add is used to add untracked/modified files to staging area and prepare for commit. If a directory is added, its contents will be added recursively.

  • Adding whole file:
1
git add README.md
  • Adding portion of a file:
1
git add -p README.md    # This will bring an interactive prompt

Inspect changes of a file

git diff is used to show changes of a file

1
2
3
git diff		# Shows changes between last staged and unstaged
git diff --staged # Shows changes between the last commit and staged
git diff COMMIT_HASH~ COMMIT_HASH # Show diff between last commit and its ancestor

Creating and applying patch file

For creating patch file use git diff:

1
git diff > mypatch.patch

If you want add untracked files too first stage them using git add, then create the patch file:

1
git diff --cached > mypatch.patch

Now you can remove those untracked files from staging area (See 1.13) if you don’t want to commit them.

If you want to add binary files in the patch:

1
git diff --binary > mypatch.patch

To apply a patch:

1
git apply mypatch.patch

Commiting

git commit is used for commiting changes that are staged

1
2
3
4
git commit			    # To commit changes that are staged.
git commit -m "COMMIT_MSG" # Commit with inline commit message.
git commit -a # stage all tracked modified files and commit.
git commit -v # will add diff of changes in the commit message.

Removing files

git rm is used to remove files.

Following command will remove a file from tracking and also remove from system(HDD/SSD).

1
git rm project_file

If the file is already modified or staged, then to remove -f have to be used.

1
git rm -f project_file

To remove only from git but not from the system(HDD/SSD). So that git no longer track the file.

1
git rm --cached project_file

Moving/Renaming files

git mv is used to rename files/folders

1
git mv project_file my_project_file

View logs

git log is used to view commit logs/history

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
git log					# Show entire commit log of the repo
git log -n # Show last n commit logs
git log -p -n # Show last n commit log including change/patch/diff in those commits
git log --stat # Commit log witch list of modified files and stat of the changes in those files
git log --pretty=oneline # show each commit log in one line with only full SHA and subject
git log --pretty=short # Log with SHA, author and subject
git log --pretty=full # Log with SHA, author, commiter, subject, message body
git log --pretty=fuller # Log with SHA, author, commiter, subject, message body, write and commit dates
git log --oneline # Show first 7 characters of SHA value, Heading
git log --format="%h - %an, %ad: %s" # Custom log output format
git log --graph # Will show a ascii graph of commit history
git log --relative-date # Will show relative date(days, weeks etc) rather than exact date
git log --since=2.weeks # Show all commits since a specific date or relative date
git log --until=2.weeks # Show all commit before a specific date or relative date
git log --author=AUTHOR # Show the logs of a specific author
git log --follow -- FILE # Show all the commits to a specific file
git log testing # Show the logs of testing branch
git log --all # Show logs of all branches. By default shows only the logs of checked out branch

Modifying last commit

After a commit, to redo that commit for additional changes git commit --amend can be used. This is useful when accidentally forgot to add files to a commit.

1
2
3
git commit -m "Serious bugfix"		# Accidentally commited without forgotten_files
git add forgotten_files # forgotten_files is added
git commit --amend # Previous commit will be modified by adding forgotten_files

If this command is used just after a commit, editor will show up to edit the commit message. So this can be used to modify minor changes in the latest commit message.

1
2
git commit -m "Serious busfix"
git commit --amend # Edit busfix -> bugfix

If you want to remove files that were added in a commit mistakenly:

1
git reset --soft HEAD~N

N is the commit number relative to HEAD. Which is 1 for the latest commit.

You will see last commit was deleted and all the files in staging area again. Now you can remove unwanted files from staging area.

Removing files from staging area

After files are staged in the staging are using git add command, to remove the file from the staging area:

1
git reset HEAD file_name

Unmodifying a modified file

When a file is modified which is tracked by git, git show the modified flag in git status. Following command can be used to discard the modification.

If this is used, git will no longer show it as modified, also modificationsto that file will be gone.

1
git checkout -- file_name

Managing Remote

  • Adding a remote repository to local
1
2
git remote add <shortname> <url>
git remote add origin [email protected]:aagontuk/cheatsheets.git
  • View the list of remote
1
git remote show
  • View details of a remote
1
2
git remote show <shortname>
git remote show origin
  • Fetching data from remote. Fetching will only download data from remote but will not merge automatically. After fetching you have to manually merge.
1
2
git fetch <remote>
git fetch origin
  • Pulling will fetch data and merge automatically
1
git pull
  • Pushing local to remote
1
2
git push <remote> <branch>
git push origin master
  • Renaming a remote
1
git remote rename back backports
  • Changing URL of a remote
1
2
git remote set-url <remote_name> <new_url>
git remote set-url origin https://github.com/aagontuk/cheatsheets.git
  • Removing a remote
1
git remote remove paul
  • Dry run merge
1
git merge --no-commit --no-ff $BRANCH_NAME

This will stage the changes but won’t commit them. To see the staged changes:

1
git diff --cached

To undo the merge:

1
git merge --abort

Tagging

  • View tags
1
git tag
  • Search for tags
1
git tag -l "v1.1.*"
  • Creating a annotated tag
1
git tag -a v1.2 -m "Version 1.2"
  • Creating a lightweight tag
1
git tag v1.2-rc4

In annotated tag when shown with git show tag information and commit will be shown. But in lightweight tag only commit information will be shown.

  • Tagging later in a specific commit
1
2
git tag <tag> <sha>
git tag v1.2 a02fed6
  • Viewing tag information
1
2
git show <tag>
git show v1.2
  • Tags have to be pushed separately in the remote. To push tags:
1
2
git push origin v1.2	# will push tag v1.2
git push origin --tags # will push all tags
  • To delete a tag from local:
1
git tag -d v1.2-rc4

This will only delete from local. To also delete from remote:

1
git push origin :refs/tags/v1.2-rc4
  • To checkout in a tag following command have to be used.
1
git checkout v1.1

Checkout in a tag will leave the repo in detached HEAD state. After checking out to make further commits to this a new branch have to be created.

1
git checkout -b version1.1 v1.1

Aliasing

Creating command aliases:

1
git config --global alias.unstage 'reset HEAD --'

Now you can use git unstage file1 instead of git reset HEAD -- file1 to unstage file1.

Aliasing a external tool:

1
git config --global alias.mergetool '!meld'

Branching and Merging

Creating a new branch

Create a new branch named testing:

1
git branch testing

Switching to a branch

Switch to a branch named testing:

1
git checkout testing

This shortcut can be used to create and checkout to a new branch in one command:

1
git checkout -b testing

This will create branch testing and switch to it.

Show all the branches

1
git branch --all

Deleting a branch

1
git branch -d testing

Renaming a branch

For example you want to change your branch name from bad_branch_name to good_branch_name.

1
git branch --move bad_branch_name good_branch_name

This will only change the local branch name. To change the remote branch name too you have do first push the renamed branch to remote:

1
git push --set-upstream origin good_branch_name

Now there will be two branch in remote remotes/origin/bad_branch_name and remotes/origin/good_branch_name.

Remove remote/origin/bad_branch_name

1
git push origin --delete bad_branch_name

Merging a branch to another branch

Let’s say you are now in testing branch. You have added some changes and now want to merge the commits of testing branch into master branch:

1
2
git checkout master
git merge testing

Remote Branches

Remote branches are named like <remote_name>/<remote_branch_name>. For example origin/master. This points to the latest commit of your remote repository at the time of your last synchronization.

Fetching commits from remote repository

1
git fetch <remote_name>/<remote_branch_name>

This will synchronize the remote branch with the local repository. For example if your remote branch name is origin/master this will synchronize orgin/master with the local repository.

Note that this will not merge commits from remote branch(origin/master) into your local branch(master). You have to do it manually.

Pushing/fetching a new branch to/from remote repository

For example you have created a new branch named testing and added some commits to it. Now you want to upload it to your remote repository:

1
git push origin testing

This will create a new branch named testing in your remote repository. If you want to name the remote branch name different from the local branch name:

1
git push origin testing:experimental

For the next example lets say you didn’t name the differently. Now if someone who is also forked that remote repository do fetch, a new remote branch named origin/testing will be created in their local repository. Not this will not create any new local branch to work on for them. They will have to do it manually:

1
git checkout -b testing origin/testing

You can also use this shortcut if you keep the local branch name and remote branch name same:

1
git checkout testing

If you want to give a different name to the local branch:

1
git checkout -b experimental origin/testing

Tracking Branch

Checking out a local branch from a remote branch creates a tracking branch. Tracking branch means that local branch and the remote branch has a direct relationship with each other. If you are in that local branch when you do a push git will automatically
push the commits to the remote branch and when you do pull git will automatically merge all the commits from the remote branch
to that local branch.

For example in above example if you are in testing branch, when you do a pull git will merge all the commits from origin/testing to testing automatically. And after working on testing when you do push git will automatically push all the commits to origin/testing.

If you have a local branch which don’t have a tracking branch or you want to change the remote tracking branch. For example if you currently on testing branch and want to set origin/testing as you remote tracking branch:

1
git branch -u origin/testing

Pulling

For automatically fetching commits from remote branch and merging with local tracking branch use:

1
git pull

It is equivalent to:

1
2
git fetch
git merge

Deleting a remote branch

1
git push origin --delete testing

This will delete branch testing from remote repository.


Cherry Picking

In Git, you can cherry-pick a commit (a set of changes) from an existing branch, and apply those changes to another branch. Cherry-picks can help you:

  • Backport bug fixes from the default branch to previous release branches.
  • Copy changes from a fork to the upstream repository.

For merging a specific commit or set of commits from a brach to another branch.

1
2
3
4
5
6
7
8
9
git checkout my-feature
# ... changes to the working tree (your file system).
git add .
git commit -m "Minor tweaks."

git checkout master

git cherry-pick -
git cherry-pick COMMIT_HASH

In the command above, the - refers to the most recent commit of the my-feature branch, whereas COMMIT_HASH is the hash of the commit you cherry picked from my-feature branch, these will create new commits to master branch.

For cherry picking a set of commits(A, B, C, and D are commit hash):

1
git cherry-pick A^..D

The command above assumes that A is an older commit than D. Also, it includes both extremities. What if you want to exclude A? In such case, you’d run this:

1
git cherry-pick A..D


Rewriting history

Removing commits

Lets say your commit history looks like this:

Number (newest-oldest)Commit HashCommit Message
14d884d0Important fix
25540e24server fix
34c5199fclient fix
47879940My awesome fix
52c27f34minor fix

If you want to remove consecutive commits you can use git rebase. For example if you want to remove commit 3 and 4 you can do following:

1
git rebase --onto <branch_name>~<number of the oldest commit you want to remove> <branch_name>~<number of the oldest commit you want to keep> <branch_name>

So if you want to remove 3 and 4 in master branch:

1
git rebase --onto master~4 master~2 master

Now if you want to remove non-consecutive commits, you can use git cherry-pick. For example if you want to remove commit 2 and 4 you have to do following procedure:

  • Go to oldest usable commit(In this case 5)
  • Create a new branch
  • cherry pick the commits you want to keep(In this case 3 and 1) after that commit(In this case 5).
  • Checkout to the previous branch you were in.
  • Do a hard reset to the last usable commit(In this case 5)
  • Merge the new branch in this branch

So lets say in our case you were in master branch and want to remove commit 2 and 4:

1
2
3
4
5
6
7
git checkout 2c27f34
git checkout -b new_branch
git cherry-pick 4c5199f
git cherry-pick 4d884d0
git checkout master
git reset --hard 2c27f34
git merge new_branch

After deleting the commit(s) you have to force push in the remote repository:

1
git push --force origin master

Note that deleting commits can introduce merge conflicts.