Making a Git push from a detached head

前端 未结 6 931
时光说笑
时光说笑 2020-11-29 20:05

I am on a detached head and made some changes. I want to push up these changed to this detached head with Git. I do not want my changes to go onto the develop branch and cer

相关标签:
6条回答
  • 2020-11-29 20:39

    If you are on a detached head and you want to push to your remote branch

    git push origin HEAD:name-of-your-branch
    

    otherwise you can create a new branch and push to it ( it will be created automatically )

    git branch new-branch-name
    git push -u origin new-branch-name
    
    0 讨论(0)
  • 2020-11-29 20:46

    While all the answers here sort of answer the original question (how to push from a detached head without affecting other branches) all suggest creating a new branch.

    Here's how to push to a new remote branch without creating a new local branch:

    git checkout --detach # (or anything else that leaves you with a detached HEAD - guillotine anyone?)
    [change stuff & commit]
    git push origin HEAD:refs/heads/my-new-branch
    

    Replace origin with the appropriate remote name (that you have write access to), and my-new-branch with whatever you want the new branch to be called.

    Your commit(s) on HEAD will be pushed to a new branch named my-new-branch.

    0 讨论(0)
  • 2020-11-29 20:47

    Create a new branch using git checkout -b BRANCH_NAME

    Then push the new branch to remote: git push origin BRANCH_NAME

    0 讨论(0)
  • 2020-11-29 20:59

    Create a new branch for that commit and checkout to it: git checkout -b <branch-name> <commit-hash>. Now you can push your changes to the new branch: git push origin <branch-name>

    In case you need to clean up your other branch from leftover commits be sure to run git reset --hard <branch-name>.

    Here is an article that explains how branching and detached head works.

    0 讨论(0)
  • 2020-11-29 21:03

    Note: making a branch before pushing is all the more recommended that git 2.11 or less used to segfault!

    This won't be the case with Git 2.12+ (Q1 2017)

    See commit b10731f (07 Jan 2017) by Kyle Meyer (kyleam).
    (Merged by Junio C Hamano -- gitster -- in commit b85f79c, 18 Jan 2017)

    branch_get_push: do not segfault when HEAD is detached

    "git <cmd> @{push}" on a detached HEAD used to segfault; it has been corrected to error out with a message.

    The error now will be:

    HEAD does not point to a branch
    

    With Git 2.12 or more, you can then push your detached HEAD to a remote branch, as shown in Matt's answer.

    0 讨论(0)
  • 2020-11-29 21:04

    git checkout

    git checkout <commit_id>
    git checkout -b <new branch> <commit_id>
    git checkout HEAD~X // x is the number of commits t go back
    

    This will checkout new branch pointing to the desired commit.
    This command will checkout to a given commit.
    At this point you can create a branch and start to work from this point on.

    # Checkout a given commit. 
    # Doing so will result in a `detached HEAD` which mean that the `HEAD`
    # is not pointing to the latest so you will need to checkout branch
    #in order to be able to update the code.
    git checkout <commit-id>
    
    # create a new branch forked to the given commit
    git checkout -b <branch name>
    
    0 讨论(0)
提交回复
热议问题