How to apply update code to previous commit in Git?

For don't have to create new commit, you must apply the new code to previous commit, and push force it if you have already pushed it to your remote branch.

Fortunately in Git have a command help you do that easily, follow the tips bellow:

Step 1: in the window command line, navigate to the repository that contains the commit you want to update with new code.
Use $ git add . to add all file changes to stage, or $ git add file_name for add each file

Step 2: update old commit with new message:

 $ git commit --amend -m "New commit message"

Or to keep previous commit message with this command:

 $ git commit --amend --no-edit

Step 3: If you have already pushed the commit to your remote branch, you will have to force push the commit with an amended message use the command:

 $ git push --force your-branch-name

 $ git push -f your-branch-name

See all command again:

 # add file changes to stage
 $ git add . 

 # override old commit with amend
 $ git commit --amend -m "New commit message"
 # or
 $ git commit --amend --no-edit

 # push force the commit to you remote branch
 $ git push -f your_branch 

© 2019 4codev

Created with love by Sil.