This document is just a collection of everyday commands that I use to perform common Git operations.
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.
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.
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.
To overwrite any unstaged changes in the working directory, use:
git checkout -- .
Don't forget the full stop at the end.
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.
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]"
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
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
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
To return staged changes to the working directory, use:
git reset
or
git reset [file]
To remove a branch from a remote repo, use:
git push origin :[branch-name]