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?
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.
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.
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.
git checkout -b yourBranchName
will do it for you
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
.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With