Branching is the Cat’s meow. It allows you to replicate a codebase that exists in a Git repository and try out that crazy idea without completely screwing up your original code. Whaaa?? Example you say! Well I am currently battling with the grid that ships in Twitter’s Bootstrap Framework. I am thinking about scrapping it and creating my own. So, rather than effing up the code I have been working on over the past couple weeks, I created a branch to play around with different grid implementations. That way I can simultaneously keep working on my original codebase in the master branch whilst playing around with different grid implementations in my test branch. If things work out with my custom grid, I can merge those changes in the test branch with my master branch. Pretty gnarly.

Viewing, Creating & Checking Out Branches

git branch
View a list of all branches in the repository. Note: the branch with asterix next to it is the active branch – aka “checked out” branch.
git branch [name_of_branch]
Create a new branch named [name_of_branch].
git checkout [name_of_branch]
Switch to the branch named [name_of_branch].
git checkout -b [name_of_branch]
Create a new branch named [name_of_branch] and check it out. Note: this shortcut combines the two previous commands in to a single command.
git checkout -- [file.ext]
Checkout the last committed [file.ext] file and overwrite the [file.ext] version in your working directory. Essentially this is the “undo all changes” option. The “–” following the checkout command tells Git that you want to checkout a file rather than a branch.
cat .git/HEAD
View where the HEAD is currently pointing to. This command allows you to confirm that the HEAD is pointing to the last commit within the intended branch.

Note: whenever you create a new branch, make sure that you are instantiating it off of the intended source branch. For example, if you currently have two branches and you want to create a third branch, be sure to run the git branch command and verify that the branch you intend on branching off of is the currently active/checked out branch.

Viewing Differences, Renaming & Removing Branches

git diff [name_of_branch]..[name_of_branch_2]
Compare the [name_of_branch] branch with the [name_of_branch_2] branch. Note: the order of the branch names does not matter. Typically, the most recently edited branch would be last though. In the example above the [name_of_branch] branch would be the “old state” and the [name_of_branch_2] branch would be the “new state”.
git branch --merged
Show a list of all branches in which the entirety of their commits are included in the current branch. Note: this means that we could delete any of the branches in this list without losing data.
git branch -m [name_of_branch] [new_name_of_branch]
Rename the [name_of_branch] branch to [new_name_of_branch].
git branch -d [name_of_branch]
Remove/delete the [name_of_branch] branch. Note: you cannot delete the branch that you are currently on (the checked out branch). Also, if the branch you are attempting to delete has not been fully merged with another branch, Git will inform you.
Posted by: John Dugan

Comments