Skip to content

Instantly share code, notes, and snippets.

@hugoalvarado
Created November 28, 2018 03:59
Show Gist options
  • Save hugoalvarado/246240510536bbcc213ae77eac87f9d1 to your computer and use it in GitHub Desktop.
Save hugoalvarado/246240510536bbcc213ae77eac87f9d1 to your computer and use it in GitHub Desktop.
Git Commands

Handy Git Command Dump

Find common parent

git merge-base velocity_msync master

git status git diff HEAD --name-only git diff --staged -M

  • diff of our most recent commit, which we can refer to using the HEAD pointer git diff HEAD git diff --staged

git log --oneline --decorate git log --oneline --decorate --graph --all git log --oneline git log -1 git log --stat git log -p git log -S function_name

  • Search a Git repository by commit message git log --grep='8044'

https://stackoverflow.com/questions/37219/how-do-you-remove-a-specific-revision-in-the-git-history git rebase --onto ^ git rebase -i

git config -l git config --get remote.origin.url

  • find changes in file g log --follow -U0 -S "if json_dict['role'] in ('find-me'" lib/file.py g log --follow --oneline -U0 --since=1.month -m lib/file.py

g log --follow -U0 -S "if json_dict['role'] in ('find-me'" lib/file.py g log --follow --oneline -U0 --since=1.month -m lib/file.py

g log --follow -U0 -S "value = cat.availableOptions.peek()[0];" lib/file.py

git diff file against its last change: git log -p [--follow] [-1]

git describe --tags git rev-parse HEAD

git show f2cd76d19f8371a27711728e1a21f67be3440371 git branch --contains f2cd76d19f8371a27711728e1a21f67be3440371

show local branches: git branch git branch -vv

change tracking branch git branch --set-upstream-to=origin/master

list branches: git branch --remote --list git branch -a git branch -r git remote show origin git remote -v git ls-remote

-Verify remote git remote -v

  • Update remote branch list git fetch upstream

  • Local branch and want to set it to a remote branch you just pulled down, use -u or --set-upstream-to option to git branch to explicitly set it at any time. git branch -u origin/serverfix

  • You can switch branches using: git checkout

-To check out a particular commit: git checkout

-Switch to a different commit and delete history git checkout --hard

  • Create and checkout new branch tracking the upstream/master remote git checkout -b mybranch upstream/master

  • Files can be changed back to how they were at the last commit: (That will reset foo.txt to HEAD - revert local changes) -- basically means: treat every argument after this point as a file name. git checkout -- foo.txt git checkout -- . git checkout HEAD -- my-file.txt

  • Pull from remote git repository and override the changes in my local repository: git fetch origin git reset --hard origin/master

git reset -- will unstage any staged changes for the given file(s). git checkout -- will reset unstaged file

-Delete branch matching name g branch | grep branch-name-to-find | xargs git branch -D

-Undo local commit (not added to remote repo and keep changes): git reset HEAD^ git reset HEAD~

-Undo local commit (not added to remote repo and loose changes): git reset HEAD^ --hard

git mv source dest git rm '*.txt'

git pull = fetch + merge git pull --rebase = fetch + rebase g pull upstream master --rebase

Update the local master with upstream, or other remote git pull upstream master git push origin master

  • Set tracking information on my_branch git branch --set-upstream-to=/ my_branch

  • (To keep your branch up to date by pulling from upstream and pushing to your branch remote.) git pull --rebase git push origin mybranch

  • Keeping a fork up to date - Updating your fork from original repo to keep up with their changes: git pull upstream master git push

-- add remote git remote add git@github.com:/.git

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment