In git, how can I checkout out the HEAD version of my remote/tracking branch? Basically, I want to do a 'svn checkout ' in git.
I think the closest thing I find is 'git fetch', but from the man page, I don't know how to checkout 1 particular file using that?
Therefore simple " git checkout origin " (assuming that remote is called origin ), which is shortcut for " git checkout origin/HEAD ", which is usually " git checkout origin/master " would checkout a state of remote-tracking branch into unnamed branch, so called detached HEAD.
To be precise, renaming a remote branch is not direct – you have to delete the old remote branch name and then push a new branch name to the repo. Step 2: Reset the upstream branch to the name of your new local branch by running git push origin -u new-branch-name .
You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".
Sometimes you may suffer from detached HEAD
problems:
$ git checkout origin/master
Note: checking out 'origin/master'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits
you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you
create, you may
do so (now or later) by using -b with the checkout command again. Example:
git checkout -b <new-branch-name>
HEAD is now at c3ff60a rename
Then list all branches, you will see:
$ git branch -a
* (HEAD detached at origin/master)
master
remotes/origin/HEAD -> origin/master
remotes/origin/master
If you want to checkout the branch which referenced by remotes/origin/HEAD, that is remotes/origin/HEAD -> origin/master
, you can do:
HEAD_BRANCH="$(git branch -r | grep 'HEAD')"
HEAD_BRANCH="${HEAD_BRANCH#*origin/HEAD -> *origin/}"
git checkout "$HEAD_BRANCH"
So that you can checkout "HEAD" version of remote/tracking branch
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