git checkout -t origin/5.0

* df957e8 (HEAD, origin/5.0, 5.0) commit A
* 93f3185 commit B
* abd1176 commit C

I checked out a change from gerrit using below command

git fetch gerrit refs/changes/36/236/1 && git checkout FETCH_HEAD

Now its going to detached HEAD state with incoming commits on top of master branch instead of 5.0

    * bdd7f9c (HEAD) part 3 of 236
    * fa8f60f part 2 of 236
    * bddd168 part 1 of 236
    * ffc7982 (origin/master, origin/HEAD) commit master
    * 415668e 
    * 991d48d 

I want it to be like

* bdd7f9c (HEAD) part 3 of 236
* fa8f60f part 2 of 236
* bddd168 part 1 of 236
* df957e8 (HEAD, origin/5.0, 5.0) commit A
* 93f3185 commit B
* abd1176 commit C

I tried changing HEAD pointing to refs/heads/5.0 using symbolic-ref but immediately after checking out FETCH_HEAD it is showing on top of master branch instead of 5.0.

My aim is to get list of commits came from gerrit change checkout using command

"git log --format="%H" origin/5.0..HEAD"

but its not working in this case as it is checking out on top of master instead of 5.0

Please let me know if I am missing anything

有帮助吗?

解决方案

There are a couple of things going on here. Firstly, in your desired result, you cannot have two HEADs. There is always only one HEAD.

You get a detached head state when your current head commit is not referenced by any branch name or tag.

Now, regarding why commits are getting placed on top of master instead of 5.0. Each commit references a parent commit. i.e. every single commit has a parent which is referenced by its hash. When you fetch changes, the fetched commits also come with their parent references and are placed accordingly. For ex, suppose the following is the state of your remote repo:

A -> B -> C (C being the latest commit)

And you have the following history in your local:

A -> B

Now no matter what branch you pull or fetch from, when commit C gets fetched, it will look for commit B set camp there.

Meaning, it doesn't matter where you call fetch from. A particular commit will reside over same parent on which it was made in the first place.

If you still need to move those 3 commits, I suggest you look into rebase. Although rebasing public commits will most certainly bite you unless you are the sole contributor to repo.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top