Skip to content

Instantly share code, notes, and snippets.

@byjg
Last active February 2, 2022 15:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save byjg/1f66c0bcfc7de60d4857af97bf5dd97a to your computer and use it in GitHub Desktop.
Save byjg/1f66c0bcfc7de60d4857af97bf5dd97a to your computer and use it in GitHub Desktop.
If you want a single commit you don't need to rebase your development branch. Just add your commit, and then merge squeezing the branch into a single commit.

Prepare the test environment

# Create the git repository
mkdir test2
cd test2
git init

# Add some files
echo "Content" > file1.txt
echo "Content" > file2.txt
git add .
git commit -m "Initial commit"

# Check commits
git log --oneline 

The result:
c14c3f6 (HEAD -> master) Initial commit

Add one more commit

echo "Added a new line" >> file1.txt
git add .
git commit -m "Second commit in master"
git log --oneline 

The result:
1c4db7a (HEAD -> master) Second commit in master
c14c3f6 Initial commit

Create a branch and several commits

git checkout -b branch1
echo "More lines in fileee1" >> file1.txt
git add .
git commit -m "Add more lines on file1"
sed -i -e "s/fileee1/file1/g" file1.txt
git add file1.txt
git commit -m "Fix typo"
echo "*.txt-e" >> .gitignore
git add .
git commit -m "Added .gitignore"
git log --oneline

The result:
14bdb2d (HEAD -> branch1) Added .gitignore
db26032 Fix typo
e7596e0 Add more lines on file1
1c4db7a (master) Second commit in master
c14c3f6 Initial commit

Now, rebase working branch into a single commit

git reset --soft master    # This is the equivalent of "git rebase --interative master" where I select FIX-UP
git add . 
git commit -m "Rebase FIX-UP"
git log --oneline

The result:
e697511 (HEAD -> branch1) Rebase FIX-UP
1c4db7a (master) Second commit in master
c14c3f6 Initial commit

Merge the rebased branch.

git checkout master
git merge --no-edit branch1
git branch -D branch1
git log --oneline

The result:
06f98ca (HEAD -> master) Merge branch 'branch1'
e697511 Rebase FIX-UP
1c4db7a Second commit in master
c14c3f6 Initial commit

Create another branch and squash commit

git checkout -b branch2
echo "Add one line" >> file1.txt
git add .
git commit -m "Add one line to file1"

echo "Add other line line" >> file1.txt
git add .
git commit -m "Add other line to file1"

git log --oneline

The result:
0c83bd3 (HEAD -> branch2) Add other line to file1
321cb1e Add one line to file1
06f98ca (master) Merge branch 'branch1'
e697511 Rebase FIX-UP
1c4db7a Second commit in master
c14c3f6 Initial commit

Now, rebase working branch into a single commit

git reset --soft master    # This is the equivalent of "git rebase --interative master" where I select FIX-UP
git add . 
git commit -m "Rebase branch2 FIX-UP"
git log --oneline

The result:
14665f1 (HEAD -> branch2) Rebase branch2 FIX-UP
06f98ca (master) Merge branch 'branch1'
e697511 Rebase FIX-UP
1c4db7a Second commit in master
c14c3f6 Initial commit

Merge the rebased branch.

git checkout master
git merge --no-edit branch2
git branch -D branch2
git log --oneline

The result:
14665f1 (HEAD -> master) Rebase branch2 FIX-UP
06f98ca Merge branch 'branch1'
e697511 Rebase FIX-UP
1c4db7a Second commit in master
c14c3f6 Initial commit

History log of the file1.txt

git log -p file1.txt | grep commit


Prepare the test environment

# Create the git repository
mkdir test1
cd test1
git init

# Add some files
echo "Content" > file1.txt
echo "Content" > file2.txt
git add .
git commit -m "Initial commit"

# Check commits
git log --oneline 

The result:
5852f1b (HEAD -> master) Initial commit

Add one more commit

echo "Added a new line" >> file1.txt
git add .
git commit -m "Second commit in master"
git log --oneline 

The result:
447644d (HEAD -> master) Second commit in master
5852f1b Initial commit

Create a branch and several commits

git checkout -b branch1
echo "More lines in fileee1" >> file1.txt
git add .
git commit -m "Add more lines on file1"
sed -i -e "s/fileee1/file1/g" file1.txt
git add file1.txt
git commit -m "Fix typo"
echo "*.txt-e" >> .gitignore
git add .
git commit -m "Added .gitignore"
git log --oneline

The result:
349c432 (HEAD -> branch1) Added .gitignore
f0c54d2 Fix typo
ba09ab8 Add more lines on file1
447644d (master) Second commit in master
5852f1b Initial commit

Now, merge squashing into a single commit

git checkout master
git merge --squash branch1
git commit -m "Squashed commit"
git branch -D branch1
git log --oneline

The result:
c709e2c (HEAD -> master) Squashed commit
447644d Second commit in master
5852f1b Initial commit

Create another branch and squash commit

git checkout -b branch2
echo "Add one line" >> file1.txt
git add .
git commit -m "Add one line to file1"

echo "Add other line line" >> file1.txt
git add .
git commit -m "Add other line to file1"

git log --oneline

The result:
016032c (HEAD -> branch2) Add other line to file1
369d482 Add one line to file1
22d5122 (master) Squashed commit
97109fd Second commit in master
6c89f18 Initial commit

Now, merge squashing into a single commit

git checkout master
git merge --squash branch2
git commit -m "Squashed second commit"
git branch -D branch2
git log --oneline

The result:
16e0184 (HEAD -> master) Squashed second commit
22d5122 Squashed commit
97109fd Second commit in master
6c89f18 Initial commit

History log of the file1.txt

git log -p file1.txt | grep commit

The result:
commit 16e0184db9e42bc2abda4f68dd2df94ffd62a87b
    Squashed second commit
commit 22d5122b1e266084f2b29f7ddc9642188ee2a053
    Squashed commit
commit 97109fddda784dd2f5d8ff3cd07b7f3723206ea2
    Second commit in master
commit 6c89f18327f853c7e47537826078672b083c9031
    Initial commit
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment