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
1841
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
  • 20,027
  • 4
  • 24
  • 50
1776
votes
32 answers

How to fetch all Git branches

I cloned a Git repository, which contains about five branches. However, when I do git branch I only see one of them: $ git branch * master I know that I can do git branch -a to see all the branches, but how would I pull all the branches locally so…
David542
  • 96,524
  • 132
  • 375
  • 637
1610
votes
22 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
  • 71,442
  • 73
  • 210
  • 338
1470
votes
37 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
  • 17,483
  • 5
  • 27
  • 40
1421
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
  • 43,330
  • 32
  • 87
  • 97
1261
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
  • 29,902
  • 21
  • 83
  • 125
1220
votes
10 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
  • 12,265
  • 3
  • 13
  • 4
1209
votes
21 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
  • 155,475
  • 189
  • 518
  • 818
1071
votes
7 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
  • 41,172
  • 7
  • 39
  • 53
995
votes
10 answers

Git: How do I list only local branches?

git branch -a shows both remote and local branches. git branch -r shows remote branches. Is there a way to list just the local branches?
munyengm
  • 13,831
  • 4
  • 21
  • 33
975
votes
5 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
  • 39,821
  • 17
  • 77
  • 107
942
votes
4 answers

Git: Find the most recent common ancestor of two branches

How to find the most recent common ancestor of two Git branches?
Ted
  • 12,125
  • 5
  • 26
  • 28
850
votes
16 answers

Rename master branch for both local and remote Git repositories

I have the branch master which tracks the remote branch origin/master. I want to rename them to master-old both locally and on the remote. Is this possible? For other users who tracked origin/master (and who always updated their local master branch…
Albert
  • 57,395
  • 54
  • 209
  • 347
841
votes
10 answers

Move branch pointer to different commit without checkout

To move the branch pointer of a checked out branch, one can use the git reset --hard command. But how to move the branch pointer of a not-checked out branch to point at a different commit (keeping all other stuff like tracked remote branch)?
Mot
  • 24,166
  • 22
  • 78
  • 117
795
votes
10 answers

Update Git branches from master

I'm new to Git, and now I'm in this situation: I have four branches (master, b1, b2, and b3). After I worked on b1-b3, I realized I have something to change on branch master that should be in all other branches. I changed what I needed in master…
Ionuț Staicu
  • 18,620
  • 11
  • 47
  • 58