How do I push amended commit to the remote Git repository?

后端 未结 16 1381
深忆病人
深忆病人 2020-11-22 04:42

When I\'ve worked a bit with my source code, I did my usual thing commit and then I pushed to a remote repository. But then I noticed I forgot to organize my imports in the

相关标签:
16条回答
  • 2020-11-22 05:05

    You are getting this error because the Git remote already has these commit files. You have to force push the branch for this to work:

    git push -f origin branch_name
    

    Also make sure you pull the code from remote as someone else on your team might have pushed to the same branch.

    git pull origin branch_name
    

    This is one of the cases where we have to force push the commit to remote.

    0 讨论(0)
  • 2020-11-22 05:05

    Here, How I fixed an edit in a previous commit:

    1. Save your work so far.

    2. Stash your changes away for now if made: git stash Now your working copy is clean at the state of your last commit.

    3. Make the edits and fixes.

    4. Commit the changes in "amend" mode: git commit --all --amend

    5. Your editor will come up asking for a log message (by default, the old log message). Save and quit the editor when you're happy with it.

      The new changes are added on to the old commit. See for yourself with git log and git diff HEAD^

    6. Re-apply your stashed changes, if made: git stash apply

    0 讨论(0)
  • 2020-11-22 05:10

    I actually once pushed with --force and .git repository and got scolded by Linus BIG TIME. In general this will create a lot of problems for other people. A simple answer is "Don't do it".

    I see others gave the recipe for doing so anyway, so I won't repeat them here. But here is a tip to recover from the situation after you have pushed out the amended commit with --force (or +master).

    1. Use git reflog to find the old commit that you amended (call it old, and we'll call the new commit you created by amending new).
    2. Create a merge between old and new, recording the tree of new, like git checkout new && git merge -s ours old.
    3. Merge that to your master with git merge master
    4. Update your master with the result with git push . HEAD:master
    5. Push the result out.

    Then people who were unfortunate enough to have based their work on the commit you obliterated by amending and forcing a push will see the resulting merge will see that you favor new over old. Their later merges will not see the conflicts between old and new that resulted from your amending, so they do not have to suffer.

    0 讨论(0)
  • 2020-11-22 05:10

    Here is a very simple and clean way to push your changes after you have already made a commit --amend:

    git reset --soft HEAD^
    git stash
    git push -f origin master
    git stash pop
    git commit -a
    git push origin master
    

    Which does the following:

    • Reset branch head to parent commit.
    • Stash this last commit.
    • Force push to remote. The remote now doesn't have the last commit.
    • Pop your stash.
    • Commit cleanly.
    • Push to remote.

    Remember to change "origin" and "master" if applying this to a different branch or remote.

    0 讨论(0)
提交回复
热议问题