Adding more changes to your last commit

 

From https://medium.com/@igor_marques/git-basics-adding-more-changes-to-your-last-commit-1629344cb9a8

Git Basics: Adding more changes to your last commit

 

With this post you'll learn how to modify your last commit, adding (or removing) some changes. You can also follow this tutorial to learn how to edit your last commit message.

I assume you are working on a branch alone to avoid messing around with your teammates’ work. Remember: shared branches are usually very hard to maintain.

 

So you have a commit history like this one (older commits on top):

But you forgot to add just a single line on the changelog file you mentioned on the commit a5f4a0d :(

You could simply commit it and have a history like this:

But that’s just not good for your history (it’s just a change on a change you already did). The best case scenario here is to have both changes in a single commit.

And you can achieve that with the simply amazing amend command!

Just remember: do not commit any of the changes you want to add to your last commit before doing these steps!

The Basic of the Amend Command

Just add the modified file(s):

And amend it:

After executing the command, this file will show up (probably on vi/vim. check this cheatsheet if you don't know how to use it):

Here you can edit your commit message. Do as you wish and then save the file. If you don’t want to change the message, just save the file without changing it.

Now the changes you did on the last commit and after it will be in the same commit!

Amending a Commit Without Changing Its Message

If you don’t want to change your commit message, you can run the amend command with the no-edit flag, like this:

You’ll not be "redirected" a file to edit the commit message and that’s it!

Pushing an Amended Commit

If you haven’t pushed the last commit yet to your remote, a single push is enough. Otherwise, you’ll have to push using the -f option since you’ve rewritten your commit history:

Be warned: pushing with -f is a very dangerous thing. Proceed with caution and always be sure that you’re pushing to the right branch.

Remember: NEVER rewrite the commit history of public branches (like master). This will truly mess your teammates work.

Bonus 1: Just Editing a Commit Message

To just edit a commit message (without adding new changes to your last commit), just run the amend command without adding changes. Simple as that!

Bonus 2: Editing a Commit Without Opening a File

Of course there would be a command for that! Just run:

 

And there you go!

If you wanna learn how to use Git with teams, you should check this other post ;)

Happy gitting!

 

 

 

 Below will add new change to the last commit without generating new commit.

git add

git commit -a -m "Add config.pbtxt for xnet"

git push origin xxx/add_config.txt

 

$ git commit --amend --no-edit

$ git push -f origin xxx/add_config.txt

posted on 2021-08-18 16:03  cdekelon  阅读(50)  评论(0)    收藏  举报

导航