Search:   Dictionary All Posts
Store Your Knowledge at the Brain Bank!

Git Branch - Revert - Checkout - Merge

Summary - Save the current version of git for future reference in a new branch, revert to an older git copy. Also information on creating and checking out a branch, merging branches, and handling a merge conflict.
Recently I worked a long time on a redesign. I got so frustrated and went all the wrong way I wanted to revert back my latest version. This was way more complicated than I thought it should be, so for my future reference, here is the final fix.

INTERLUDEUse the github guide for answers to all things git...

1. Create a new branch, perhaps called 'Redesign'
git checkout -b Redesign

2. Save current files of redesign and commit changes
git add .   
git commit -m 'save redesign files'

3. Move back to master
git checkout master

4. Revert to previous version
git log
// get the previous version you want to make the current version on master

git reset --soft versionNumberCopiedFromAbove
// this sets the head of the master to the version you pasted above
// perhaps you can do git reset --hard     not sure why one is better than the other

gets you out of git log

Show branches
git branch

PS - The answer to this in the future is to always work in a new branch after making a commit. Every feature or addition should be on a new branch and merged to the master after it has been tested and agreed to by your team.

Normal branching and merging...

Follow this excellent guide. It explains how a normal git process works in a live environment like at work. And here is a visual interactive tool that is good to get your head wrapped around literally what git looks like in graphical representation.

You have some work you have to do. You create and checkout a new branch and start working on it. Meanwhile, you get a request from your boss to fix a bug immediately. You have to checkout master, create a new branch for the bug fix and begin working on it. You add and commit your changes, then you checkout master again and merge the bug fix branch into master. Then you go back to work on your first branch by checking it out. you finish your work, add and commit your changes then checkout master again. you merge the branch you were working on into master.

delete previous branch
You should also delete previous branches that are complete.

merge conflicts
If you merge a branch to master and you or someone else has merged a branch they were working on into master and both of you happened to change the same section of code in the same file you will encounter a merge conflict. Git will not allow the merge and you must choose to either open up the file in your branch or the master and resolve the conflict. Git will mark what is conflicting with the branch you are in (master) on top and the branch that is conflicting below it. You can override changes by replacing the code with the proper code. After fixing the code and saving the file, git will ask you if you would like to merge these changes and resolve the conflict. If you agree git will add the changed file which you can see by running 'git status'. Then, you must commit the changes and everything will be up and running with the latest code.