A Quick & Simple Guide to Advanced Git Commands

The following guide compiles a list of useful advanced Git commands that will make our everyday coding life easier.

1. Merge vs. Rebase

The merging workflow of git allows us to integrate changes from one branch into another. Rebasing serves the same purpose, but it does so in a more streamlined fashion. Rebasing and merging use very different methods to integrate changes from one branch into another.


If the development of the feature branch is completed and we want to move changes into the master branch, we use merge command.

git checkout mastergit merge feature


Let’s rewind, and pretend that we still have to do some work on the feature branch but before we continue working on our feature branch, we decide we want to bring in the latest changes from master to keep things fresh.

git checkout featuregit rebase master
  • Merge will generally create an extra commit.
  • Merge and rebase will handle conflicts differently. Rebase will present conflicts of one commit at a time where merge will present them all at once.
  • Use rebase when we want to keep a linear commit history

2. Resolve conflicts after a Git rebase

When we perform a git rebase, chances are we will run into a situation where a merge conflict is introduced. A merge conflict occurs when the exact same lines were edited in the same file by two commits. In that case, Git has no way of knowing what’s correct — we will have to look at the changes and decide how we want to handle it. When merge conflicts occur during rebase, we get the following error.

error: could not apply ba66749... something to add to patch AWhen you have resolved this problem, run "git rebase --continue".
If you prefer to skip this patch, run "git rebase --skip" instead.
To check out the original branch and stop rebasing, run "git rebase --abort".
Could not apply ba66749b807321c786a65bbb9b2169b7a6ed957d... Change fake file
  • git rebase --skip : to completely skip the commit. Hence none of the changes introduced by the commit(ba66749) will be included in the history.
  • We can fix the conflict using the standard procedure for merge conflict resolution.

3. Temporarily stash changes

In most cases, when we edit, add, or delete a file in your working repository, we would stage and then commit those changes to apply them to our project.

$ git stash
$ git stash push <path>

4. Delete commit - locally and remotely

When working with Git we will find that sometimes commits need to be removed as they have introduced a bug or need to be reworked. Deleting a commit in Git must be approached in one of two ways, depending on if we have or have not pushed changes.

# Use --soft if you want to keep your changes
git reset --soft HEAD~1
# Use --hard if you don't care about keeping the changes you made
git reset --hard HEAD~1
# Use --soft if you want to keep your changes
git reset --soft <commit-id>
# Use --hard if you don't care about keeping the changes you made
git reset --hard <commit-id>
git push origin +<branch-name>
# Delete last two commits
git reset --hard HEAD~2
# Delete last n commits
git reset --hard HEAD~n

5. Cherry-pick

When we are working with a team of developers on a medium to a large-sized project, managing the changes between a number of git branches can become a complex task. Sometimes we don’t want to merge a whole branch into another, and only need to pick one or two specific commits. In order to perform this operation, we can use the cherry-pick command. It takes changes from a specific commit and applies them to your current branch in a new commit.

git cherry-pick <commit-id>
a - b - c - d        Master
e - f - g Feature
git cherry-pick f
a - b - c - d - f    Master
e - f - g Feature

6. Clean - remove all untracked changes

The clean is a builtin command to clean up the untracked files. Untracked files are files that have been created within our repo’s working directory but have not yet been added to the repository’s tracking index using the git add command. Before running the actual command and removing untracked files and directories, use the -n option that will perform a “dry run” and show us what files and directories will be deleted:

# list all files that would be removed
git clean -n
# list all files/directories that would be removed
git clean -n -d
# Output of git clean -n command
Would remove project/blog/example.txt
# Output of git clean -n -d command
Would remove project/test/
Would remove project/blog/example.txt
git clean -d -f
git clean -f     # remove untracked files
git clean -d -f # remove untracked files/directories

7. Who did all this?

If the production server is broken, we want to know the details of changes like who changed it or when. Just do a git blame. It is used to examine the contents of a file line by line and see when each line was last modified and who was the author of the modification of that line.

git blame <fileName>
git blame manage.pyOutput
66fe3ebef (Tony 2020-06-14 11:33:01 +0530 1) import os;
3e537ffda (steve 2020-05-29 18:45:17 +0530 2) import sys;
5c5490232 (Tony 2020-06-14 09:55:03 +0530 3) import requests;

A passionate Software Engineer having experience of building Web and Mobile applications with Django,Laravel,Nodejs and some other cool libraries and frameworks