Top 10 Git Commands that all you need to know

   by Kundan Ugale, Sunday, Aug 18, 2019

Being one of the leader in open source community, Github is quite popular source control tool in developer community. If you are in open source development then you must be aware of Git commands. This post will help you to advance your knowledge about git commands.

if you are using github as your source control then here are some practical scenarios where you can use these git commands in your day to day work.

Note: This post is for those who have some basic knowledge about source control system and git.

 

Scenario 1: When you switch to a new branch locally from master branch and this branch does not exist on github yet

$ git checkout -b feature-branch

  Switched to a new branch 'feature-branch'

$ git push --set-upstream origin feature-branch

above command will set the upstream and create a new branch with your changes.

 

Scenario 2: When you have some un-committed changes on local repo but you want to get latest changes from remote branch

if you try to do a git pull it will give you following error.

$ git pull --rebase

error: cannot pull with rebase: Your index contains uncommitted changes.

error: please commit or stash them.

this is because there is conflict between your remote and local branch. You can stash your change temporarily. After that you can do a git pull --rebase to sync the remote branch

$ git stash save

Saved working directory and index state WIP on feature-branch: 929f7f8 Test commit

above command will save all your un-committed changes to shelve set which you can retrieve it later by using following command

$ git stash pop

 

Scenario 3: When you have locally committed changes (not pushed to remote branch) but you want to revert it to un-committed state then use git reset HEAD~

This will revert your changes from committed to un-committed

$ git reset HEAD~

Unstaged changes after reset:

M       README.md

$ git status

On branch test-branch

Your branch is up to date with 'origin/test-branch'.

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   README.md

no changes added to commit (use "git add" and/or "git commit -a")

 

Scenario 4: When you want to checkout to specific commit you need to mention a reference point (HEAD) in your checkout command

You can get the details about the commits with git reflog command. Every time the HEAD is modified there will be a new entry in the reflog.

$ git reflog

3da8a89 (HEAD -> test-branch, origin/test-branch) HEAD@{0}: reset: moving to HEAD~
e9f8687 HEAD@{1}: commit: test reset HEAD~
3da8a89 (HEAD -> test-branch, origin/test-branch) HEAD@{2}: commit: test commit
a40550d HEAD@{3}: pull git@github.com:cloud-solutions/templatedb-job-scripts.git test-branch: Fast-forward
f6df8e0 (origin/master, origin/HEAD, master) HEAD@{4}: reset: moving to HEAD
f6df8e0 (origin/master, origin/HEAD, master) HEAD@{5}: reset: moving to HEAD
f6df8e0 (origin/master, origin/HEAD, master) HEAD@{6}: checkout: moving from master to test-branch
f6df8e0 (origin/master, origin/HEAD, master) HEAD@{7}: commit: cleanup
402e25c HEAD@{8}: clone: from git@github.com:cloud-solutions/templatedb-job-scripts.git

Now with above reference you can checkout to any reference point by providing commit id.

$ git checkout HEAD@{8}

Note: checking out 'HEAD@{8}'.
You are in 'detached HEAD' state. You can look around, make experimental
changes and commit them, and you can discard any commits you make in this
state without impacting any branches by performing another checkout.
If you want to create a new branch to retain commits you create, you may
do so (now or later) by using -b with the checkout command again. Example:

  git checkout -b <new-branch-name>

  HEAD is now at f6df8e0 cleanup
$ git checkout -b "Reset-head-to-f6df8e0"

Switched to a new branch 'Reset-head-to-f6df8e0'

$ git push --set-upstream origin Reset-head-to-f6df8e0   

All set: you created new branch with changes (git head) pointing to your desired branch f6df8e0

 

Scenario 5: When you have messed up with all changes and now you want to get latest changes from remote branch just do a hard reset with following command.

$ git reset --hard

HEAD is now at 3da8a89 test reset \~HEAD

  Note: if you have any local changes then everything will be overwritten by remote changes.

 

Scenario 6: Git maintains its own log which you can view using git log command.

if you want to see short notes about git log then use ‘–oneline’ argument.  

$ git log --oneline
da133f6 (HEAD -> feature-branch) test reset ~HEAD
929f7f8 (origin/feature-branch) Test commit
3fbfdf9 (origin/master, origin/HEAD, master) Create README.md
cc79ac3 To Check Auto Trigger
fffe400 To Check Auto Trigger
9f50c2d Initial Code check-in

 

Scenario 7: Git tag - Git has the ability to tag specific points in a repository’s history as being important.

tag functionality is used to mark release points (v1.0, v2.0 and so on). Lets check out various flavors of tag command.

Git supports two types of tags: lightweight and annotated. A lightweight tag is very much like a branch that doesn’t change — it’s just a pointer to a specific commit. However annotated tags are stored as full objects in the Git database. It’s recommended that you create annotated tags

Annotated Tags

$ git tag -a v1.0.3 -m "my version 1.0.3"
$ git tag
v1.0.2
v1.0.1
v1.0.0

Lightweight Tags: To create a lightweight tag, don’t supply any of the -a, -s, or -m options, just provide a tag name:

$ git tag v1.4-lw-test
$ git tag
v1.0.1
v1.1.3
v1.1.4
v1.1.4-lw-test
v1.1.5

if you run git show on the tag, you don’t see the extra tag information. The command just shows the commit:

$ git show v1.0.2
commit 38f77ef7ca2116defabe8d5da11x61ba38a0d359 (tag: v1.0.2, tag: v1.0.1, origin/feature-branch)
Author: Author22 <testmail@test.com>
Date:   Wed Aug 28 20:30:39 2019 +0200

    test version tagging

 

Scenario 8: Git archive - This command is useful when you are working in not Windows environment

You can easily archive your current branch to to zip file and keep it in the same directory.

$ git archive --format zip --output filename.zip feature-branch

 

Scenario 9: To modify the commit message of your commit use commit message with amend attribute

$ git commit --amend -m "Change your commit message"

 

Scenario 10: If you want to easily search any keyword through any committed tree use git grep command

$ git grep -p 'Console.Write' *.vb
ConsoleAppCI/ConsoleAppCI/Module1.vb:  Console.WriteLine("Test the Continuos Integration")
ConsoleAppCI/ConsoleAppCI/Module1.vb:  Console.WriteLine("New Module Added ValidateUser")
ConsoleAppCI/ConsoleAppCI/Module1.vb:  Console.WriteLine("New Module Added ValidateUser")

 

Scenario 11: specify the private SSH-key to use when executing shell command on Git

Note: You need to set your ssh key to your git account before you try these commands. Use these instructions to setup your ssh key in github

use following command to add ssh key while executing git command

ssh-agent bash -c 'ssh-add /path-to-sshkey/private-key; git clone git@github.com:user/project.git'

and if you want to add ssh key permanently to your session

# start the ssh-agent in the background
$ eval $(ssh-agent -s)
> Agent pid 59566

$ ssh-add ~/.ssh/id_rsa




Categories



Clap for me