I run:
git checkout mygithub/master
but for some reason, running 'git status' shows "not currently on any branch". Running:
git checkout master
and then git status
, says that I'm now on branch master. Now I want to switch to another branch. Running git checkout anotherbranch
works, but git status
says I am still on branch 'master'. What am I doing wrong?
Git checkout works hand-in-hand with git branch . 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.
Force a Checkout 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.
You can have many branches in your repository, but only one of these will be "checked out" as the working-tree so that you can work on it and make changes. git worktree adds the concept of additional working trees. This means you can have two (or more) branches checked-out at once.
mygithub/master
is a remote branch. To create a local branch based off of that remote branch, you have to use git checkout -b mymaster mygithub/master
. Git tries to make this easy for you: if you write git checkout branchname
, and branchname only exists in a remote, but not locally, Git will automatically set up a local branch with <remote>/branchname
being its parent.
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