This is a draft cheat sheet. It is a work in progress and is not finished yet.
Glossary
[value] |
Optional value |
value |
User-defined value |
<ref> |
SHA-1 reference to commit or tag |
<paths> |
List of paths/wildcards to match files |
|
Git vocabulary |
Init Repository
Create new git repository in current folder |
|
Create a copy of git repository by given url |
git clone https://repository.git
|
List remote repositories |
|
Create alias origin for remote repository |
git remote add origin https://repository.git
|
Change url of remote repository with alias origin |
git remote set-url origin https://repository-2.git
|
Manage remote repositories |
|
Branching
Create new branch |
git checkout -b branch-name [<commit>] // -B to override existing branch
|
Switch to existing branch |
git checkout branch-name
|
Remove branch |
git branch -d branch-name // Use -D or -df to force
|
Rename current branch |
git branch -m new-branch-name
|
List branches |
git branch --list [pattern] // -r (remote) -a (all) -v (show last commit) // --contains <commit> (contains specified commit) // --merged (can be safely removed) --no-merged (may need attention)
|
Create branch without commits history |
git checkout --orphan -b branch-name [<commit>]
|
More actions with branches |
git branch --help git checkout --help
|
|
|
Commit Changes
Add selected files to staging (prepare to commit) |
git add <paths> // '.' will match all unstaged files
|
Describe current state of local repository |
|
Make commit with message |
|
Make commit and append it to previous one |
|
Make commit and append it to previous one with new message |
git commit --amend -m "message"
|
Undo Changes
Get the previous version of a given files |
git checkout <commit> <paths> // HEAD is used implicitly if <commit> omitted
|
Revert certain commit by applying another one (to persist history) |
|
Unstage files |
|
Unstage all files |
|
Move state to specified commit |
git reset <commit> // --hard (to remove changes) --soft (to leave changes staged)
|
Set specified files to state of certain revision |
git checkout <commit> <paths>
|
Revisions
HEAD |
Reference to last commit in current branch |
<commit>^ |
First parent of selected commit |
<commit>^^ |
Second (and so on) parent of specified commit |
<commit>~n |
N-th parent of selected commit (e.g. ^2) |
<commit1>..<commit2> |
|
More git revision syntax here |
Use Cases
Remove all the local updates |
git reset --hard
git clean -f
|
|