cheat-sheet.md: Fix 'Incorperate' -> 'Incorporate' typo
[swc-version-control-git.git] / cheat-sheet.md
1 Configuring
2 ===========
3
4 * Tell Git who you are:
5
6         $ git config --global user.name 'User Name'
7         $ git config --global user.email 'user@email.com'
8
9 * Customize the user interface:
10
11         $ git config --global core.editor nano
12         $ git config --global color.ui auto
13
14 Basic use
15 =========
16
17 * Create a local repository from a public repository at `$URL`:
18
19         $ git clone $URL
20
21 * Stage a (possibly new) file (or files) for the next commit:
22
23         $ git add $FILES
24
25 * Remove a file (or files) with the next commit:
26
27         $ git rm $FILES
28
29 * Commit any local changes to the local repository:
30
31         $ git commit -a -m "$MESSAGE"
32
33 * Push any local commits to a public repository (e.g. `origin`):
34
35         $ git push origin
36
37 Merging
38 =======
39
40 * Update your local view of a public repository (e.g. `origin`):
41
42         $ git fetch origin
43
44 * Merge new commits from another branch (e.g. the `origin/master`
45   branch tracking the `master` branch of the `origin` repository):
46
47         $ git merge origin/master
48
49 * Mark conflicts as resolved:
50
51         $ git add $FILES
52
53 * Instead of merging `origin/master` into your local branch, you can
54   rebase your local branch onto `origin/master` (useful options:
55   `-i`):
56
57         $ git rebase origin/master
58
59 Comparing changes
60 =================
61
62 * Compare the current working directory to the staging area:
63
64         $ git diff
65
66 * Compare the staging area to the last commit:
67
68         $ git diff --cached
69
70 * Compare the current working directory to the last commit:
71
72         $ git diff HEAD
73
74 Browsing history
75 ================
76
77 * Check your local state:
78
79         $ git status
80
81 * View past commits (useful options: `--oneline`, `--graph`,
82   `--decorate`, `--stat`, …):
83
84         $ git log
85
86 * Who wrote this line, and what were they thinking?
87
88         $ git blame $FILES
89         $ git show $COMMIT
90
91 Recovering old versions
92 =======================
93
94 * Undo local changes to `$FILES` since the last commit:
95
96         $ git checkout $FILES
97
98 * Checkout `$FILES` as they were during `$COMMIT`:
99
100         $ git checkout $COMMIT -- $FILES
101
102 * Create a new commit backing out changes made by an old commit:
103
104         $ git revert $COMMIT
105
106 Setting up a repository
107 =======================
108
109 * Create a new repository in the current directory:
110
111         $ git init
112
113 Provenance
114 ==========
115
116 * Tag releases:
117
118         $ git tag v1.2.3
119         $ git push --tags origin
120
121 * Incorporate the tags into any releases.  For example, the Git
122   project extracts [version information][git-version-gen] when you
123   [compile the project][git-makefile].  Most Python projects just
124   increment `package.__version__` by hand
125   (e.g. [pygit2][pygit2-version]).
126
127 Branches
128 ========
129
130 * What branch am I on?  What other branches are there?
131
132         $ git branch
133
134 * Make a new branch (e.g. `some-feature`):
135
136         $ git branch some-feature
137
138 * Change the current branch (e.g. to `some-feature`) and update the
139   staging area and working directory:
140
141         $ git checkout some-feature
142
143 Remotes
144 =======
145
146 * List configured remotes (URL nicknames):
147
148         $ git remote
149
150 * List configured remotes with push/pull URLs:
151
152         $ git remote -v
153
154 * Add a new remote:
155
156         $ git remote add $NAME $URL
157
158 Manipulating history
159 ====================
160
161 * Squash new changes onto the most recent commit:
162
163         $ git commit --amend …
164
165 * Move the current branch to `$COMMIT` without touching the staging
166   area or working directory:
167
168         $ git reset $COMMIT
169
170 * Move the current branch to `$COMMIT` while also resetting the
171   staging area and working directory:
172
173         $ git reset --hard $COMMIT
174
175
176 [git-version-gen]: http://git.kernel.org/cgit/git/git.git/tree/GIT-VERSION-GEN
177 [git-makefile]: http://git.kernel.org/cgit/git/git.git/tree/Makefile
178 [pygit2-version]: https://github.com/libgit2/pygit2/blob/master/pygit2/version.py