Git - git branch

git

How can we create a new branch?

To create a new branch named "experimental", use:

git branch experimental

If you run 'git branch' alone, you’ll get a list of all existing branches. The asterisk marks the branch you are currently using.

How can we delete a branch?

git branch -d experimental

This command ensures that the changes in the experimental branch are already in the current branch. If you develop on a branch crazy-idea, then regret it, you can always delete the branch with:

git branch -D crazy-idea

What is the purpose of the 'git branch -r' command?

Git also keeps a pristine copy of Alice’s master branch under the name "origin/master":

git branch -r

How can we display a list of branches?

Use the git branch command without any option. The current one is denoted by an "*".

How can we create a new branch from an older revision?

In Git you can make commits on top of the older revision and use it as another branch. I am not sure on how to do this yet.

With Subversion, we can create a branch from any revision, and then you can make change in the new branch and commit it (perhaps, we we commit, it will complain or won't let us). On the other hand, also with Subversion, if we switch our working copy to a particular revision, it is stuck with that particular revision, and will not let us commit until we switch back to the head of the branch.

How can we create a new branch and automatically switch to using it?

Use the -b option with the checkout command:

git checkout -b branchName

How can we switch our working copy to another branch?

git checkout branchName

How can we list available branches?

git branch

How can we delete a branch?

git branch -d test

Why should we create branches?

Instead of creating branches, we can create another temporary repository when we work on a specific feature. But depending on the environment that you are in, you may not want to litter the public namespace with so many "feature" repositories. In such environment, we can create branches.

How can we list local branches?

git branch
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-ShareAlike 3.0 License