2020-08-11 01:47:54 +02:00
|
|
|
# Git cheat sheet
|
2017-06-21 21:06:09 +02:00
|
|
|
|
2017-11-26 15:25:01 +01:00
|
|
|
See also [fixing commits][fix-commit]
|
2017-06-21 21:01:10 +02:00
|
|
|
|
2020-08-11 01:47:54 +02:00
|
|
|
## Common commands
|
2017-11-26 15:25:01 +01:00
|
|
|
|
|
|
|
- add
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git add foo.py`
|
2017-11-26 15:25:01 +01:00
|
|
|
- checkout
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git checkout -b new-branch-name`
|
|
|
|
- `git checkout main`
|
|
|
|
- `git checkout old-branch-name`
|
2017-11-26 15:25:01 +01:00
|
|
|
- commit
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git commit -m "topic: Commit message title."`
|
|
|
|
- `git commit --amend`: Modify the previous commit.
|
2017-11-26 15:25:01 +01:00
|
|
|
- config
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git config --global core.editor nano`
|
|
|
|
- `git config --global core.symlinks true`
|
2017-11-26 15:25:01 +01:00
|
|
|
- diff
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git diff`
|
|
|
|
- `git diff --cached`
|
|
|
|
- `git diff HEAD~2..`
|
2017-11-26 15:25:01 +01:00
|
|
|
- fetch
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git fetch origin`
|
|
|
|
- `git fetch upstream`
|
2017-11-26 15:25:01 +01:00
|
|
|
- grep
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git grep update_unread_counts`
|
2017-11-26 15:25:01 +01:00
|
|
|
- log
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git log`
|
2017-11-26 15:25:01 +01:00
|
|
|
- pull
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git pull --rebase`: **Use this**. Zulip uses a [rebase oriented workflow][git-overview].
|
|
|
|
- `git pull` (with no options): Will either create a merge commit
|
|
|
|
(which you don't want) or do the same thing as `git pull --rebase`,
|
|
|
|
depending on [whether you've configured Git properly][git-config-clone]
|
2017-11-26 15:25:01 +01:00
|
|
|
- push
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git push origin +branch-name`
|
2017-11-26 15:25:01 +01:00
|
|
|
- rebase
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git rebase -i HEAD~3`
|
|
|
|
- `git rebase -i main`
|
|
|
|
- `git rebase upstream/main`
|
2017-11-26 15:25:01 +01:00
|
|
|
- reflog
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git reflog | head -10`
|
2017-11-26 15:25:01 +01:00
|
|
|
- remote
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git remote -v`
|
2017-11-26 15:25:01 +01:00
|
|
|
- reset
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git reset HEAD~2`
|
2017-11-26 15:25:01 +01:00
|
|
|
- rm
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git rm oops.txt`
|
2017-11-26 15:25:01 +01:00
|
|
|
- show
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git show HEAD`
|
|
|
|
- `git show HEAD~~~`
|
|
|
|
- `git show main`
|
2017-11-26 15:25:01 +01:00
|
|
|
- status
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git status`
|
2017-11-26 15:25:01 +01:00
|
|
|
|
2020-08-11 01:47:54 +02:00
|
|
|
## Detailed cheat sheet
|
2017-06-21 21:01:10 +02:00
|
|
|
|
|
|
|
- add
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git add foo.py`: add `foo.py` to the staging area
|
|
|
|
- `git add foo.py bar.py`: add `foo.py` AND `bar.py` to the staging area
|
|
|
|
- `git add -u`: Adds all tracked files to the staging area.
|
2017-06-21 21:01:10 +02:00
|
|
|
- checkout
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git checkout -b new-branch-name`: create branch `new-branch-name` and switch to/check out that new branch
|
|
|
|
- `git checkout main`: switch to your `main` branch
|
|
|
|
- `git checkout old-branch-name`: switch to an existing branch `old-branch-name`
|
2017-06-21 21:01:10 +02:00
|
|
|
- commit
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git commit -m "commit message"`: It is recommended to type a
|
|
|
|
multiline commit message, however.
|
|
|
|
- `git commit`: Opens your default text editor to write a commit message.
|
|
|
|
- `git commit --amend`: changing the last commit message. Read more [here][fix-commit]
|
2017-06-21 21:01:10 +02:00
|
|
|
- config
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git config --global core.editor nano`: set core editor to `nano` (you can set this to `vim` or others)
|
|
|
|
- `git config --global core.symlinks true`: allow symbolic links
|
2017-06-21 21:01:10 +02:00
|
|
|
- diff
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git diff`: display the changes you have made to all files
|
|
|
|
- `git diff --cached`: display the changes you have made to staged files
|
|
|
|
- `git diff HEAD~2..`: display the 2 most recent changes you have made to files
|
2017-06-21 21:01:10 +02:00
|
|
|
- fetch
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git fetch origin`: fetch origin repository
|
|
|
|
- `git fetch upstream`: fetch upstream repository
|
2017-06-21 21:01:10 +02:00
|
|
|
- grep
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git grep update_unread_counts static/js`: Search our JS for references to update_unread_counts.
|
2017-06-21 21:01:10 +02:00
|
|
|
- log
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git log`: show commit logs
|
|
|
|
- `git log --oneline | head`: To quickly see the latest ten commits on a branch.
|
2017-06-21 21:01:10 +02:00
|
|
|
- pull
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git pull --rebase`: rebase your changes on top of `main`.
|
|
|
|
- `git pull` (with no options): Will either create a merge commit
|
|
|
|
(which you don't want) or do the same thing as `git pull --rebase`,
|
|
|
|
depending on [whether you've configured Git properly][git-config-clone]
|
2017-06-21 21:01:10 +02:00
|
|
|
- push
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git push origin branch-name`: push you commits to the origin repository _only if_ there are no conflicts.
|
|
|
|
Use this when collaborating with others to prevent overwriting their work.
|
|
|
|
- `git push origin +branch-name`: force push your commits to your origin repository.
|
2017-06-21 21:01:10 +02:00
|
|
|
- rebase
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git rebase -i HEAD~3`: interactive rebasing current branch with first three items on HEAD
|
|
|
|
- `git rebase -i main`: interactive rebasing current branch with `main` branch
|
|
|
|
- `git rebase upstream/main`: rebasing current branch with `main` branch from upstream repository
|
2017-06-21 21:01:10 +02:00
|
|
|
- reflog
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git reflog | head -10`: manage reference logs for the past 10 commits
|
2017-06-21 21:01:10 +02:00
|
|
|
- remote
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git remote -v`: display your origin and upstream repositories
|
2017-06-21 21:01:10 +02:00
|
|
|
- reset
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git reset HEAD~2`: reset two most recent commits
|
2017-06-21 21:01:10 +02:00
|
|
|
- rm
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git rm oops.txt`: remove `oops.txt`
|
2017-06-21 21:01:10 +02:00
|
|
|
- show
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git show HEAD`: display most recent commit
|
|
|
|
- `git show HEAD~~~`: display third most recent commit
|
|
|
|
- `git show main`: display most recent commit on `main`
|
2017-06-21 21:01:10 +02:00
|
|
|
- status
|
2021-08-20 22:54:08 +02:00
|
|
|
- `git status`: show the working tree status, unstaged and staged files
|
2017-06-16 16:03:51 +02:00
|
|
|
|
2019-09-30 19:37:56 +02:00
|
|
|
[fix-commit]: fixing-commits.md
|
2022-02-16 01:39:15 +01:00
|
|
|
[git-config-clone]: cloning.md#step-1b-clone-to-your-machine
|
2022-02-24 00:17:21 +01:00
|
|
|
[git-overview]: overview.md
|