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 -a with -m.

Solution

Terminal output

$ 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. Use git 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 use git cherry-pick with its name.

Solution

Terminal output

$ 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.txt came to main. The unfinished feature.txt did 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

Terminal output

$ 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 to main, 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.