Reputation: 371
I recently pushed a commit to my git remote repository, and now I'm going to commit another change.
Is there a way I can merge these commits into a single commit?
Upvotes: 3
Views: 6702
Reputation: 439
Are you working from the command line?
1).
If you have just made commit A
to branch my-branch
and pushed it up, and you do some work, you can use:
git add -A
followed by git commit --amend
which will add your changes to that commit you have already pushed. Then use git push -f origin my-branch
2).
If you have pushed commit A
and then already committed B
to branch my-branch
then you can do the following:
git rebase --soft HEAD~
which will undo commit B
and then use git commit --amend
to add the changes into commit A
and then as above use git push -f origin my-branch
Upvotes: 0
Reputation: 6476
You need to execute the rebase
command
$ git init
$ touch readme
$ git add readme
$ git commit -m "Initial Commit"
[master (root-commit) 6dde006] Initial Commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 readme
$ touch a
$ touch b
$ git add a
$ git commit -m "First commit"
[master 74296cb] First commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a
$ git add b
$ git commit -m "Second commit" --squash=HEAD
[master 9ba46ab] squash! First commit
1 file changed, 0 insertions(+), 0 deletions(-)
create mode 100644 b
$ git rebase HEAD~2 -i
Simple save and close the editor of rebasing, then git will ask you for a new message for the merged commit, you can edit it, if you want.
[detached HEAD f855a0f] First commit
2 files changed, 0 insertions(+), 0 deletions(-)
create mode 100644 a
create mode 100644 b
Successfully rebased and updated refs/heads/master.
$ git log --oneline
cf855a0f First commitinsertions(+), 0 deletions(-)
6dde006 Initial Commit
If you pushed the first commit you can update the remote branch with the force
option, but be carefull, if someone has already pulled your changes they will have tons of problems when you forcible push your changes:
git push origin master --force
Upvotes: 0
Reputation: 10618
You can do this using interactive rebasing.
Run the following:
git rebase -i HEAD~2
You'll see something like this, in the editor that opens:
pick 3e7ee36 Older commit message
pick fa20af3 Newer commit message
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
# ...
fixup
or squash
the newer commit, i.e., replace the pick
in the newer commit to fixup
and save and quit the file. This will merge the 2 commits keeping the older message:
pick 3e7ee36 Older commit message
fixup fa20af3 Newer commit message
# Rebase 8db7e8b..fa20af3 onto 8db7e8b
# ...
Upvotes: 2