10 Useful Git Tips

Over the past few years git has vastly grown in popularity to become one of the most used version control systems. It is used by developers coding in various languages and teams of all sizes, from small open-source projects to huge codebases like the linux kernel.

In this article we are going to share with you a few tips that could improve your git experience and workflow.


git log --no-merges

This git command shows the whole commit history but skips commits that merged two branches together or solve a merge conflict. This allows you to quickly see all the changes done to the project, without having merge commits cluttering the git history.

$git log --no-merges

commit e75fe8bf2c5c46dbd9e1bc20d2f8b2ede81f2d93
Author:  John
Date:   Mon Jul 10 18:04:50 2017 +0300

    Add new branch.

commit 080dfd342ab0dbdf69858e3b01e18584d4eade34
Author:  John
Date:   Mon Jul 11 15:40:56 2017 +0300

    Added index.php.

commit 2965803c0deeac1f2427ec2f5394493ed4211655
Author:  John
Date:   Mon Jul 13 12:14:50 2017 +0300

    Added css files.


git revert --no-commit [commit]

Git revert generates a new commit that undoes the changes made by existing commits and generates a new commit with the resulting content. If you want to to revert the named commits and avoid the automatic commits, you can use the flag --no-commit or the shorthand -n.


git diff -w

Git diff shows the changes between two commits, two working trees or two files on disk. When multiple people work on the same project, often there are changes due to text editor's tab and space setting. In order to ignore differences caused by whitespaces when comparing lines, you can use it with the -w flag.


git diff --stat

Shows how each file has been changed over time. You can add 3 parameters: width to override the default output width, name-width to set the width of the filename and count to limit the output to the first number of lines.

$ git diff --stat
 index.php | 83 +++++++++++++++++++++++++++++---------------------------
 1 file changed, 43 insertions(+), 40 deletions(-)

$ git diff --stat-width=10
 index.php | 83 +++---
 1 file changed, 43 insertions(+), 40 deletions(-)

git reset --soft HEAD^

Reset the head to a certain commit without touching the index file and the working tree. All changes made after this commit are moved to "staged for commit" stage. After that you just need to run git commit to add them back in.


git stash branch [branch-name] [stash]

This command creates a new branch named branch-name and check it out, then applies the changes from the given stash to it and drops the stash. If no stash is given, it uses the latest one. This allows you to apply any stashed changes into a safer environment, that can later be merged into master.


git branch -a

It shows a list of all remote-tracking and local branches. You can use the --merged flag to see only the branches that are fully merged to the master branch. This way you can track your branches and find out which ones aren't used anymore and can be deleted.

$ git branch -a

  dev
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/dev

git commit --amend

With git commit --amend you can change your previous commit, instead of making a new one. If you haven't pushed your changes to a remote branch, you can use this command to amend the most recent commit, adding your latest changes and even changing your commit message.


git pull --rebase

Git pull --rebase forces git to first pull the changes and then rebase the unpushed commits on top of the latest version of the remote branch. The --rebase option can be used to ensure a linear history by preventing unnecessary merge commits.


git add -p

When you use this command, instead of immediately adding all the changed to the index, it goes through each change and asks what you want to do with it. This way, it allows you to interactively choose exactly what you want to be committed.


diff --git a/package.json b/package.json
index db78332..a814f7e 100644
--- a/package.json
+++ b/package.json
@@ -6,7 +6,6 @@
   },
   "devDependencies": {
     "bootstrap-sass": "^3.3.7",
-    "gulp": "^3.9.1",
     "jquery": "^3.1.0",
     "laravel-elixir": "^6.0.0-11",
     "laravel-elixir-vue-2": "^0.2.0",
Stage this hunk [y,n,q,a,d,/,e,?]? 

Bootstrap Studio

The revolutionary web design tool for creating responsive websites and apps.

Learn more

Related Articles

Comments 1

  • I really like the "git add -p", which i use on every commit i make. It really helps to check if you've forgotten some traces in your code. Just one small precision, "git add -p" ignore new files created. So don't forget to make a "git status" to see if all your changes are ready for commit ! I was really boring to make a commit assuming that every thing was in, switch branch, beginning some coding, make a git stash because it has to be remade in another way and then fund that my new created files in the first commit have gone :/