Skip to content

Instantly share code, notes, and snippets.

@SureshKL
Last active March 19, 2019 16:35
Show Gist options
  • Save SureshKL/8017ceb0886f9d8c3f811fe96070e911 to your computer and use it in GitHub Desktop.
Save SureshKL/8017ceb0886f9d8c3f811fe96070e911 to your computer and use it in GitHub Desktop.
Useful Git Commands

Useful Git commands

Used initial phase Used on daily basis Used rarely(Advanced)
git init git add git clean
git config git commit git blame
git remote git checkout git show
git push git rm
git pull git revert
git log git reset
git status git rebase
git stash git merge

Table of Contents:

Files to be concerned:

  • .gitignore
  • .git\config

git init

Convert a directory into a git repository

git config

Configure Git command and repository settings

Few options

  • git config --list
  • git config --global user.name "Suresh K L"
  • git config --global user.email "Suresh K L"

Three types of config:

--global              use global config file  # C:\Users\skoolanx\.gitconfig
--system              use system config file  # C:\Program Files\Git\mingw64\etc\gitconfig
--local               use repository config file # .git\config

Git alias

git config --global alias.co checkout git config --global alias.br branch git config --global alias.ci commit git config --global alias.st status

git remote

Add remote git repository URLs for fetch/push git remote -v

git add

Add the contents to track

git commit

Save the changes with meaningful message as a checkpoint

Reset author (Usefull in gerrit based project)

git commit --amend --reset-author

git push

Push local change to remote tracking branch

git pull

Pull the updates from remote tracking branch

git log

Investigate git history

git blame

Find out modifications by author name

git status

Show the working tree status

git stash

Save changes temporarily to keep the working tree clean

git show

Show the changes happened to a commit

git checkout

  • Switch b/w branches
  • Chekcout a commit to try out

git revert

  • Revert commited changes by creating a new revert commit
  • To be used when commits are published to remote

git reset

  • Reset the uncommited changes
  • Reset the history by moving the HEAD
  • *Recommended to use only when commits are local
  • It cannot be used to reset commit anywhere.
  • It works like a paper roll. A paper can be rolled only from the end and not anywhere in between
> git reset HEAD file4.txt
> git reset HEAD~3 file4.txt  # Reset changes made to file by 3 commits from where HEAD is now

Three types of reset:

  • --mixed(default): Resets the index but not the working tree
  • --soft: Does not reset index or working tree. But resets the HEAD to <commit>
  • --hard: Resets index and working tree

git rebase

  • Get all the commits from a branch to working branch
  • Replay all the new commits in current branch on top of

git merge

  • Merge all the commits from one branch to another
  • Creates a new commit as Merge commit

git clean

  • Cleanup the working directory

git blame

  • Find out who and when code was modified
> git blame file4.txt
657b9ad2 (Test User         2019-03-15 19:21:32 +0530 1) "file4.txt"
00000000 (Not Committed Yet 2019-03-15 19:54:41 +0530 2) Something different
00000000 (Not Committed Yet 2019-03-15 19:54:41 +0530 3) "file4.txt"
00000000 (Not Committed Yet 2019-03-15 19:54:41 +0530 4) "file4.txt"

git show

> git show 657b9ad2 --name-only
commit 657b9ad2e0b90dd5495b141bc446396e90fc5aae
Author: Test User <TestUser@domain.com>
Date:   Fri Mar 15 19:21:32 2019 +0530

    Added file4.txt

file4.txt

Common Scenarios

Scenario: Revert one or more specific files from recent commit to it's previous or specific state

  1. Create a new file and commit changes
echo New > file.txt
git add file.txt
git commit -m "Commit 1"
  1. Modify the newly added file with some more new changes
echo Additional changes >> file.txt
git add file.txt
git commit -m "Commit 2"
  1. Reset changes made in step 2 to step 1 with a new commit
git checkout HEAD~1 -- file.txt
git add file.txt
git commit -m "Commit 3"

Scenario: Revert the recent commit to it's previous state as things broke

echo First modification > file.txt
git add file.txt
git commit -m "Commit 1"
echo Second modification >> file.txt
git add file.txt
git commit -m "Commit 2"
echo Third modification >> file.txt
git add file.txt
git commit -m "Commit 3"
git revert HEAD --no-commit
git commit -m "Commit4: Revert Commit 3 to Commit 2"

Scenario: Find out author and commit details for specific block of code

