How can I combine two commits into one commit?
Solution 1
You want to git rebase -i
to perform an interactive rebase.
If you're currently on your "commit 1", and the commit you want to merge, "commit 2", is the previous commit, you can run git rebase -i HEAD~2
, which will spawn an editor listing all the commits the rebase will traverse. You should see two lines starting with "pick". To proceed with squashing, change the first word of the second line from "pick" to "squash". Then save your file, and quit. Git will squash your first commit into your second last commit.
Note that this process rewrites the history of your branch. If you are pushing your code somewhere, you'll have to git push -f
and anybody sharing your code will have to jump through some hoops to pull your changes.
Note that if the two commits in question aren't the last two commits on the branch, the process will be slightly different.
Solution 2
Lazy simple version for forgetfuls like me:
git rebase -i HEAD~3
or however many commits instead of 3.
Turn this
pick YourCommitMessageWhatever
pick YouGetThePoint
pick IdkManItsACommitMessage
into this
pick YourCommitMessageWhatever
s YouGetThePoint
s IdkManItsACommitMessage
and do some action where you hit esc
then enter
to save the changes. [1]
When the next screen comes up, get rid of those garbage # lines [2] and create a new commit message or something, and do the same escape
enter
action. [1]
Wowee, you have fewer commits. Or you just broke everything.
[1] - or whatever works with your git configuration. This is just a sequence that's efficient given my setup.
[2] - you'll see some stuff like # this is your n'th commit
a few times, with your original commits right below these message. You want to remove these lines, and create a commit message to reflect the intentions of the n commits that you're combining into 1.
Solution 3
- Checkout your branch and count quantity of all your commits.
- Open git bash and write:
git rebase -i HEAD~<quantity of your commits>
(i.e.git rebase -i HEAD~5
) - In opened
txt
file changepick
keyword tosquash
for all commits, except first commit (which is on the top). For top one change it toreword
(which means you will provide a new comment for this commit in the next step) and click SAVE! If in vim, pressesc
then save by enteringwq!
and press enter. - Provide Comment.
- Open Git and make "Fetch all" to see new changes.
Done
Solution 4
This is what I do. It's easier(for me) than doing an interactive rebase:
git reset HEAD~2. // go back 2 commits.
git commit -am “My squashed single commit”
git push --force
Comments
-
Don P about 2 years
I have a branch 'firstproject' with 2 commits. I want to get rid of these commits and make them appear as a single commit.
The command
git merge --squash
sounds promising, but when I rungit merge --squash
my terminal just brings up options for the command. What is the correct command?Commit 1: Added 'homepage.html' Added 'contacts.html' Commit 2: Added 'homepage.php' Added 'homepage.php' Deleted 'homepage.html' Deleted 'contacts.html'
-
Dr.jacky over 6 yearsHow to do
Then write your file, and quit
? I'm using Android Studio Terminal, git console itself Or even opened git console by SourceTree software. But how to save and quit? -
Weishi Z over 6 yearsHow to combine 2 commits that aren't the last two commits on the branch?
-
Igor Mironenko almost 6 yearsDoes it make any difference if I want to squash the 2nd into the 1st instead of the 1st into the 2nd as you describe?
-
user229044 almost 6 years@PandaWood that doesn't really make sense. A squashed into B is the same thing as B squashed into A.
(1 + 2) = (2 + 1)
. -
Igor Mironenko almost 6 years@meagar I think it does make sense... if you check this issue (a duplicate of this), squashing 'direction' made a big difference and was the root of the entire issue (see the first comment with around 200 upvotes about 'squashing the wrong way') - stackoverflow.com/a/2568581/43453
-
user229044 almost 6 years@PandaWood You're misreading the answer. The first way was simple wrong, not different. You can only squash backwards, it makes no sense to think of squashing an older commit into a newer one, but if you could it would be the same operation.
-
Andrew Torr about 5 yearsI understood up to " change the first word of the second line" - second line of what?
-
UrbanConor over 4 yearsVery concise solution - thank you. Might be handy to outline that
s
used here stands for squash. To use the commit, but meld into the previous commit -
scrutari over 3 yearsIf you have just
2
commits and want to combine the same2
commit, you will need to rungit reset --soft HEAD~
andgit commit --amend
. -
Abhay Koradiya over 3 years@Stachu When I command push after this, It will ask for pull and merge. What I can do to avoid that?
-
Stachu over 3 yearsWhy wouldn't you want to pull and merge?
-
PlsWork over 2 yearsWhat do you mean by "anybody sharing your code will have to jump through some hoops to pull your changes"?
-
Tal Kohavy almost 2 yearsThis should be the accepted answer!! or at least get more votes! All other solutions are so complex, and require mass typing frenzy, that a GUI software is better off used (such as sourcetree or tortoise) where you could get the same result only with a few mouse clicks. Your solution, however, is the cleanest, fastest, and great for solo projects, or team projects where you're on a side branch of your own.