Skip to content

Instantly share code, notes, and snippets.

@klj613
Created September 4, 2012 16:12
Show Gist options
  • Save klj613/3622924 to your computer and use it in GitHub Desktop.
Save klj613/3622924 to your computer and use it in GitHub Desktop.
Git Tips

...

  • treeish: anything which references a commit (e.g. branch, HEAD, HEAD10, master15, SHA)

git show <treeish>:<file>

Displays file from a specific commit

git checkout <treeish> -- <file>

Checkout a file from a specific commit

e.g.

git checkout HEAD~1 -- foo.php - Checkout foo.php from the previous commit

git checkout -- foo.php - Checkout foo.php from the current commit

git checkout master -- foo.php - Checkout foo.php from master

git checkout -b <branch>

Same as:

git branch <branch>

git checkout <branch>

git add -i

Interactive mode add

git reset

Remove files from stage/index

git reset -- <file>

Remove specific file from stage/index

git reset --hard

Same as git reset but also re-checkout files which has been modified

git reset HEAD~1 --hard

Rollback one commit from where you are now (HEAD)

git reset <treeish>

Move your current HEAD (and branch) to a specific commit e.g. git reset origin/master --hard to move your local master back to origin/master. Note that --hard also resets your working directory

git stash; git stash apply

Stash your current changes and then get them back to the working copy. Whilst keeping the stash commit so if you mess up your working copy. You still got a stash copy!

Note: To stash new files, you must add them to the stage/index

Remove a file you accidentally committed

Remove file form the latest commit

git rm <file>

git commit --amend

If the commit is older than 1 commit ago..

git rebase <treeish>~1 -i Treeish being the commit the file was introduced on

select the appropriate commit and change to 'edit' (probably the top one)

git rm <file>

git commit --amend

git rebase --continue - You might have to resolve conflicts if the file has been modified since it was added to git.

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