DEV Community

Fred B.
Fred B.

Posted on • Updated on

Git/Github Cheat Sheet

Originally posted on Codepen on January 21, 2015

Offical Doc

Documentation

Git stash

Handle blocking situations well

Git Tutorial

Advanced Git Tutorial (Level 1): Git Rebase


Show commits oneline, author, commit date, change

git log --pretty=format:"%h%x09%an%x09%ad%x09%s"
Enter fullscreen mode Exit fullscreen mode

%h = abbreviated commit hash
%x09 = tab (character for code 9)
%an = author name
%ad = author date (format respects --date= option)
%s = subject

Git stash

Use git stash to be able to checkout master

git stash // put changes aside temporarily
git checkout master
git stash pop // apply the changes back to the master local branch
Enter fullscreen mode Exit fullscreen mode

Create and view repo from command line

gh repo create boostup/`basename "$PWD"`
gh repo view -w boostup/`basename "$PWD"`
Enter fullscreen mode Exit fullscreen mode

Check for unpushed commits

git log --branches --not --remotes
Enter fullscreen mode Exit fullscreen mode

or

git log @{u}..
Enter fullscreen mode Exit fullscreen mode

or

git log origin/master..HEAD 
Enter fullscreen mode Exit fullscreen mode

Publish on gh-pages

Follow instructions here

Open repo on Git Hub

gh repo view -w boostup/`basename "$PWD"`
Enter fullscreen mode Exit fullscreen mode

New Repo

gh repo create boostup/`basename "$PWD"`
Enter fullscreen mode Exit fullscreen mode

Stop tracking a file

git rm --cached <file>
Enter fullscreen mode Exit fullscreen mode

Download one file from any github repo

curl -o app.css https://raw.githubusercontent.com/angular-university/complete-typescript-course/section-7-6/client/src/assets/app.css
Enter fullscreen mode Exit fullscreen mode

Showing repo remote URLs

git remote -v
Enter fullscreen mode Exit fullscreen mode

Changing repo remote URLs

git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git
Enter fullscreen mode Exit fullscreen mode

Showing remote tracking

git remote show origin
Enter fullscreen mode Exit fullscreen mode

Setting remote tracking from local branch

git push --set-upstream origin <branch name>
Enter fullscreen mode Exit fullscreen mode

or

git branch --set-upstream-to origin/master
Enter fullscreen mode Exit fullscreen mode

Unsetting remote tracking by branch name

git config --unset branch.<branch name>.remote
git config --unset branch.<branch name>.merge
Enter fullscreen mode Exit fullscreen mode

Unsetting remote tracking from branch

git branch --unset-upstream
Enter fullscreen mode Exit fullscreen mode

Squasing commits

Follow instructions here

Finding commits where a file has been modified

git log --author=clark --stat <file path from project root>
Enter fullscreen mode Exit fullscreen mode

Finding remote merged branches

git branch -r --merged | grep origin | grep -Ev 'master|deamon'
Enter fullscreen mode Exit fullscreen mode

Note: (grep -v 'master|deamon' => exlcuding branches with names that contain the words 'master' or 'deamon')

To amend the previous commit

git add .
git commit --amend

git log --stat
Enter fullscreen mode Exit fullscreen mode

To change git password when Windows password changes

git config --global credential.helper wincred
Enter fullscreen mode Exit fullscreen mode

To remove untracked files / directories do:

git clean -fdx
Enter fullscreen mode Exit fullscreen mode

-f - force
-d - directories too
-x - remove ignored files too ( don't use this if you don't want to remove ignored files)

Show a file on a particular commit

git show REVISION:path/to/file
Enter fullscreen mode Exit fullscreen mode

See a graph of branch hierarchy

git log --all --graph --decorate --oneline --simplify-by-decoration
Enter fullscreen mode Exit fullscreen mode

Throwing out all of the changes not committed or pushed

git checkout -f 
or 
git reset --HARD
Enter fullscreen mode Exit fullscreen mode

Git Undo Last Commit (not yet pushed)

  • keeping changes
git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode
  • trashing changes
git reset --hard HEAD^
Enter fullscreen mode Exit fullscreen mode

Now if you already pushed and someone pulled which is usually my case, you can't use git reset. You can however do a git revert,

git revert HEAD
Enter fullscreen mode Exit fullscreen mode

This will create a new commit that reverses everything introduced by the accidental commit.

Create a new branch:

git checkout -b nombranche ( = git branch branchname + git checkout branchname )
Enter fullscreen mode Exit fullscreen mode

Pushing local branch and tracking its remote counterpart:

git push -u origin nombranche
Enter fullscreen mode Exit fullscreen mode

Display all local and remote branches:

git branch -a
Enter fullscreen mode Exit fullscreen mode

Display remote branch commits:

git log origin/nombranche
Enter fullscreen mode Exit fullscreen mode

Merging a branch into the build branch:

git checkout build
git pull origin build
git merge branchname
Enter fullscreen mode Exit fullscreen mode

Deleting a local branch:

git branch -d nombranche
Enter fullscreen mode Exit fullscreen mode

Deleting a remote branch:

git push origin :nombranche
ou 
git push origin --delete nombranche
Enter fullscreen mode Exit fullscreen mode

Removing staged and working :

git reset --hard directory changes
Enter fullscreen mode Exit fullscreen mode

Removing untracked files:

git clean -f -d 
Enter fullscreen mode Exit fullscreen mode

Get list of files with conflicts:

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

or

git ls-files -u

Display commits by user:

git log --author=authorname
Enter fullscreen mode Exit fullscreen mode

Diffing

git diff f98c5f277e28160ee5076e8ac4fabe7a60e8ca6f  --name-only
Enter fullscreen mode Exit fullscreen mode

Creating a new tag

git tag tagname
Enter fullscreen mode Exit fullscreen mode

Pushing the tag

git push origin branchname --tags
Enter fullscreen mode Exit fullscreen mode

Deleting a pushed tag

  • distant
git push origin :refs/tags/tagname
Enter fullscreen mode Exit fullscreen mode
  • local
git tag -d tagname
Enter fullscreen mode Exit fullscreen mode

Troubelshooting

Git asks me to commit or stash changes on checkout master, even though there are no changes

git checkout -- .
Enter fullscreen mode Exit fullscreen mode

Top comments (2)

Collapse
 
vlasales profile image
Vlastimil Pospichal

What is gh?

Collapse
 
boostup profile image
Fred B.

it is the GitHub command line tool