Rakesh Jha

Back to Top

Interesting things to know about GIT

Table of Contents

This post is not intended to be about basic git stuff, but stuff you probably didn't know about or used just once or twice.

  1. Recover a commit from reset --HARD

    Just use:
     git reflog 
    

  2. Diff

    If you want to check what is the difference between two branches, you can simply do:
     git diff branch1..branch2 
    

  3. Show a commit that matches a regex


    You can find the last commit in with the message contains the string that was passed, in this case fixes, using the following command:

     git show :/fixes 
    

  4. Limit git push default actions

    If you run
     git push 
    
    which is the default action is to push all branches on the remote. That can cause many accidents, if you want to prevent them, do this:

     git config --global push.default tracking 
    
    UPDATE: Git 2.0 removed this default http://blog.nicoschuele.com/posts/git-2-0-changes-push-default-to-simple
    

  5. Checkout a branch, rebase and merge to master


    Do this magic:

     git rebase HEAD feature && git rebase HEAD @{-2} 
    

  6. Git stash

    If you can't commit, because you did not finish your work yet, but something urgent comes up you can use git stash to save those changes, commit your urgent task and then git stash pop to bring your stuff back.
  7. Aliases


    Tired of typing checkout again and again? Go ahead and:

     git config --global alias.co checkout 
    
    Now, you can checkout to master by doing: 
    
     git co master 
    

  8. Renaming a local branch

    You can easily rename a local branch with:

     git branch -m old-name new-name 
    

  9. Searching for an author

    You can search for a commit by an author by using:

     git log --author=jrakesh 
    

  10. Status with options

    Most people just use git status, but you can pass arguments to change the way status is shown. With the command:

     git status -sb 
    
    you have an output like this:
    
         ## master
             M Gemfile
             M Gemfile.lock
             M app/controllers/home_controller.rb
             M app/views/home/index.html.erb