Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I combine 2 'git commit's

Tags:

git

I have made 2 'git commit' locally. But I have not pushed, is it possible for me to edit my git and combine those 2 git commit into 1?

like image 575
michael Avatar asked Mar 28 '11 22:03

michael


People also ask

How do I merge two branches?

To merge branches locally, use git checkout to switch to the branch you want to merge into. This branch is typically the main branch. Next, use git merge and specify the name of the other branch to bring into this branch. This example merges the jeff/feature1 branch into the main branch.

How do you merge squash?

You can choose to squash merge when completing a pull request in Azure Repos. Choose Squash commit under Merge type in the Complete pull request dialog to squash merge the topic branch.


1 Answers

Yes it is possible:

You have to first find the commit that is before the ones you want to work with. Imagine we want to combine last two commits:

git rebase -i HEAD~3

This will fire up an editor for interactive rebase that is giving you a possibility to perform so called squashing of the commits and it will list commits starting from one that is three commits ago.

PLEASE, read the help message in the fired up editor window to understand how to use interactive rebase. In order to cancel rebase operation you have to delete all lines from the file and save it ! You also don't touch the commit ID and description - you just change the command before the commit ID from pick to "s" (squash)

Another thing is that you absolutely correctly mentioned that you didn't push yet. First and only one rule of git is "Do not rewrite published history". I.e. you don't rebase, reorder or delete commits that were already pushed for public access. This is true for all situation except when you're the only one who works on the project.

like image 191
Eugene Sajine Avatar answered Sep 27 '22 18:09

Eugene Sajine