Working with branches in your local repo is so easy with git that using feature branches is the normal workflow for most users of git.
Unlike with subversion, branches and tags are distinctly different. A branch is dynamic and moves with each commit. Whereas a tag is static and points to a specific point in the history of the repository.
master branch is usually created by git during
repository initialization. It is best to think about this branch as
equivalent to Subversions
trunk. In theory, you can rename or
master branch, but it’s best to just leave it alone.
Let’s create a branch off of
master in our
~/repos/example/ repository we created early:
$ cd ~/repos/example $ git checkout master $ git branch $ git branch train/stuff $ git branch $ git checkout train/stuff $ git branch
Now you should have a branch called
train/stuff and have it
You can also checkout and create a branch at the same time:
$ git checkout -b train/doodle master $ git branch
Notice that we were still on the
train/stuff branch, but we
created a new branch called
train/doodle relative to
master without having to checkout
Let’s make a change on the
$ echo '# Change on doodle branch' >> hello $ git add hello $ git commit
Now make a change on the
$ git checkout train/stuff $ echo '# Change on stuff branch' >> hello $ git add hello $ git commit
Now the fun begins. Let’s go back to the
master branch and
merge the changes from the
$ git checkout master $ git merge train/doodle $ git merge train/stuff # <-- Oops! We got a merge conflict!
Before we can move on with our work, we need to fix the merge conflict:
$ git mergetool $ git status $ git diff $ git diff --cached $ git commit $ git log --oneline --graph
You can configure git to use the mergetool of your choice (see Diff Tool and Merge Tool Configuration).
Once you have merged all the changes on your feature branch back to
master, you can delete the branch:
$ git checkout master $ git branch -d train/doodle $ git branch
You can also delete a branch that you consider a dead end without
merging it back to
master (or some other branch):
$ git checkout -b train/deadend master $ echo '# this is a deadend branch' >> hello $ git add hello $ git commit $ git checkout master $ git branch $ git branch -d train/deadend # <-- Notice that git fails here $ git branch -D train/deadend $ git branch