GIT ADVANCED USAGE

  • Combine multiple commits into one commit with a custom commit message
  • Combine multiple commits into one commit while preserving all commit messages
  • Remove one or multiple commits
  • Change commit messages
  • Prepare feature branch for the merge
$ git config --global core.editor code
$ git config --global diff.tool default-difftool 
$ git config --global difftool.code.cmd "code --wait --diff $LOCAL $REMOTE"
$ git config --global difftool.prompt false
$ git config --global merge.tool code 
$ git config --global mergetool.code.cmd "code --wait $MERGED"
$ git config --global mergetool.prompt false 
$ git config --global mergetool.keepbackup false
$ git config --global alias.onelinegraph 'log --oneline --graph --decorate'
$ git config --global alias.expireunreachablenow 'reflog expire --expire-unreachable=now --all' 
$ git config --global alias.gcunreachablenow 'gc --prune=now'
$ git config --global fetch.prune true
$ git config --global push.followTags true
$ git clone git@github.com:jamalshahverdiev/gitexplain.git && cd gitexplain

Combine multiple commits into one commit with the custom commit message

$ cat <<'EOF' > ./autocommit.sh 
#!/usr/bin/env bash
if [[ $# != 2 ]]; then echo "Usage: ./$(basename $0) f_num s_num"; exit 21; fi
file_name=commitfile.txt
f_num=$1
s_num=$2
if [[ ! -f $file_name ]]; then touch $file_name && git add $file_name; fi
for num in `seq $f_num $s_num`; do
echo "Commit #$num - Change line $num" >> $file_name
git commit -am "Add line number: #$num"
done
EOF
chmod +x autocommit.sh
$ ./autocommit.sh 1 5
$ git onelinegraph
* b6cc70f (HEAD -> main) Add line number: #5
* d0dbac0 Add line number: #4
* 48e2dcb Add line number: #3
* 6068a3a Add line number: #2
* cfcef54 Add line number: #1
* 5ccb1e7 (origin/main, origin/HEAD) Initial commit
$ git rebase -i HEAD~5
[detached HEAD 41360ed] Merge last 5 commits into first one: #1
Date: Thu Mar 17 12:43:02 2022 +0400
1 file changed, 1 insertion(+)
create mode 100644 commitfile.txt
Successfully rebased and updated refs/heads/main.
$ git onelinegraph
* 122b0ea (HEAD -> main) Merge last 5 commits into first one: #1
* 5ccb1e7 (origin/main, origin/HEAD) Initial commit

Combine multiple commits into one commit while preserving all commit messages

$ ./autocommit.sh 6 10
[main affdcd5] Add line number: #6
1 file changed, 1 insertion(+)
[main a32d523] Add line number: #7
1 file changed, 1 insertion(+)
[main 408f7d8] Add line number: #8
1 file changed, 1 insertion(+)
[main f94024d] Add line number: #9
1 file changed, 1 insertion(+)
[main a3d5273] Add line number: #10
1 file changed, 1 insertion(+)
$ git rebase -i HEAD~5
[detached HEAD fb65358] Merge next last 5 commits into first one with their commit messages: #1
Date: Thu Mar 17 12:50:36 2022 +0400
1 file changed, 1 insertion(+)
[detached HEAD 0fdbabf] Merge next last 5 commits into first one with their commit messages: #1
Date: Thu Mar 17 12:50:36 2022 +0400
1 file changed, 5 insertions(+)
Successfully rebased and updated refs/heads/main.
$ git log -1
commit 0fdbabfa0b51e5202d7feff5a8ee6d9f51a15d64 (HEAD -> main)
Author: Jamal Shahverdiev <jamal.shahverdiev@gmail.com>
Date: Thu Mar 17 12:50:36 2022 +0400
Merge next last 5 commits into first one with their commit messages: #1

Add line number: #7

Add line number: #8

Add line number: #9

Add line number: #10

Remove one or multiple commits

$ git onelinegraph
* 0fdbabf (HEAD -> main) Merge next last 5 commits into first one with their commit messages: #1
* 122b0ea Merge last 5 commits into first one: #1
* 5ccb1e7 (origin/main, origin/HEAD) Initial commit
$ git rebase -i HEAD~2
$ git onelinegraph
* 5ccb1e7 (HEAD -> main, origin/main, origin/HEAD) Initial commit

Change commit messages

$ ./autocommit.sh 1 5
[main 209b669] Add line number: #1
1 file changed, 1 insertion(+)
create mode 100644 commitfile.txt
[main 266989c] Add line number: #2
1 file changed, 1 insertion(+)
[main 442e773] Add line number: #3
1 file changed, 1 insertion(+)
[main c60c65f] Add line number: #4
1 file changed, 1 insertion(+)
[main 2958109] Add line number: #5
1 file changed, 1 insertion(+)
$ git onelinegraph
* 2958109 (HEAD -> main) Add line number: #5
* c60c65f Add line number: #4
* 442e773 Add line number: #3
* 266989c Add line number: #2
* 209b669 Add line number: #1
* 5ccb1e7 (origin/main, origin/HEAD) Initial commit
$ git rebase -i HEAD~3
[detached HEAD 8886abb] Modified commit message for commit #3
Date: Thu Mar 17 13:14:22 2022 +0400
1 file changed, 1 insertion(+)
[detached HEAD d5b96a1] Modified commit message for commit: #5
Date: Thu Mar 17 13:14:22 2022 +0400
1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/main.
$ git onelinegraph
* d5b96a1 (HEAD -> main) Modified commit message for commit: #5
* a2db568 Add line number: #4
* 8886abb Modified commit message for commit #3
* 266989c Add line number: #2
* 209b669 Add line number: #1
* 5ccb1e7 (origin/main, origin/HEAD) Initial commit

Prepare feature branch for the merge

$ git checkout -b featureA
Switched to a new branch 'featureA'
$ ./autocommit.sh 6 8
[featureA 6c64ddd] Add line number: #6
1 file changed, 1 insertion(+)
[featureA 5e2a67d] Add line number: #7
1 file changed, 1 insertion(+)
[featureA 31f745a] Add line number: #8
1 file changed, 1 insertion(+)
$ git checkout main && git fetch origin && git pull origin main
$ git checkout featureA
$ git rebase -i main
[detached HEAD 6f94556] Change main variable to the parameter - CORE-226
Date: Thu Mar 17 13:29:20 2022 +0400
1 file changed, 1 insertion(+)
Successfully rebased and updated refs/heads/featureA.
$ git onelinegraph
* 9fc0736 (HEAD -> featureA) Change main variable to the parameter - CORE-226
* d5b96a1 (main) Modified commit message for commit: #5
* a2db568 Add line number: #4
* 8886abb Modified commit message for commit #3
* 266989c Add line number: #2
* 209b669 Add line number: #1
* 5ccb1e7 (origin/main, origin/HEAD) Initial commit
$ git checkout main && git merge featureA
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 5 commits.
(use "git push" to publish your local commits)
Updating d5b96a1..9fc0736
Fast-forward
commitfile.txt | 3 +++
1 file changed, 3 insertions(+)
$ git onelinegraph
* 9fc0736 (HEAD -> main, featureA) Change main variable to the parameter - CORE-226
* d5b96a1 Modified commit message for commit: #5
* a2db568 Add line number: #4
* 8886abb Modified commit message for commit #3
* 266989c Add line number: #2
* 209b669 Add line number: #1
* 5ccb1e7 (origin/main, origin/HEAD) Initial commit

Cherry Pick

$ git checkout -b dev && ./autocommit.sh 9 12
Switched to a new branch 'dev'
[dev bfd6b36] Add line number: #9
1 file changed, 1 insertion(+)
[dev 0723b87] Add line number: #10
1 file changed, 1 insertion(+)
[dev cae4448] Add line number: #11
1 file changed, 1 insertion(+)
[dev 3412960] Add line number: #12
1 file changed, 1 insertion(+)
$ git checkout main && git checkout -b hotfix-CORE-222
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 6 commits.
(use "git push" to publish your local commits)
Switched to a new branch 'hotfix-CORE-222'
$ echo "Fix: Resolved icon page bug from main branch" >> commitfile.txt
$ git commit -am "Fix: Hotfix for CORE-222"
[hotfix-CORE-222 9460c06] Fix: Hotfix for CORE-222
1 file changed, 1 insertion(+)
$ git checkout main
Switched to branch 'main'
Your branch is ahead of 'origin/main' by 6 commits.
(use "git push" to publish your local commits)
$ git merge hotfix-CORE-222
Updating 9fc0736..9460c06
Fast-forward
commitfile.txt | 1 +
1 file changed, 1 insertion(+)
$ git checkout dev
Switched to branch 'dev'
$ git cherry-pick 9460c06
Auto-merging commitfile.txt
CONFLICT (content): Merge conflict in commitfile.txt
error: could not apply 9460c06... Fix: Hotfix for CORE-222
hint: After resolving the conflicts, mark them with
hint: "git add/rm <pathspec>", then run
hint: "git cherry-pick --continue".
hint: You can instead skip this commit with "git cherry-pick --skip".
hint: To abort and get back to the state before "git cherry-pick",
hint: run "git cherry-pick --abort".
$ git add commitfile.txt && git cherry-pick --continue
[dev e43818b] Fix: Hotfix for CORE-222
Date: Thu Mar 17 14:10:40 2022 +0400
1 file changed, 1 insertion(+)
$ git onelinegraph
* e43818b (HEAD -> dev) Fix: Hotfix for CORE-222
* 3412960 Add line number: #12
* cae4448 Add line number: #11
* 0723b87 Add line number: #10
* bfd6b36 Add line number: #9
* 9fc0736 (featureA) Change main variable to the parameter - CORE-226
* d5b96a1 Modified commit message for commit: #5
* a2db568 Add line number: #4
* 8886abb Modified commit message for commit #3
* 266989c Add line number: #2
* 209b669 Add line number: #1
* 5ccb1e7 (origin/main, origin/HEAD) Initial commit

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store