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.

3220 questions
2256
votes
34 answers

How do I fetch all Git branches?

I cloned a Git repository containing many branches. However, git branch only shows one: $ git branch * master How would I pull all the branches locally so when I do git branch, it shows the following? $ git branch * master * staging * etc...
David542
  • 104,438
  • 178
  • 489
  • 842
2113
votes
48 answers

Remove tracking branches no longer on remote

Is there a simple way to delete all tracking branches whose remote equivalent no longer exists? Example: Branches (local and remote) master origin/master origin/bug-fix-a origin/bug-fix-b origin/bug-fix-c Locally, I only have a master branch. Now…
Mailo Světel
  • 24,002
  • 5
  • 30
  • 40
2095
votes
5 answers

How to replace master branch in Git, entirely, from another branch?

I have two branches in my Git repository: master seotweaks (created originally from master) I created seotweaks with the intention of quickly merging it back into master. However, that was three months ago and the code in this branch is 13…
Jason
  • 22,645
  • 5
  • 29
  • 51
1963
votes
14 answers

How to get just one file from another branch

I have a main branch with a file called app.js. I made changes to this file on an experiment branch. I want to apply only the changes made to app.js from experiment onto the main branch.
Nick Vanderbilt
  • 36,724
  • 29
  • 83
  • 106
1939
votes
32 answers

How can I get a list of Git branches, ordered by most recent commit?

I want to get a list of all the branches in a Git repository with the "freshest" branches at the top, where the "freshest" branch is the one that's been committed to most recently (and is, therefore, more likely to be one I want to pay attention…
Joe White
  • 94,807
  • 60
  • 220
  • 330
1928
votes
29 answers

Why do I need to do `--set-upstream` all the time?

I create a new branch in Git: git branch my_branch Push it: git push origin my_branch Now say someone made some changes on the server and I want to pull from origin/my_branch. I do: git pull But I get: You asked me to pull without telling me…
Ram Rachum
  • 84,019
  • 84
  • 236
  • 374
1637
votes
12 answers

Create a branch in Git from another branch

I have two branches: master and dev I want to create a "feature branch" from the dev branch. Currently on the branch dev, I do: git checkout -b myfeature dev ... (some work) git commit -am "blablabla" git push origin myfeature But, after…
revohsalf
  • 16,443
  • 3
  • 15
  • 4
1521
votes
12 answers

Default behavior of "git push" without a branch specified

I use the following command to push to my remote branch: git push origin sandbox If I say git push origin does that push changes in my other branches too, or does it only update my current branch? I have three branches: master, production and…
Debajit
  • 46,327
  • 33
  • 91
  • 100
1519
votes
24 answers

Can I recover a branch after its deletion in Git?

If I run git branch -d XYZ, is there a way to recover the branch? Is there a way to go back as if I didn't run the delete branch command?
prosseek
  • 182,215
  • 215
  • 566
  • 871
1475
votes
20 answers

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

I have a local branch master that points to a remote branch origin/regacy (oops, typo!). How do I rename the remote branch to origin/legacy or origin/master? I tried: git remote rename regacy legacy But this gave an error: error : Could not…
JayD
  • 15,483
  • 5
  • 15
  • 14
1352
votes
11 answers

Git merge hotfix branch into feature branch

Let’s say we have the following situation in Git: A created repository: mkdir GitTest2 cd GitTest2 git init Some modifications in the master take place and get committed: echo "On Master" > file git commit -a -m "Initial commit" Feature1 branched…
theomega
  • 31,591
  • 21
  • 89
  • 127
1321
votes
8 answers

How do I copy a version of a single file from one Git branch to another?

I've got two branches that are fully merged together. However, after the merge is done, I realise that one file has been messed up by the merge (someone else did an auto-format, gah), and it would just be easier to change to the new version in the…
madlep
  • 47,370
  • 7
  • 42
  • 53
1291
votes
39 answers

How to prune local tracking branches that do not exist on remote anymore?

With git remote prune origin I can remove the local branches that are not on the remote any more. But I also want to remove local branches that were created from those remote branches (a check if they are unmerged would be nice). How can I do this?
Alex
  • 32,506
  • 16
  • 106
  • 171
1192
votes
6 answers

Create Git branch with current changes

I started working on my master branch thinking that my task would be easy. After a while I realized it would take more work and I want to do all this work in a new branch. How can I create a new branch and take all these changes with me without…
willcodejavaforfood
  • 43,223
  • 17
  • 81
  • 111
1145
votes
6 answers

Git: Find the most recent common ancestor of two branches

How to find the most recent common ancestor of two Git branches?
Ted
  • 14,465
  • 6
  • 28
  • 28