Skip to content

Instantly share code, notes, and snippets.

@andykuszyk
Last active April 20, 2016 08:37
Show Gist options
  • Save andykuszyk/4009ef2b36d3e9d9b4ff to your computer and use it in GitHub Desktop.
Save andykuszyk/4009ef2b36d3e9d9b4ff to your computer and use it in GitHub Desktop.
Everyday Git Commands

Everyday Git Commands

This document is just a collection of everyday commands that I use to perform common Git operations.


Checking out a remote branch

To checkout a remote branch into a new local branch that tracks the remote equivilant:

git checkout -t origin/[remote-branch-name]

This does the equivilant of git checkout -b but also creates a new local branch called remote-branch-name which tracks its remote equivlant.


Selectively stage files and hunks of files (patching)

To work through all the changed files in your working directory and stage files, or parts of files, use:

git add -i

This gives you an "interactive" adding app, which allows you to stage parts of files on a change by change (or line by line basis). It also allows you to see the diffs of these files.


See the difference between a staged file and the working directory

Once files, or parts of files, are staged, you can see the difference between the index (the staged changesets) and the working directory using:

git diff --cached path/to/filename

This is especially useful if you have staged parts of files and want to quickly review those changes before commiting the changes.


Discard the unstaged changes in the working directory

To overwrite any unstaged changes in the working directory, use:

git checkout -- .

Don't forget the full stop at the end.


Revert a previous commit to the index

To revert a previous commit to the current index so that you can review reverting this change before committing the revert, use:

git revert --no-commit [commit-hash]

If you want to un-stage these changes and manually re-add them to the index before committing, use:

git reset

This will remove all staged changes from the index ready for re-staging.


Commit & stage everything in the working directory at once

If you want to stage and commit everything in the working directory, this can be done in one command as follows:

git commit -am "[message]"


Undo the last commit

If the last commit was made in error, you can effectively undo it by returning all the changes to the working directory with:

git reset HEAD~1


Apply another commit's changes to the index

If there is another commit that has a changeset in that you want to apply to your current working directory, you can apply it to the index with:

git cherry-pick [commit-hash] --no-commit


Stash changes that aren't staged, to see how staged changes would work as a commit

If you want to temporarily take un-staged changes out of the picture, so that you can see how the code works with just the staged changes, you can stash un-staged changes with:

git stash save --keep-index


Remove staged files from the index

To return staged changes to the working directory, use:

git reset

or

git reset [file]


Delete a branch from a remote repo

To remove a branch from a remote repo, use:

git push origin :[branch-name]


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