Recipes for working with branches in `git`
If you've ever used `git`, and had to create and manage branches, you may have been impressed by how flexible and accomodating it is, but discouraged by how hard it can be to remember all the incantations. That's why I've created a list of recipes for basic branching and merging operations. Enjoy!
These recipes were distilled by me based on information found in an awesome book by Scott Chacon.
# create a branch git branch [new-branch] [base-branch] # switch to an existing branch git checkout [branch] # push branch to remote git push [remote] [branch] # pull changes from remote # pulls down remote branches, # but doesn't set up tracking branches git fetch [remote] # create and switch to a new branch git checkout -b [branch] # create a new branch from a specific commit (starting point) git branch [branch] [sha-1 hash] # create new local branch from remote branch git checkout -b [localbranch] [remote]/[remotebranch] # or git checkout --track [remote]/[branch] # see http://git-scm.com/book/en/Git-Branching-Remote-Branches#Tracking-Branches # for more information about tracking branches # merge branch into master # have to first switch to master git checkout master git merge [branch] # check that the merge worked # shows an "unmerged" section if any files failed git status # delete a branch (after merging it) git branch -d newbranch # see last commit on each branch git branch -v # see merged branches (to current branch) git branch --merged # see unmerged branches (to current branch) git branch --no-merged # delete remote branch # the syntax is widely acknowledged to be obtuse git push origin :newbranch
This comment has been removed by the author.
ReplyDeleteMight be good to elaborate on what "git checkout --track [remote]/[branch]" actually does - as its a powerful command.
DeleteGood call -- I've needed that a couple times recently! I started trying to explain it ... but then decided to just add a link to the Pro Git book.
DeleteHere's my git recipe of the day :
ReplyDelete######### start by creating a new branch ########
- git checkout -b some_refactoring
######### write some code. ##################
- git add -A .
- git commit
- git push origin some_refactoring
######### Now, merge it into master ###########
- git checkout master
- git merge feature1