Category Archives: Git

Git Notes – Stashing and Merging

I first started using Git about 2 years ago. Since then i’ve really only ever done minor things with it, pull, add, edit, commit, push, make branches… the basic things. I’ve been spending some time reading about Git and watching Git Essential Training on Lynda.com to better familiarize myself with the things I don’t normally do.

Merging

In the past I had merged using ‘git merge –no-ff branch-name‘. I didn’t know what the “–no-ff” flag actually did (just that it stood for “no fast forward”) though. When you do a normal ‘git merge branch-name‘ it will attempt to merge it in as if the branch did not exist (fast-forward). So reviewing the timeline you would still see commits you made, but no reference to the branch. That being said, it only actually does this if the parent SHA value for your commit matches the commit at the HEAD of the branch being merged into.

Conflicts

I hate conflict (in life and coding!). Take the following for example:

<<<<<<< HEAD
if($var == 'test')
=======
if ($var == 'test' || $var == 'test2')
>>>>>>> branch-name

I’ve always looked at this sort of merge conflict as intimidating because of how hands on it is. What the above means is that from <<<<<<< HEAD until the ======= delineator, that is the code that exists in the branch you are merging into. The code from the ======= to the >>>>>>> is the code you are attempting to merge. So you need to either decide at this point that only one is right, or to merge them together and continue onward.

Sidebar: Something that may be helpful to someone is that I was presented with a merge conflict about a whitespace character that git had on the master branch, but not in my file. I removed the whitespace character and proceeded onward. I was then presented with another whitespace character conflict. This time however I removed it and attempted to git add the file only to be told that I had not made a chance to the file. It would not add the file into the list of changes and kept telling me that the file needed a resolution. In this case knowing that the whitespace wouldn’t harm anything I skipped that merge and proceeded as normal.

Stash

Stashes are probably my favorite new thing to find out about. If i’m working on some changes, but i’m not prepared to commit them, you can stash them away until you can finish them! As you may have gathered, stashing is when you pull the modified files out of the current branch using   “git stash save “the comment about the files you are saving”.  After you stash files Git runs a git reset hard HEAD  which tosses any uncommitted changes out and resets the files to the HEAD.