Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I checkout out the HEAD version of my remote/tracking branch

Tags:

git

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?

like image 242
hap497 Avatar asked Sep 30 '09 23:09

hap497


People also ask

How do I checkout to the origin head?

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.

How do I checkout a remote branch with a different name?

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 .

Which command can be used to track a remote branch?

You can tell Git to track the newly created remote branch simply by using the -u flag with "git push".


1 Answers

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

like image 122
allenyllee Avatar answered Nov 01 '22 13:11

allenyllee