0

I want to keep same commit with another change like.

Few hours ago, i commuted a change and push to my origin like.

git commit -m "header removed"

and later i push it to remote origin like:

git push origin mybranhname

After pushing i realized i need to change something again and i changing some code again.

Now i want to push the change in the same commit, is it possible?

I mean, i dont want to commit again with the new named commit. I want to commit with the header removed

I dont want to open new commit. can i do it?

  • you can see this: [Link](https://stackoverflow.com/questions/8981194/changing-git-commit-message-after-push-given-that-no-one-pulled-from-remote) – user404 Jun 13 '19 at 11:52

1 Answers1

0

If the branch is not shared (either you're working alone on the whole repo or alone on this branch), just amend and force-push :

# do your changes in the file, then
git add path/to/changed/file
git commit --amend
git push --force origin HEAD

But you should not do this if the branch is shared. In that case, you'll have to keep the new changes as a new commit and push it on top of the branch as usual.

Romain Valeri
  • 19,645
  • 3
  • 36
  • 61
  • Git never knows whether someone pulled the faulty ref. That information must be determined by other means, `--force-with-lease` does not do it. It helps only when there are concurrent *writers* to the ref in question. – j6t Jun 13 '19 at 12:44
  • @j6t Ouch... thanks a lot for your comment but it means I was in dangerous teritory for a while on this one. I'll have to look further into it. Edited. – Romain Valeri Jun 13 '19 at 12:55
  • @j6t Hmmm... I went back to reread the whole thing in the [doc](https://git-scm.com/docs/git-push#Documentation/git-push.txt---force-with-leaseltrefnamegtltexpectgt).... and I don't see there what you described. Rather a confirmation of what I thought of it so far. Could you share the source where you got the info? And git doesn't need to know *who* pulled the faulty ref. Either it is the same or it changed. That's the only criterion here. – Romain Valeri Jun 13 '19 at 13:01
  • `--force-with-lease` helps only if someone pulled *and then pushed back* their work to that ref in that repository. But it does not help if someone only pulled and did something to the branch without telling you. The omnipresent warning "do that only if you are sure that nobody depends on the faulty commit" addresses the latter case, that you can actually only know about if there is some form of communication going on. `--force-with-lease` helps only in the tiny, tiny subset of cases where the communication is via a ref in a repository. – j6t Jun 13 '19 at 14:04
  • @j6t OK that's also what I got, it's much clearer now, I must have misunderstood your first comment. – Romain Valeri Jun 13 '19 at 14:08