craken
craken

Reputation: 1411

How do I push amended commit to the remote git repo on a specific commit

I already edit some files in my repo and push them.
After some other work, I noticed that i keep a line commented.
So, i don't want to create a new commit for it and I want to change it in the previous commit (Not the last one commit).
I think that i can use the commit ID, but i don't know how or maybe Git not tolerating this feature.

Upvotes: 3

Views: 6278

Answers (2)

eckes
eckes

Reputation: 67067

Don't!

As Tim points out: it's dangerous to rewrite remote history.

Simply accept that you made a mistake (and didn't test your code thoroughly before pushing it), fix your mistake and make a new commit with the message

amend <COMMIT_HASH_YOU_WANT_TO_FIX>

If you're using a half-decent history viewer, it will generate a link out of <COMMIT_HASH_YOU_WANT_TO_FIX> so you could easily jump to the errornous commit.

Upvotes: 4

Tim Biegeleisen
Tim Biegeleisen

Reputation: 521499

You can do a rebase in interactive mode:

git rebase -i HEAD~10

Git will prompt you with a list of commits, with the oldest ones showing up first:

...
pick ab89k22 Old commit
pick ml9826x Another old commit
pick xke82ml This is the commit you want to edit
pick aow82md Here is your latest commit

Find the commit you want to edit, and change pick to edit:

edit xke82ml This is the commit you want to edit

Then save the file and exit. Git will now begin the rebase and will stop at the commit you want to edit, allowing you to make changes. Once you have finished making those changes, you can type

git add path/to/file

for each file you modiifed, and then type

git rebase --continue

to finish the rebase.

Note that in editing this commit on your local branch, you have effectively rewritten history. This means that when you push to the remote, you will have to force the branch out, using --force in your git push command:

git push origin master --force

Be wary that rewriting the remote history can be dangerous depending on who is also sharing this branch with you.

If you don't want to rewrite the remote history, then a safer bet would be to add a new commit to your branch. You could also remove the commit in question using git revert, and then add the changes you want to keep.

Upvotes: 6

Related Questions