echo Line 1 > file.txt
git add file.txt
git commit -m "Commit 1"
echo Line 2 >> file.txt
echo Line 3 >> file.txt
echo Line 4 >> file.txt
echo Line 5 >> file.txt
git add file.txt
git commit -m "Commit 2"
echo Line 6 >> file.txt
echo Line 7 >> file.txt
git add file.txt
git commit -m "Commit 3"
git blame --color-lines -L 1,5 file.txt

Scenario: Squash multiple commits related to PEP8 changes into one

echo list = [1,2,3,] > file.py
git add file.py
git commit -m "Commit 1: Created list of numbers"
echo list = [1, 2, 3,] > file.py
git add file.py
git commit -m "Commit 2: Added space around operators"
echo numbers = [1, 2, 3,] > file.py
git add file.py
git commit -m "Commit 3: Renamed variable name"
> git log --oneline
2f408e4 (HEAD -> scenario) Commit 3: Renamed variable name
043cf10 Commit 2: Added space around operators
295f8d5 Commit 1: Created list of numbers
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
  • After running below command, default editor of git opens
> git rebase -i 895e7da
pick 295f8d5 Commit 1: Created list of numbers
pick 043cf10 Commit 2: Added space around operators
pick 2f408e4 Commit 3: Renamed variable name

# Rebase 895e7da..2f408e4 onto 895e7da (3 commands)
#
# Commands:
# p, pick <commit> = use commit
# r, reword <commit> = use commit, but edit the commit message
# e, edit <commit> = use commit, but stop for amending
# s, squash <commit> = use commit, but meld into previous commit
# f, fixup <commit> = like "squash", but discard this commit's log message
# x, exec <command> = run command (the rest of the line) using shell
# b, break = stop here (continue rebase later with 'git rebase --continue')
# d, drop <commit> = remove commit
# l, label <label> = label current HEAD with a name
# t, reset <label> = reset HEAD to a label
# m, merge [-C <commit> | -c <commit>] <label> [# <oneline>]
# .       create a merge commit using the original merge commit's
# .       message (or the oneline, if no original merge commit was
# .       specified). Use -c <commit> to reword the commit message.
#
# These lines can be re-ordered; they are executed from top to bottom.
#
# If you remove a line here THAT COMMIT WILL BE LOST.
#
# However, if you remove everything, the rebase will be aborted.
#
# Note that empty commits are commented out
~
~
--INSERT--
  • Here modify the first column with required actions
  • Below changes indicate we want to squash Commits 2 & 3 into 1, having the commit message of Commit 1 only
pick  295f8d5 Commit 1: Created list of numbers
fixup 043cf10 Commit 2: Added space around operators
fixup 2f408e4 Commit 3: Renamed variable name

:wq
  • Now check the logs to see how the squash has happened
> git log --oneline
1d46f11 (HEAD -> scenario) Commit 1: Created list of numbers
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt

Scenario: Permanently remove local commits using git reset with (--mixed, --soft, --hard)

echo First modification > file.txt
git add file.txt
git commit -m "Commit 1"
echo Second modification >> file.txt
git add file.txt
git commit -m "Commit 2"
> git log --oneline
cf9f64f (HEAD -> scenario) Commit 2
200b7c4 Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt

--mixed

  • Remove the previous commit from history
  • But retain the changes made in previous commit in working tree
> git reset --mixed HEAD~1
Unstaged changes after reset:
M       file.txt
> git status
On branch scenario
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:   file.txt

no changes added to commit (use "git add" and/or "git commit -a")
> git log --oneline
200b7c4 (HEAD -> scenario) Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt

Continuation after mixed reset

git add file.txt
git commit -m "Commit 2"
> git log --oneline
8b84203 (HEAD -> scenario) Commit 2
200b7c4 Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
> git status
On branch scenario
nothing to commit, working tree clean

--hard

  • After this command, the changes made to file in Commit 2 is completely lost
> git reset --hard HEAD~1
HEAD is now at 200b7c4 Commit 1
> git status
On branch scenario
nothing to commit, working tree clean
> git log --oneline
200b7c4 (HEAD -> scenario) Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt

Continuation after hard reset

echo Second modification >> file.txt
git add file.txt
git commit -m "Commit 2"
> git log --oneline
f32f940 (HEAD -> scenario) Commit 2
200b7c4 Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt

--soft

git reset --soft HEAD~1
> git log --oneline
200b7c4 (HEAD -> scenario) Commit 1
895e7da (master) Revert "Modified file1.txt"
c5e5a93 Modified file1.txt
d4ccf91 Add file1.txt
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment