This study will illustrate the method to combine multiple commits in Git.
How to Merge/Combine Multiple Commits Before Pushing in Git?
To merge or combine multiple commits before pushing in Git, try out the below-mentioned steps:
- Navigate to the local repository.
- View commits history.
- Perform rebase operation.
- Combine commits by squashing them in the default editor.
- Ensure changes.
Step 1: Redirect to Local Directory
First, enter the below-listed command and switch to the desired local repository:
Step 2: Check Git Log
Then, display the commit history of the current repository and choose the desired commits that need to be combined:
The below output displays the commit history. Now, we want to combine the first five commits:
Step 3: Combine Commits
Next, perform the squash operation on the selected commits by writing out the provided command:
Here, the “-i” flag is utilized for an interactive mode that permits editing on desired commits, and the “HEAD~5” option is specified to edit the first five commits.
After executing the above-listed command, a file will open up:
Now, replace the “pick” keyword with the “squash” with all the commits that you want to combine and close the editor by saving modification via the “CTRL + S” keys:
After that, another screen will be opened where you have to add the desired commit message:
Upon doing so, the commits will be combined:
Step 4: Verify Changes
Lastly, check the Git log to ensure that the commits have been combined or not:
It can be observed that selected multiple commits have been combined into one commit successfully:
That was all about combining multiple commits before pushing them to the remote repository.
Conclusion
To combine or merge multiple commits before pushing them to the remote repository, first, switch to the local repository and view its commit history. Then, choose the desired commits that need to be combined. After that, execute the “git rebase -i HEAD~5” command and combine commits by changing the default editor. Lastly, ensure changes by viewing the commit history. This study illustrated the method of combining multiple commits in Git.