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