Skip to content

Instantly share code, notes, and snippets.

@quagly
Last active January 9, 2020 14:56
Show Gist options
  • Save quagly/0c1623dde5d87749e9eb9c1b74c3db0b to your computer and use it in GitHub Desktop.
Save quagly/0c1623dde5d87749e9eb9c1b74c3db0b to your computer and use it in GitHub Desktop.
git-commands

git commands I use sometimes

but not often, so I forget

Most common commands are covered in atlassian's gitflow documentation and this medium post

Usage documentation

Documentation for Commands used in this gist

git docs

atlassian docs

List Remote Branches

update remote tracking branches

update remote tracking branches

git fetch --all

see what will happen

git fetch --all --dry-run

list local and remote branches

git branch -a

list remote only

git branch -r

check out remote branch

When checking out a remote branch that has no local tracking branch yet In version 2 of git this is the same as switcing between local branches

git checkout <remote branch name>

review branching history

git reflog

Preview remote changes

Sometimes I want to review changes on the remote branch before merging with my local branch with a git pull this seems to not always show changes. Need to investigate.

fetch remote changes. This will only update the local copy of the remote branch, but not the local branch

git fetch

specify branch to compare to

git diff origin/develop

if good with all of the differences then merge to local copy like this

git pull --rebase origin develop

overwrite local with remote

This seems like a safer way, but I haven't tried it yet

git checkout branchA

git merge -X theirs branchB

DANGER!!! this will throw away all local changes commit not on remote

pull down remove branches - do not merge with local working tree

git fetch --all

point local working tree to origin commit of remote branch throwing away local changes

git reset --hard origin/<branch-name>

purge untracked or ignored files from the local tree

`git clean -fdx

conflict resolution

Haven't figured this out yet really

list files with conflicts

git diff --name-only --diff-filter=U

delete branch

If using git flow and wanted to delete remote branch when finished with feature or release use -F Normally there would not be a remote branch except for develop and master

git flow finish feature SMH-201 -F

remote branch

git push -d origin <branch_name>

local branch

git branch -d branch_name

force delete local branch

git branch -D branch_name

remove files that should not be tracked and add to .gitignore

following this blog and this blog

remove all files from local tracking

git rm -r --cached .

add them again honoring the changed .gitignore files in the working tree that should be ignored will be deleted

git add .

git commit -m "Clean up ignored files"

view commit history

basic useful view

git log

one I use a lot

git log --graph --oneline --decorate -10

formatting

each commit on a single line

git log --oneline

single line with merges and tags - NICE!

git log --oneline --decorate

single line with ascii merge tree - NICER!

git log --graph --oneline --decorate

inserts and deletes to files

git log --stat

get a summary of changes by author

git shortlog

detailed specification of output useful when piping to other commands

git log --pretty=format:"%cn committed %h on %cd"

filtering

three commits

git log -3

commits since yesterday

git log --after="yesterday"

by author

git log --author="Umar*"

by commit message ignore case

git log -i --grep="JRA-224:"

changes to specific files

git log -- logging.yml

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