I really, really like Git and in my opinion, it is the best version control system for developing code or writing LaTeX documents to track changes. There are a lot of UI tools out there that help you with your daily work. These tools are great, of course, but I personally like the old-school command prompt way mostly because I know exactly what I am doing (or better: I think I know what I am doing :D) and I can better learn and understand the processes. Sometimes, if I am not careful, I enter a wrong git command and make a mistake or I want to make some special things instead of the normal pull-commit-push way. These mistakes are mostly non-critical and the special things aren't so special at all, so a solution can easily be found by a quick google search.
These mistakes and special workflows often repeat and it feels very annoying to google the same thing again and again. That's the reason why I started to create a little Git Cheatsheet with explanations for the most important commands, mistakes and workflows. I know that there are a lot of Cheatsheets and solutions out there, but this is my personal version.
The most important Basic Commands
If you are using Git very often, these are the most important commands you should always know without looking them up:
git pull
- Pull new changes from the origin server to your local working copygit add .
- Track all changed, deleted, and created files (do this before your commit)git commit -m "Commit message"
- Creates a (local) commit with a commit messagegit push
- Pushes the changes (commits) to the origingit status
- Get some useful information (current branch, untracked / tracked files, ...)
I know that working with branches is pretty important, too, but if you are a single user on a small project, this may be sufficient. But I will talk about some branch commands later.
Commit related
How to remove all uncommitted changes?
If you want to reset your local working directory to the last commit and remove all local changes:
Possibility 1:
This is the possibility I like the most because it also works if there are some other Git tools running that creates an autostash
(e.g. GitKraken).
git stash
- Puts all uncommitted changes in a temporary spacegit stash drop
- Removes all files and folders from this temporary space
Reference: [1]
Possibility 2:
git reset --hard
- Resets all existing and tracked files and folders to the last commitgit clean -fdx
- Removes all untracked things from the working directory- Flag explanation:
-f
: forces the command,-d
: removes directories,-x
: removes ignored files
- Flag explanation:
Reference: [2], [3]
How to change a commit message?
How to change the commit message of the last commit?
Possibility 1: Commit executed locally, not pushed to the server:
git commit --amend -m "New message"
- Changes the commit message to a new message
If you prefer editing a long and / or multiline commit message in an editor, just use git commit --amend
How to get new changes from the origin server but keep local, uncommitted changes?
If you want to get the latest state from the origin, but keep your local, uncommitted changes, git pull
should do this if there aren't any conflicts at all (fast-forward).
If you know that there will be some conflicts, one possible solution could be:
git stash
- Put all changes into a temporary space and removes them from the current working directorygit pull
- Get all new commits and changes from the origingit stash pop
- Put the changes from the temporary space back into the working directory (after this, the conflicts must be handled)
Reference: [4]
Important: These steps work, but they aren't the correct way to get the new changes while keeping local changes. Dustin described this pretty well in his StackOverflow answer. He says that the first thing before pulling changes into a working directory with uncommitted changes is creating a commit. This commit mustn't be pushed to the origin, but this is the more secure way if some conflict-solving strategies don't work. Just think of problems that occur in step 3: In this case, you will mess up your local work.
How to undo an unpushed commit but keep the changes?
Just one command to remove the last commit while keeping the changes in this commit:
git reset HEAD~1 --soft
- Creates a soft reset
Reference: [5]
Branch related
How to create a new Branch while keeping changes?
To create a new branch, put all the changed and uncommitted files and folders to this new branch and leave the old branch as it. Remember to push this branch to the origin (if you want):
git checkout -b BRANCHNAME
- Create a local branchgit push -u origin BRANCHNAME
- Push the branch to the origin
How to switch Branches?
To switch to another branch (yes, this should go into the "The most important basic commands" section above) use git checkout
:
-
git checkout BRANCHNAME
- "Typical" switching to branchBRANCHNAME
- Remember that the uncommitted changes come with you when executing the
git checkout
command. If you don't want this, have a look at the next question.
- Remember that the uncommitted changes come with you when executing the
-
git checkout -f BRANCHNAME
- Force branch switching toBRANCHNAME
(Caution: This will throw away your local, uncommitted changes)
How to switch / checkout Branches without taking the uncommitted work to the new Branch?
When using git checkout
, the uncommitted changes will be transferred to the new branch. There could be some scenarios where this isn't wanted, for example, if you work on branchA
and just need to make a little change on branchB
but want to work on brnachA
just afterward. In this case:
- Use
git stash
in yourbranchA
to save the changes in a temporary space - Switch into
branchB
withgit checkout branchB
- (Make what you want on
branchB
) - Go back to
branchA
withgit checkout branchA
- Get the changes back from the temporary space with
git stash pop
Reference: [6]
How to delete a Branch?
If you don't need a branch anymore, use:
git branch -d BRANCHNAME
- Deletes the local branchBRANCHNAME
git push origin --delete BRANCHNAME
- Deletes the branchBRANCHNAME
from the origin
Tag related
Sometimes you want to work with tags to keep track of version or relevant milestones.
How to create a tag?
To create a tag:
git tag TAGNAME
- Creates a tag calledTAGNAME
- To add a description to the tag, use
git tag TAGNAME -m "My tag description"
- To add a description to the tag, use
git push origin TAGNAME
- Pushes the tagTAGNAME
to the origin- To push all tags, use
git push origin --tags
- To push all tags, use
References
Most information comes from the original Git Reference. Some special tips and tricks are collected from these great answers:
[1] https://stackoverflow.com/a/17719386, answer by glumgold
[2] https://stackoverflow.com/a/4630316, answer by Benjamin Bannier
[3] https://stackoverflow.com/a/5812972, answer by htanata
[4] https://stackoverflow.com/a/10416070, answer by GoZoner
[5] https://stackoverflow.com/a/19859644, answer by Isantipov
[6] https://stackoverflow.com/a/7218106, answer by Bill Door