How to modify existing, unpushed commits?

2018-12-31 01:59发布

I wrote the wrong thing in a commit message. Alternatively, I've forgotten to include some files.

How can I change the commit message/files? The commit has not been pushed yet.

27条回答
其实,你不懂
2楼-- · 2018-12-31 01:59

I use the Git GUI as much as I can, and that gives you the option to amend the last commit:

Tick that box

Also, git rebase -i origin/masteris a nice mantra that will always present you with the commits you have done on top of master, and give you the option to amend, delete, reorder or squash. No need to get hold of that hash first.

查看更多
初与友歌
3楼-- · 2018-12-31 02:00

Amend

You have a couple of options here. You can do

git commit --amend

as long as it's your last commit.

Interactive rebase

Otherwise if it's not your last commit you can do an interactive rebase,

git rebase -i [branched_from] [hash before commit]

Then inside the interactive rebase you simply add edit to that commit. When it comes up do a git commit --amend and modify the commit message. If you want to roll back before that commit point you could also use git reflog and just delete that commit. Then you just do a git commit again.

查看更多
路过你的时光
4楼-- · 2018-12-31 02:00

I like to use the following:

  1. git status
  2. git add --all
  3. git commit -am "message goes here about the change"
  4. git pull <origin master>
  5. git push <origin master>
查看更多
千与千寻千般痛.
5楼-- · 2018-12-31 02:03

As already mentioned, git commit --amend is the way to overwrite the last commit. One note: if you would like to also overwrite the files, the command would be

git commit -a --amend -m "My new commit message"
查看更多
零度萤火
6楼-- · 2018-12-31 02:03

Update your last wrong commit message with new commit message in one line:

git commit --amend -m "your new commit message"

Or, try git reset like below:

# You can reset your head to n number of commit
# NOT a good idea for changing last commit message
# but you can get an idea to split commit into multiple commits
git reset --soft HEAD^

# it will reset you last commit. Now, you
# can re-commit it with new commit message.

Using reset to split commits into smaller commits

git reset can help you to break one commit into multiple commits too:

# reset your head. I am resetting to last commits:
git reset --soft HEAD^
# (you can reset multiple commit by doing HEAD~2(no. of commits)

# Now, reset your head for splitting it to multiple commits
git reset HEAD

# add and commit your files seperately to make multiple commits: e.g
git add app/
git commit -m "add all files in app directory"

git add config/
git commit -m "add all files in config directory"

Here you have successfully broke your last commit into two commits.

查看更多
听够珍惜
7楼-- · 2018-12-31 02:04

Amending the most recent commit message

git commit --amend

will open your editor, allowing you to change the commit message of the most recent commit. Additionally, you can set the commit message directly in the command line with:

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

…however, this can make multi-line commit messages or small corrections more cumbersome to enter.

Make sure you don't have any working copy changes staged before doing this or they will get committed too. (Unstaged changes will not get committed.)

Changing the message of a commit that you've already pushed to your remote branch

If you've already pushed your commit up to your remote branch, then you'll need to force push the commit with:

git push <remote> <branch> --force
# Or
git push <remote> <branch> -f

Warning: force-pushing will overwrite the remote branch with the state of your local one. If there are commits on the remote branch that you don't have in your local branch, you will lose those commits.

Warning: be cautious about amending commits that you have already shared with other people. Amending commits essentially rewrites them to have different SHA IDs, which poses a problem if other people have copies of the old commit that you've rewritten. Anyone who has a copy of the old commit will need to synchronize their work with your newly re-written commit, which can sometimes be difficult, so make sure you coordinate with others when attempting to rewrite shared commit history, or just avoid rewriting shared commits altogether.


Use interactive rebase

Another option is to use interactive rebase.
This allows you to edit any message you want to update even if it's not the latest message.

In order to do a git squash, follow these steps:

// X is the number of commits to the last commit you want to be able to edit
git rebase -i HEAD~X

Once you squash your commits - choose the e/r for editing the message

enter image description here

Important note about Interactive rebase

When you use the git rebase -i HEAD~X there can be more than X commits. Git will "collect" all the commits in the last X commits and if there was a merge somewhere in between that range you will see all the commits as well so the outcome will be X+.

Good tip:

If you have to do it for more than a single branch and you might face conflicts when amending the content, set up git rerere and let git resolve those conflicts automatically for you.


查看更多
登录 后发表回答