Git Advanced
Create an annotated tag for a release
Description: Mark the latest commit as version v1.0 with the message “First release”. Show all tags and the details of the tag.
Hint:
- Use
git tag -awith-m.
Solution
$ git tag -a v1.0 -m "First release"
$ git tag
v1.0
$ git show v1.0 –no-patch
tag v1.0
Tagger: Robin Sharma <robin@example.com>
Date: Sat Jan 10 10:49:00 2026 +0000
First release
commit 378ffde0b81bbd9da251867b0d52884f32d49425 (HEAD -> main, tag: v1.0)
Author: Robin Sharma <robin@example.com>
Date: Sat Jan 10 10:42:00 2026 +0000
Add app
How it works
- An annotated tag stores the tagger, date and message.
- Tags are not pushed by
git push. Usegit push origin v1.0.
Read Git Tag.
Copy one commit to another branch with cherry-pick
Description: The branch develop has an unfinished feature and a bug fix. Bring only the bug fix commit to main.
Hint:
- Find the commit with
git log, then usegit cherry-pickwith its name.
Solution
$ git log –oneline develop
0bbe41e (develop) Fix header bug
c03680a Add feature (not ready)
1e3d759 (HEAD -> main) Add home
$ git cherry-pick develop
[main 97e3b7d] Fix header bug
Date: Sat Jan 10 11:31:00 2026 +0000
1 file changed, 1 insertion(+)
create mode 100644 fix.txt
$ git log –oneline
97e3b7d (HEAD -> main) Fix header bug
1e3d759 Add home
$ ls
fix.txt
home.txt
How it works
- The fix is the latest commit of
develop, so we can use the branch name. Otherwise use the hash. - Only
fix.txtcame tomain. The unfinishedfeature.txtdid not.
Read Git Cherry Pick.
Recover a deleted branch with git reflog
Description: You deleted the branch feature-x with git branch -D and you want it back.
Hint:
- The reflog keeps the commit. Create a branch from it.
Solution
$ git reflog -3
1e3d759 (HEAD -> main) HEAD@{0}: checkout: moving from feature-x to main
72e9eac HEAD@{1}: commit: Add feature work
1e3d759 (HEAD -> main) HEAD@{2}: checkout: moving from main to feature-x
$ git branch feature-x HEAD@{1}
$ git log –oneline feature-x
72e9eac (feature-x) Add feature work
1e3d759 (HEAD -> main) Add home
How it works
HEAD@{1}is the commit where HEAD was before we moved tomain, which is the last commit of the deleted branch.- The reflog works only on your computer and for some time (90 days by default).
Read Git Reflog.