Understanding git pull command

Git pull often gets misunderstood by newbies. It is a compound command and 2 things happen when you run this command

  • git fetch
  • git merge (be prepared for merge conflicts)

Git has an option to store your current changes which you do not intend to commit to origin(master) repository yet. You can do this by using the git stash command. Let’s say you have a scenario where you have made some changes in your current HEAD and would like to get the latest stuff from origin, then run the following commands

  • git stash (saves your unfinished work on a stack of unfinished changes)
  • git pull
  • git pop (apply the top stash)

If you do not want to stash your changes and are ready to commit your changes but would like to get the latest changes from origin then use

  • git pull –rebase

This will alter the history of your local commits. This command will first get the changes from origin and then overlays your local changes on top of it. Here is a picture to understand this:

 

Before Rebase

o - o - X - Y - Z (local/master)
               \
                1 - 2 - 3 (origin/master)

After Rebase

o - o - 1 - 2 - 3 - X - Y - Z (local/master)


About sasamka

Senior DevOps Principle engineer at Broadcom

Leave a comment