Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make git switch to newly created branch automatically?

Tags:

git

git-branch

1: git checkout master
2: git branch feature
3: git commit 'commit msg'

I am expecting git to switch branch on line 2 (but it doesn't which is really annoying because I have to revert the check-in on master).

Is there any way to make git switch the branch?

like image 211
kev Avatar asked Nov 01 '16 06:11

kev


People also ask

How do I force switch to another branch?

You can pass the -f or --force option with the git checkout command to force Git to switch branches, even if you have un-staged changes (in other words, the index of the working tree differs from HEAD ). Basically, it can be used to throw away local changes.

How will you create a new branch and switch directly?

New Branches The git branch command can be used to create a new branch. When you want to start a new feature, you create a new branch off main using git branch new_branch . Once created you can then use git checkout new_branch to switch to that branch.

How do I push a newly created local branch to remote?

Push a new Git branch to a remote repo Clone the remote Git repo locally. Create a new branch with the branch, switch or checkout commands. Perform a git push with the –set-upstream option to set the remote repo for the new branch. Continue to perform Git commits locally on the new branch.


3 Answers

git checkout -b yourBranchName will do it for you

like image 74
Pragun Avatar answered Nov 15 '22 07:11

Pragun


You can use git checkout -b, which will checkout the newly created branch, i.e.

git checkout master
git checkout -b feature
# work work work
git commit 'commit msg'

The commit will go into the new branch feature which was created from master.

like image 25
Tim Biegeleisen Avatar answered Nov 15 '22 05:11

Tim Biegeleisen


As you noted git branch just creates a new branch but does not switch over to it. Instead, you can use git checkout -b feature which both creates the new branch and switches over to it.

like image 32
Mureinik Avatar answered Nov 15 '22 05:11

Mureinik