Android App

TechLogic
1. 
Suppose you have multiple commits in your feature branch and want to create single commit with well descriptive commit message to maintain a clean git history. Then you can do Interactive Rebase which will re-apply all commits, one by one and in order. 

You checked your commits by git log and found 5 commits in your branch. Now, you can squash them in one commit and/or you can edit commit message if you want.


git rebase -i HEAD~5 here `~5` means number of commits we want to rebase.


After entering above command editor will open and will look like as below:

pick a0a0abc My first commit message pick b0a0abc Fix linting errors pick ca0a0abc Just added unit tests pick d0a0abc Removed css file
pick e0a0abc Resolve review comments # Rebase a0a0abc..f0a0abc onto a0a0abc # # Commands: # p, pick = use commit # r, reword = use commit, but edit the commit message # e, edit = use commit, but stop for amending # s, squash = use commit, but meld into previous commit # f, fixup = like "squash", but discard this commit's log message # x, exec = run command (the rest of the line) using shell # # These lines can be re-ordered; they are executed from top to bottom. # # If you remove a line here THAT COMMIT WILL BE LOST. # # However, if you remove everything, the rebase will be aborted. # # Note that empty commits are commented out


Now you can squash all commits into one commit as below

pick a0a0abc My first commit message squash b0a0abc Fix linting errors squash ca0a0abc Just added unit tests squash d0a0abc Removed css file
squash e0a0abc Resolve review comments


Then you close editor window and git will do `squash` for you and will create single commit. And you will be see new editor as below, where you can edit git message: 


# This is a combination of 5 commits.
# The first commit's message is:
My first commit message
# This is the 2nd commit message: Fix linting errors
# This is the 3rd commit message:

Just added unit tests

# This is the 4th commit message:

Removed CSS file

# This is the 5th commit message:

Resolve review comments
# Please enter the commit message for your changes. Lines starting # with '
#' will be ignored, and an empty message aborts the commit.


You can edit   first commit's message by changing its text or can keep as it is. But, you might need to delete other commits messages by removing it or simply adding `#` before message.



# This is a combination of 5 commits.
# The first commit's message is:
Added My new feature's updated message
# This is the 2nd commit message: # Fix linting errors
# This is the 3rd commit message:

# Just added unit tests

# This is the 4th commit message:

# Removed CSS file



Now save and exit the editor , you will see successful squashed commit in your branch.

You just need to push this to your remote repo by doing

 
git push origin my-branch -f  // you need to push forcefully

After rebasing we need to add `--force` in `git push` command. That means we are overwriting repository’s history.


2. 

You can use git merge --squash  as well, which is slightly more elegant way than
git rebase -i .

# Reset the current branch to the commit just before the last 5:
git reset --hard HEAD~5
# HEAD@{1} is where the branch was just before the previous command
# This command sets the state of the index to be as it would just
# after a merge from that commit:
git merge --squash HEAD@{1}

# Commit those squashed changes. The commit message will be helpfully
# Prepopulated with the commit messages of all the squashed commits:
git commit



3.
 
You can squash commits without git merge --squash  or  git merge --squash  also.


git reset --soft HEAD~5 &&
git commit
You can update/edit commit message with a concatenation of the existing commit message i.e. similar to what we do in  git rebase -i .
.

# Reset the current branch to the commit just before the last 5:
git reset --soft HEAD~5 && git commit --edit -m "$(git log --format=%B --reverse HEAD..HEAD@{1})"

Both of this methods will squash the last 5 commits into a single new commit. 
 
https://dc-techlogic.blogspot.com/2020/10/git-how-to-squash-commits-into-one.html








Post a Comment