I am Deborah.
Check out my work
or read my blog.

git cheat sheet

Some commands I repeatedly had to lookup when dealing with git and github.

View history [source]

  • Simple log:git log
  • Last two commits:git log -2
  • Last two commits with changes:git log -p -2
  • commit list with hash:git log --pretty=oneline

Committing changes

What has been changed?

  • unstaged files:diff file
  • staged files:diff --staged file

Add only parts of file to stage

  • Interactive patching of file [source] git add --patch file
  • When using the above command, the hulks are somethimes too big and the edit hulk option too difficult to handle. I therefore prefer git gui [source]:sudo apt-get install git-guijust run this command before staging changes:git gui

Undo changes, remove changes, reset files..

Remove file from stage but keep changes [source]

  • git reset HEAD file

Reset all changes since last commit

  • git reset --hard HEAD

Reset all changes since second last commit

  • git reset --hard HEAD~1

Restore added changes removed with the above command [source]

  • git reset --hard HEAD@{1}


Ignore files only locally without using .gitignore [source]

To ignore files without adding them to a possibly syncronised .gitignore file, add the files to info/exclude.

Add tracked files to .gitignore [source]

  • git rm --cached path/file
    echo 'path/file' >> .gitignore

Modify commits

Split previous commit [source]

  • git rebase -i HEAD~3..where 3 is the number of commits you have to go back for the commit you want to split. The number can also be higher. You get into an edit mode where you see the last commits with the prefix “pick”. Change that to “edit” for the commit you want to split.
  • git reset HEAD~
  • Commit multiple separate changes
  • git rebase --continue

Add changes to older commit [source]

  • git add my_fixed_files
    git commit --fixup=OLDCOMMIT
    git rebase --interactive --autosquash OLDCOMMIT^

Branches / forks

Renaming branch [source]

  • git branch -m old_branch new_branch
    git push origin :old_branch
    git push --set-upstream origin new_branch

Adding remote information to fork (before syncing) [source]

  • git remote add upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git
  • Check with git remote -v

Syncing a fork [source]

  • git fetch upstream
    git checkout master
    git merge upstream/master
    git push

Working with remote branch of coworker [source]

  • Pull branch:git remote add coworker git://path/to/coworkers/repo.git
    git fetch coworker
    git checkout --track coworker/foo
  • Update branch:git checkout foo
    git pull

Creating branch with current staged changes [source]

  • Unstage added changes:git reset --soft HEAD~1
  • Create new branchgit checkout -b newBranch

Deleting branches [source]

  • Local branches:git branch -d the_local_branch
  • Remote branches:git push origin :the_remote_branch

Say something

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Posts from "Blog":

How to print (almost) invisible IR markers using a standard printerQuick, Draw! Generic drawings part 2The Art of CodingImperfect MandalasAutumn LeafsColor Explosion #25 years of KAZOOSH!Color explosionUbuntu 15.10, MongoDB and node.jsKaleidoscope of Fencinggit cheat sheetFractalsofxSortableListCrocket: hairband with jeans buttonWall painting for Marias birthdayLinux / Ubuntu + Serato Vinyl Control System + Pure DataWicket-Bootstrap – Navbar and Pageparameters – setting the active itemLinux Console SnippetsCrawling Traces @German award for multimedia mb21Crawling Traces History ViewerStreaming new.livestream.com with VLC and UbuntuCrawling Traces at Hechtfest / hechtgruenLinks: Data Security, Social Networks and Secure MailingFix: VMware Player crash on Ubuntu 13.04Xubuntu: Fix for Dockbarx bug not starting Thunar or TerminalUbuntu 12.10, libwebsockets and OFSetting up Meteor with Ubuntu 12.10 on an NTFS share partitionWebsite: KAZOOSH!Intermediate IsolinesThe barn door gets coloredSkype on Ubuntu 10.10 or 11.04 64bitElitebook 8440p vs UbuntuKryptografie & Datensicherheit – NotizenRandom Compilation IIRandom Compilation IAll You Can Eat in DresdenWebauftritt: ESE 2010