Questions tagged [git-branch]

git-branch is the Git command that manages branches.

git branch is the command that manages branches within a Git repository.

The default branch is called master.

To create a new branch:

git branch <branch-name>

To see a list of all branches in the current repository:

git branch

Switching to another branch:

git checkout <branch-name>

Creating a new branch and switch to it in one step:

git checkout -b <branch-name>

Deleting a branch:

git branch -d <branch-name>

Create a branch with the changes from the current branch:

git stash
git stash branch <branch-name>

More information on git-branch manual page.

2836 questions
765
votes
9 answers

Cleaning up old remote git branches

I work from two different computers (A and B) and store a common git remote in the dropbox directory. Let's say I have two branches, master and devel. Both are tracking their remote counterparts origin/master and origin/devel. Now while on computer…
Jayesh
  • 46,729
  • 19
  • 69
  • 96
742
votes
19 answers

How do I list all remote branches in Git 1.7+?

I've tried git branch -r, but that only lists remote branches that I've tracked locally. How do I find the list of those that I haven't? (It doesn't matter to me whether the command lists all remote branches or only those that are untracked.)
James A. Rosen
  • 60,042
  • 58
  • 173
  • 260
712
votes
5 answers

View a file in a different Git branch without changing branches

Is it possible to open a file in a git branch without checking out that branch? How? Essentially I want to be able to open a file in my github pages branch without switching branches all the time. I don't want to modify it, just want to view it.
Schneems
  • 13,095
  • 8
  • 50
  • 79
699
votes
12 answers

How do I rename both a Git local and remote branch name?

I have four branches like master -> origin/regacy, FeatureA -> origin/FeatureA. As you can see, I typed the wrong name. So I want to rename a remote branch name (origin/regacy → origin/legacy or origin/master) I try the command below: git remote…
JayD
  • 7,483
  • 5
  • 11
  • 14
650
votes
12 answers

How is a tag different from a branch in Git? Which should I use, here?

I am having some difficulty understanding how to use tags versus branches in git. I just moved the current version of our code from cvs to git, and now I'm going to be working on a subset of that code for a particular feature. A few other developers…
Bialecki
  • 26,315
  • 33
  • 78
  • 103
649
votes
2 answers

Why does git perform fast-forward merges by default?

Coming from mercurial, I use branches to organize features. Naturally, I want to see this work-flow in my history as well. I started my new project using git and finished my first feature. When merging the feature, I realized git uses fast-forward,…
Florian Pilz
  • 7,692
  • 3
  • 20
  • 28
626
votes
5 answers

Git remote branch deleted, but still it appears in 'branch -a'

Let's say I had a branch named coolbranch in my repository. Now, I decided to delete it (both remotely and locally) with: git push origin :coolbranch git branch -D coolbranch Great! Now the branch is really deleted. But when I run git branch -a I…
yonix
  • 9,925
  • 7
  • 30
  • 50
606
votes
7 answers

How to create a new branch from a tag?

I'd like to create a new master branch from an existing tag. Say I have a tag v1.0. How to create a new branch from this tag?
Andrew
  • 196,883
  • 184
  • 487
  • 673
539
votes
9 answers

Git branch command behaves like 'less'

When I use the git branch command to list all branches, I see the output of git branch | less. The command git branch is supposed to show a list of branches, like ls does for files. This is the output I get: How do I get the default behaviour of…
DenniJensen
  • 6,376
  • 3
  • 13
  • 31
511
votes
5 answers

How do I push a local Git branch to master branch in the remote?

I have a branch called develop in my local repo, and I want to make sure that when I push it to origin it's merged with the origin/master. Currently, when I push it's added to a remote develop branch. How can I do this?
picardo
  • 23,016
  • 32
  • 98
  • 148
498
votes
11 answers

Using Git, show all commits that are in one branch, but not the other(s)

I have an old branch, which I would like to delete. However, before doing so, I want to check that all commits made to this branch were at some point merged into some other branch. Thus, I'd like to see all commits made to my current branch which…
sircolinton
  • 5,486
  • 3
  • 16
  • 17
485
votes
9 answers

Push commits to another branch

Is it possible to commit and push changes from one branch to another. Assume I commited changes in BRANCH1 and want to push them to BRANCH2. From BRANCH1, is it valid to do: git push origin **BRANCH2** And then reset BRANCH1?
jviotti
  • 15,113
  • 22
  • 82
  • 134
473
votes
3 answers

What is the difference between origin and upstream on GitHub?

What is the difference between origin and upstream on GitHub? When a git branch -a command is done, some branches have a prefix of origin (remotes/origin/..) while others have a prefix of upstream (remotes/upstream/..).
jan
  • 5,061
  • 4
  • 14
  • 5
440
votes
6 answers

How do I run git log to see changes only for a specific branch?

I have a local branch tracking the remote/master branch. After running git-pull and git-log, the log will show all commits in the remote tracking branch as well as the current branch. However, because there were so many changes made to the remote…
Highway of Life
  • 18,392
  • 14
  • 43
  • 68
429
votes
11 answers

How do I merge changes to a single file, rather than merging commits?

I have two branches (A and B) and I want to merge a single file from branch A with a corresponding single file from Branch B.
Isuru
  • 6,867
  • 8
  • 26
  • 37