1

I had already pushed some changes to a remote. Now I need to push some other changes which should be in that previously pushed commit but somehow are not. I can do this simply by pushing the changes with a new commit but just now I have found the --no-edit flag and --amend

If I need to push the new changes but I need a single commit having the old and new changes is git commit --amend --no-edit going to achieve that?

AncientSwordRage
  • 7,086
  • 19
  • 90
  • 173
Piyush Chaudhary
  • 183
  • 2
  • 12
  • What happened when you used it? – evolutionxbox Aug 03 '21 at 10:12
  • 3
    Short answer is yes, but make sure rewriting history is okay in your context, and don't forget to add your new changes before amending. – Romain Valeri Aug 03 '21 at 10:12
  • 2
    Not an answer, but: when you have pushed something already, consider just pushing a new commit, unless *you are sure nobody else hasn't checked out the same branch*. – hyde Aug 03 '21 at 10:13
  • 2
    Also, just leave the `--no-edit` out until you are very comfortable with `--amend`. Seeing the commit message in editor is one extra chance for you to verify you are amending the right commit. – hyde Aug 03 '21 at 10:15
  • @RomainValeri Thank you so much for your tip to don't forget to add the new changes before amending. This is exactly what I am missing. – Piyush Chaudhary Aug 03 '21 at 10:29
  • @hyde Thank you for all tips. Yes, I am sure nobody else hasn't checked out the same branch. – Piyush Chaudhary Aug 03 '21 at 10:31

2 Answers2

6

Yes, the git commit --amend --no-edit is the thing that I am looking for.

  1. git add . (Add the added and modified files)
  2. git commit --amend --no-edit
  3. git push --force-with-lease <remote> <branch>
Piyush Chaudhary
  • 183
  • 2
  • 12
3

Generally, it's best to avoid rewriting the history, and just add a new commit as hyde suggested.

If you really need to overwrite the history, make sure to use --force-with-lease when you push.