Essential Git Commands
Here are several git commands I keep having to Google. This is either due to barely using them, or just them not sinking in. Hopefully this list will help you out as well!
Create a New Branch and Move into It
Very simple, but took me a while to remember! Create a new branch and move into it with one command.
git checkout –b branch-name
Merging and Upstream Repository into your Local Branch
You have forked someone else’s repo to make an update and provide a pull request. Whilst working on your patch other changes have been made and pushed to the master. This then puts your branch out of sync.
To make sure you are always up to date, you need to merge the most recent changes.
Firstly, move into your local branch
git checkout [your-branch]
Then grab the master branch from origin, and set up as a branch on your local.
git remote add upstream git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
Now get all the commits
git fetch upstream git pull upstream master
Then finally merge the master branch into your branch
git merge master
Undo Last Local Commit
You have just pushed a commit, and then realise that it wasn’t quite right. You could send another commit to fix it, but it is faster and cleaner to undo this.
Preserve Local Changes
git reset --soft HEAD~1
Delete Local Changes
git reset --hard HEAD~1
Delete Multiple Local Commits
What happens when you have committed 3 or 4 changes, then realised you have gone in completely the wrong direction? Don’t want to be undoing the last commit over and over.
git rebase -i HEAD~3
Change out the 3 for any number of commits.
Delete Remote Commits
Did you make a mistake and then push to the remote? Not a problem. Just be careful with this command as you will overwrite your remote commits and could break the world.
After rolling back your local commits (using the above commands), do the following:
git push -f origin [branch-name]
This forces the push over the existing remote branch.
Reset File to a Certain Point
You have been working on a new piece of code, and have decided that you want to reset a single file back to a certain point, but leave everything else as you have built.
git checkout [commit hash] -- [filename]
This only changes that one selected file.
Squash last n commits into one commit
Working on a large project? It is a nightmare to go through multiple small commits to see progress. If there are lots of small commits you could turn to one, use the following command.
git rebase -i HEAD~5 git reset --soft HEAD~5 git add . git commit -m "Update" git push -f origin master
Need to Move to Different Work? Stash it!
When you need to move to a different part of the project while still working on the current branch, use:
Checkout a Forked Branch
Working on an open-source project? Want to check that someones code is working as expected? Then you will need to pull it onto your local and test it.
Firstly you need to add as a new remote, then fetch the remove, and then check out that particular branch.
git remote add coworker git://path/to/coworkers/repo.git git fetch coworker git checkout --track coworker/foo
Another great site for git pointers is: https://rogerdudler.github.io/git-guide/. It’s quite pretty as well!