Git Commands Recap : Tagging, Branching, Merging

 Git Tagging : git tag <tag_version>

git tag command is used to tag a given commit with version/tag name. Below command open default editor and tag current commit with tag v1.0. (which can be validated with "git log" command )
➜  custom_cloned_repo git:(master) ✗ git tag -a v1.0
  latest commit is tagged with tag: v1.0

What does "-a flag" significance ?
This "-a" flag tells Git to create an annotated flag. If we don't provide the flag (i.e. git tag v1.0) then it'll create a lightweight tag.

Annotated tags are recommended because they include a lot of extra information such as:
  • the person who made the tag
  • the date the tag was made
  • a message for the tag
List all tags that are in the repository:
➜  custom_cloned_repo git:(master) ✗ git tag v2.0   
➜  custom_cloned_repo git:(master) ✗ git tag
v1.0
v2.0

Deleting A Tag : Delete a given tag in repository using flag "-d" or "--delete" 
➜  custom_cloned_repo git:(master) ✗ git tag
v1.0
v2.0
➜  custom_cloned_repo git:(master) ✗ git tag -d v2.0
Deleted tag 'v2.0' (was 67d8efc)
➜  custom_cloned_repo git:(master) ✗ git tag        
v1.0
Using --delete also, we can delete tag like "git tag --delete v2.0"

Adding A Tag To A Past Commit: By appending 6 digits of hash post version details will add tag in given commit. Below command add tag with hash 59142f, which can be validated from image below.
➜  custom_cloned_repo git:(master) ✗ git tag -a v3.0 59142f
   v3.0 is added in second commit from top.


Git Branching : git branch <branch_name>

Create branch in repository: The command "git brach" lists all branches in given repository.
If we append a branch in above command, it will create a new brach. An asterisk will appear next to the name of the active branch.

➜  custom_cloned_repo git:(master) ✗ git branch
* master
➜  custom_cloned_repo git:(master) ✗ git branch fron_end_branch
➜  custom_cloned_repo git:(master) ✗ git branch                
  fron_end_branch
* master

Switch between branches : git checkout <branch_name>
git checkout <branch_name> is used to switch between branches. On executing "git checkout br_name"
  • It remove all files and directories from the Working Directory that Git is tracking.
  • go into the repository and pull out all of the files and directories of the commit that the branch points to.
➜  custom_cloned_repo git:(master) ✗ git status         
On branch master
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)

Untracked files:
  (use "git add <file>..." to include in what will be committed)

 front.css

nothing added to commit but untracked files present (use "git add" to track)
➜  custom_cloned_repo git:(master) ✗ git checkout fron_end_branch  
Switched to branch 'fron_end_branch'
➜  custom_cloned_repo git:(fron_end_branch) ✗ git status
On branch fron_end_branch
Untracked files:
  (use "git add <file>..." to include in what will be committed)

 front.css

nothing added to commit but untracked files present (use "git add" to track)

Delete a branch
: Using flag "-d" branch can be deleted. To force deletion, you need to use a capital D flag like "git branch -D fron_end_branch"
➜  custom_cloned_repo git:(master) ✗ git branch
  fron_end_branch
* master
➜  custom_cloned_repo git:(master) ✗ git branch -d fron_end_branch  
Deleted branch fron_end_branch (was 67d8efc).
➜  custom_cloned_repo git:(master) ✗ git branch                    
* master 

 Git Merging : git merge 

Combining branches together is called merging. There are two main types of merges in Git:
  • Regular merge : This combines two divergent branches, a commit is going to be made. Whichever branch the HEAD pointer is pointing at, that's the branch that will have the merge commit.
  • Fast-forward merge: A Fast-forward merge will just move the currently checked out branch forward until it points to the same commit that the other branch (in this case, footer) is pointing to.
Fast forward merge:
➜  custom_cloned_repo git:(master) ✗ git branch                    
* master
➜  custom_cloned_repo git:(master) ✗ git branch front_end
➜  custom_cloned_repo git:(master) ✗ git checkout front_end  
Switched to branch 'front_end'
➜  custom_cloned_repo git:(front_end) ✗ vi front.js
➜  custom_cloned_repo git:(front_end) ✗ git add front.js 
➜  custom_cloned_repo git:(front_end) ✗ git commit -m "commit js file change"
[front_end c8188dd] commit js file change
 1 file changed, 3 insertions(+)
 ➜  custom_cloned_repo git:(front_end) ✗ git checkout master  
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 1 commit.
  (use "git push" to publish your local commits)
➜  custom_cloned_repo git:(master) ✗ git merge front_end  
Updating 67d8efc..c8188dd
Fast-forward
 front.js | 3 +++
 1 file changed, 3 insertions(+)

Regular merge: Command for regular merge is same as above, just a new commit will be created.
➜  custom_cloned_repo git:(master) ✗ git merge forked_front_end  

1 Comments

Previous Post Next Post