Posted in git

[GIT] Some useful GIT commands

git-logo

GIT Help

git help -g
git help everyday

GIT config:

git config --global user.name "John Doe"
git config --global user.email "john@example.com"
git config --global color.ui false // Turn off git colored terminal output

GIT checkout + update branch:

git checkout <branch-name>
git fetch
git pull
git checkout -  // Checkout to previous branch

 

GIT update files:

git checkout —  <file-name>  // discard a file

 

GIT Status:

git status
git status --short --branch // Show git status short

 

GIT Diff

git diff
git diff --cache
git diff --name-only --diff-filter=U // List all conflicted files
// Check difference of current branch with compared-branch-name
git diff <compared-branch-name>
git diff -p -1 <file-name>  // show change of file

 

GIT – How to push code ?

git add .  
git commit -m "Message"
git push

Git Branch

git branch --merged master   // list all of branches which merged to master
git branch -vv // List branches and their upstreams and last commit on it
git branch --no-merged // List all of Branch which has WIP
git branch -m <new-branch-name> // Rename a branch
git branch --all | grep <text>  // Search commited text
// List all branches that are already merged into master
git branch --merged master
// Delete a branch on your local filesystem
git branch -d [name_of_your_new_branch]
git push origin --delete <remote_branchname> // Delete REMOTE branch

Create the branch on your local machine and switch in this branch :

  1. A) git pull
  2. B) Create a new branch:
  1. git branch <new-branch-name>
  1. OR  git checkout -b <new-branch-name>

 

  1. C) Push branch to github: git push origin <new-branch-name>

 

GIT Stash:

git stash list

git stash clear

git stash push -m “message here”

git stash apply stash@{0} OR git stash apply –index 0

git stash show stash@{0} -p  // show diff of stash

 

Git Reset

git fetch
 git reset --hard origin/develop
git update-ref -d HEAD   // Reset first commit
git reset HEAD <file-name> // Unstaging Staged file

 

GIT log:

git log -p <file_name>     // show log of a file_name
git reflog -2  // show history with <COMMIT-ID>
git show  <COMMIT-ID>
 git log --pretty=oneline -2
git log --oneline -2
git log --oneline -p  -2    // show history with <COMMIT-ID> and what is changed
git log --oneline --stat  -2   // show history with <COMMIT-ID> and which file is modified
git log --pretty=format:"%h - %an, %ar : %s" -2
git log --pretty=format:"%h %s" --graph   // show history in GRAPH mode
git log --pretty="%h - %s" --author='Binh'  // show history of certain User
git log --pretty="%h - %s" --since=1.weeks  // show history since 1 week
git log -S "message"  // Search log by Content
git whatchanged --since='2 weeks ago' // What changed since 2 Weeks?
git whatchanged --since='2 days ago' // What changed since 2 Days ?
// List commits and changes to a specific file (even through renaming)
git log --follow -p -- <file_path> 
// logs between date range
git log --since='FEB 1 2017' --until='FEB 14 2017'

 

## Visualize the version tree.

gitk path/to/files
git log --pretty=oneline --graph --decorate --all
gitk --all
git log --graph --pretty=format:'%C(auto) %h | %s | %an | %ar%d'

 

Git Cherry-pick

// Cherry-pick from Source-branch at the current branch
git cherry-pick <source-branch>

Git Aliases

git config --global alias.<handle> <command> 
Example: git config --global alias.st status

 

GIT Tag

git tag -d <tag-name>  // Delete LOCAL tag
git push origin :refs/tags/<tag-name>  // Delete REMOTE tag
 

Leave a comment