- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Explain squash merging in Git
Imagine if your feature branch has large number of commits- E.g. 100s commits. Rather than merging all commits individually from feature to master, there is an option to add up all commits into a single commit. This is called a squash commit because it "squashes" all the individual commits into one big change. As far as the history of the master branch is concerned, the history of the feature branch would be lost.
We can use the following command to squash-merge the commits of a branch to the master branch.
$ git merge --squash feature_branch
The diagram shows that we have 3 commits in the master branch. The feature branch we created has two commits F1 and F2. Now we need to combine all commits from the feature branch and the master branch, as if there was only one commit in master branch.
After squash merging, the master branch will look as shown below. Once squash merge is successful, we can remove the feature branch as it is local to the repository.
Example
$ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo $ git init Initialized empty Git repository in E:/tut_repo/.git/ $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git add . $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'c1' [master (root-commit) 2f992c9] c1 1 file changed, 1 insertion(+) create mode 100644 hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello again>>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -am 'c2' [master 2e550c5] c2 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git branch feature $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git switch feature Switched to branch 'feature' $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ echo world>world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git add world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git commit -m 'F1' [feature bf61466] F1 1 file changed, 1 insertion(+) create mode 100644 world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ echo world again>>world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git commit -am 'F2' [feature 08061f5] F2 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (feature) $ git switch master Switched to branch 'master' $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ echo hello how are your>>hello.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -am 'C3' [master 91cc8c2] C3 1 file changed, 1 insertion(+) $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * 91cc8c2 (HEAD -> master) C3 | * 08061f5 (feature) F2 | * bf61466 F1 |/ * 2e550c5 c2 * 2f992c9 c1 $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git merge --squash feature Automatic merge went well; stopped before committing as requested Squash commit -- not updating HEAD $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git status -s A world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git commit -m 'squashing F1 and F2' [master ad342f3] squashing F1 and F2 1 file changed, 2 insertions(+) create mode 100644 world.txt $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * ad342f3 (HEAD -> master) squashing F1 and F2 * 91cc8c2 C3 | * 08061f5 (feature) F2 | * bf61466 F1 |/ * 2e550c5 c2 * 2f992c9 c1 $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ cat world.txt world world again $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git branch -D feature Deleted branch feature (was 08061f5). $ dell@DESKTOP-N961NR5 MINGW64 /e/tut_repo (master) $ git log --oneline --all --graph * ad342f3 (HEAD -> master) squashing F1 and F2 * 91cc8c2 C3 * 2e550c5 c2 * 2f992c9 c1
- Related Articles
- Explain rebasing in Git
- Explain cherry picking in Git
- Explain Git collaboration workflow
- Explain how reset command works in Git
- Explain BLOB object and tree object in Git.
- Explain hard reset with an example in Git
- Explain mixed reset with an example in Git
- Explain soft reset with an example in Git
- Merging Arrays in Perl
- Merging subarrays in JavaScript
- Difference between Git Fetch and Git Pull
- Merging and rectifying arrays in JavaScript
- Adaptive Merging and Sorting in Data Structure
- Alternatively merging two arrays - JavaScript
- Merging sorted arrays together JavaScript
