Git_recipes.md 15.1 KB
Newer Older
1
This page is made to give useful command or succession of commands to perform Git tasks in the context of the [CamiTK Git workflow](https://camitk.gricad-pages.univ-grenoble-alpes.fr/Docs/Getting%20Started/CamiTK%20Overviews/CamiTK_Git_Branching_Model/).
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
2

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
3
Be sure to follow both:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
4

5
- The [branch naming convention](https://camitk.gricad-pages.univ-grenoble-alpes.fr/Docs/Getting%20Started/CamiTK%20Overviews/CamiTK_Git_Branching_Model/#naming-conventions)
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
6

7
<!--
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
8
- The [commit message convention](./Core_developper_guidelines#Commit_messages)
9
-->
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
10

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
11
## Basic source Code Management using Git
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
12

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
13
### Clone the CamiTK repository and switch to develop branch
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
14

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
15
#### Clone to contribute
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
16

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
17
If you want to share your work, you first need to create an account on <https://gricad-gitlab.univ-grenoble-alpes.fr/>. Let’s say that we want to clone the CamiTK repository in a local directory named “src”.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
18
19
20
21
22
23
24
25
26
27
28

``` bash
# R/W clone
git clone git@gricad-gitlab.univ-grenoble-alpes.fr:CamiTK/CamiTK.git ./src

# Specify that every new push will be performed to the main repository
git config push.default current

# Now you are on the "develop" branch, ready to create your own branch
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
29
#### Clone to review
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
30
31
32
33
34
35
36
37
38
39

If you just want to check the current state of the CamiTK code, you can
clone the repository anonymously in a local directory name “src”:

``` bash
# anonymous clone
git clone https://gricad-gitlab.univ-grenoble-alpes.fr/CamiTK/CamiTK.git ./src
# You only have the develop branch
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
40
### Visualize Git work tree under Linux
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60

Under Linux use qGit or git-cola

get qgit:

``` bash
sudo apt-get install qgit
qgit
```

If your distrib does not have the package yet, get the source code at
<https://github.com/feinstaub/qgit> .

Otherwise you can do this for git-cola :

``` bash
sudo apt-get install git-cola
git-cola
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
61
### Visualize the git history on the web
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
62
63
64

You can check the latest push on origin here:

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
65
66
- [the CamiTK develop branch    summary](https://gricad-gitlab.univ-grenoble-alpes.fr/CamiTK/CamiTK/network/develop)
- [the list of current     branches](https://gricad-gitlab.univ-grenoble-alpes.fr/CamiTK/CamiTK/branches)
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
67

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
68
### Set VIM as default message editor
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
69
70
71
72
73
74

``` bash
git config --global core.editor "vim"
export GIT_EDITOR=vim
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
75
### List all local branches and display last modified dates
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
76

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
77
From [stackoverflow](http://stackoverflow.com/questions/2514172/listing-each-branch-and-its-last-revisions-date-in-git)
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
78
79
80
81
82

``` bash
for k in `git branch | sed s/^..//`; do echo -e `git log -1 --pretty=format:"%Cgreen%ci %Cblue%cr%Creset" $k --`\\t"$k";done | sort
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
83
## Current development
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
84
85

Two types of development process are explained in this section:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
86
87
- develop something new: you need to create a new feature
- fix a bug on the current development branch
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
88

89
The only thing that will change is how you name the local branch. Be sure to follow these [rules](https://camitk.gricad-pages.univ-grenoble-alpes.fr/Docs/Getting%20Started/CamiTK%20Overviews/CamiTK_Git_Branching_Model/#Naming_conventions) for naming your branch.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
90

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
91
### Develop a new feature during the develop process
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
92

93
You need to create a specific local branch (see below, [Create a branch](https://camitk.gricad-pages.univ-grenoble-alpes.fr/Docs/Getting%20Started/Examples%20and%20Tutorials/Git_recipes/#create-a-branch-locally) and follow the rules for naming your branch: the branch should be named *feature/XXX* where XXX is a comprehensible (although rather short) description (e.g., *feature/connect-to-apple-store*).
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
94
If the branch aim is to fulfil a feature, then "closes #XXX" should be mentioned in the branch description, where `#XXX` is the issue ID. This ensure a link from your branch to the issue (and will automatically closes the issues when your branch is merged to `develop`)
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
95

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
96
### Fix a Bug during the develop process
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
97

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
98
Just proceed like for a new feature, with the difference that it should be prepend with `bug/` instead of `feature/`.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
99

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
100
### Create a branch (locally)
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
101

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
102
As you are on the develop branch, the most usual case to work with Git is to do the job on a separate branch called feature branch creates from develop branch.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
103

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
104
1.  Choose a proper name for your branch (see [here](https://camitk.gricad-pages.univ-grenoble-alpes.fr/Docs/Getting%20Started/CamiTK%20Overviews/CamiTK_Git_Branching_Model/#naming-conventions))
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
105
106
2.  Create the new branch and switch to the new branch:

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
107
108
```bash
git checkout -b feature/add-google-voice-command origin/feature/add-google-voice-command
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
109
110
111
112
```

You can add a detailed description to your branch with:

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
113
```bash
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
114
115
116
git branch --edit-description
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
117
118
!!! note
    that you can custom “git branch” to show your description, see [this stackover flow entry](http://stackoverflow.com/a/16710084)).
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
119

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
120
### Request a merge of (finished) feature
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
121
122
123

In the CamiTK workflow, a finished feature should be merged through a
merge request on gitlab. There are two steps to create a merge request:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
124
125
126
127
128
1. push your local branch to origin
2. create a merge request

!!! Note
This is greatly simplified by using gitlab. TODO: update this documentation.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
129
130
131

``` bash
# make sure you are on the correct branch
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
132
git checkout feature/occulus-viewer
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
133
134
135
136
137
138
139

# push your local branch (warning: you have to definie the push.default, see section "Clone_to_contribute" on this page
git push
# git push your branch to the main CamiTK repository (origin) and provides a http link to start a merge request
# just click on the link to open the merge request web page and submit your merge request
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
140
Follow up the discussion on the merge request to complete the actions required by the CamiTK team.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
141

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
142
!!! Warning
143
**Do not forget to delete the local *feature/new-feature* after the merge is accepted (see below)**
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
144

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
145
### Delete a branch after the merge-request was accepted
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
146

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
147
To delete a specific branch (e.g., `bug/compilation-on-minitel`):
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
148
149

``` bash
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
150
git branch -d bug/compilation-on-minitel
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
151
152
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
153
154
!!! Note
    if you did not commit all your modification, there will be a message (you can then decide if you want to remove all and force the deletion using the `-D` option instead).
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
155
156
157
158
159
160
161

To delete all local branch that has been merged on origin:

``` bash
git branch --merged | egrep -v "(^\*|master|dev)" | xargs git branch -d
```

162
### Working on a feature branch
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
163

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
164
165
To version your code, do not hesitate to commit to your branch as often as possible (and to push it so that your changes can appear in the merge request for review or sharing).
When you push, the gitlab-ci system is automatically started unless you add `[ci skip]` or `[skip ci]` to the commit message.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
166
167


Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
168
169
170
#### Updating the git local index (staging new content)

You don’t need to perform an “add” command if the only thing you have done is to modify an already versionned file(s). To add a new single document you created:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
171
172
173
174
175

``` bash
git add myDocument.cpp
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
176
To add a set of documents created in the current directory. For example, let’s say that you have created a file called “myFile.cpp” plus a dir called “testDir” and files in it. This command will stage all the new files and dir in the current dir.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
177
178
179
180
181
182
183

``` bash
git add .
```

You can replace the “.” with a relative or absolute path.

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
184
#### Committing changes and adds
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
185
186
187
188
189
190
191

To commit all these changes loccaly, we perform a commit:

``` bash
git commit -m "meaningful commit message"
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
192
193
194
Please, be sure to read this [great documentation](https://chris.beams.io/posts/git-commit/) on how writting a good commit message.


Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
195
196
197
198
199
200
201
#### Pushing the changes to origin (gitlab server)

When you push, the gitlab-ci system is automatically started unless you add `[ci skip]` or `[skip ci]` to the commit message.
``` bash
git push
```

202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
#### Updating a feature branch that have conflicts with current develop

Sometimes your feature branch will be to distant to `develop`. In this case, gitlab will complain and say that you can not merge 
your feature branch in `develop`.
In this case **DON'T FOLLOW** the instructions given by gitlab. It is better to do the following steps :

```bash
# First update your local develop branch
git checkout develop
# Get the current remote version 
git fetch -p origin
# Merge the changes from `origin/develop` into your local `develop` branch. Your local `develop` branch will then be in sync with the remote repository
git merge origin/develop
# Check out your feature branch
git checkout feature/new-feature
# Merge the updated `develop` into your feature branch to include the latest changes
git merge develop
```

Now you can solve the conflicts. Note for Linux user, we recommand using a GUI merge tool to help to solve the conflicts, for instance
`git mergetool --tool=kdiff3`
This only updates the local feature branch. Then, you need to:

- check all the files (some automatic merges might *not be what you want*)
- if needed, add and commit your changes (e.g. `git add .; git commit -m "Solved merge conflicts"`)
 
Push you branch to remote:
```
git push origin feature/new-feature
```

Gitlab should start the CI pipeline and remove the merge conflict warnings.

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
235
## Release
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
236

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
237
### Create a specific release branch
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
238

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
239
We work only on the shaping of the release, only bug corrections, no new feature.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
240
241
242

``` bash
git checkout develop
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
243
git branch release/4.0.0
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
244
245
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
246
### Release a new CamiTK version
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
247

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
248
249
Once the release is ready, follow the three following steps.
1) push release branch on origin code
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
250
251
252
253
254

``` bash
git push
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
255
256
2) Create a merge request on **master**.
Use the gitlab web interface to start a new merge request on **master** (be careful to **select the master branch as the target to merge into**!)
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
257

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
258
259
3) Create a tag corresponding to the release number.
When the merge is accepted, do not forget to create a new tag with the release number directly on the gitlab web interface (e.g. “4.0.0”)
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
260

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
261
262
263
4) Create a merge request on **develop**.
Use the gitlab web interface to start a new merge request on **develop** in order to push the modifications back to develop branch **Do not forgot this step** as bug
fixes done in the release branch should be pushed on **develop** too!
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
264

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
265
5) Increment the CamiTK version number in the top level CMakeLists.txt
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
266

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
267
6) Delete the release branch on origin as well as locally
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
268
269

``` bash
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
270
271
git branch -d release/4.0.0
git push origin --delete release/4.0.0
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
272
273
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
274
### Fix a Bug during the release process
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
275

276
You are in the release process on the release branch and your are correcting a bug: just follow the [CamiTK Branching - Naming convention](https://camitk.gricad-pages.univ-grenoble-alpes.fr/Docs/Getting%20Started/CamiTK%20Overviews/CamiTK_Git_Branching_Model/#naming-conventions) as described by the picture in [depot referentiel](https://camitk.gricad-pages.univ-grenoble-alpes.fr/Docs/Getting%20Started/CamiTK%20Overviews/CamiTK_Git_Branching_Model/#working-with-depot).
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
277

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
278
If you fixed an important bug that you want to commit immediately to the develop branch for some reasons, follow the steps below to make your corrections available in the develop branch without waiting the end of release process.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
279

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
280
Verify that all work is commited on the release branch. If everything is ok do the following:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
281
282
283
284

1.  push your local branch to origin
2.  create a merge request on develop

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
285
Follow up the discussion on the merge request to complete the actions required by the CamiTK team.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
286
287
288

But, **don’t delete the branch** after the merge request was done.

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
289
### Try a release
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
290

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
291
If a release is getting prepared on origin (that is if you are working with some other developer on a specific release and this developer already pushed some changes on the release branch on origin), this is how you get a local copy to try it on:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
292
293
294

``` bash
git pull
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
295
git checkout -b release/4.0.0 origin/release/4.0.0
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
296
297
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
298
## Hotfix
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
299

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
300
### Create a specific release branch
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
301

302
From the master branch create a new hotfix branch by respecting the [branch naming convention](https://camitk.gricad-pages.univ-grenoble-alpes.fr/Docs/Getting%20Started/CamiTK%20Overviews/CamiTK_Git_Branching_Model/#naming-conventions).
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
303
304
305
306
307

``` bash
git checkout -b hotfix-4.0.1 master # Switched to a new branch "hotfix-4.0.1"
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
308
### Release a new CamiTK hotfix version
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
309

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
310
Make your strategic corrections, committing your modifications as you go.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
311
312
313

Once the release is ready, follow the three following steps.

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
314
1) Release code on master branch and add tag:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
315

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
316
317
318
    1.1. push your local branch to origin
    1.2. create a merge request on **master**
    1.3. Follow up the discussion on the merge request to complete the
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
319
    actions required by the CamiTK team.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
320
    1.4 Create a tag directly using Gitlab interface
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
321
322
323

**don’t delete the branch** after the merge request was done.

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
324
2) Put modifications back to develop branch from the new hotfix branch:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
325

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
326
327
328
    2.1. push your local branch to origin
    2.2. create a merge request on **develop**
    2.3. Follow up the discussion on the merge request to complete the actions required by the CamiTK team.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
329
330
331

**delete the branch** after the merge request was done.

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
332
### Try a hotfix
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
333

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
334
If a hotfix needs to be verified/tested/modified by more than one developer (that is one need more than one developer to work on a specific hotfix), the main hotfix developer should first push the hotfix branch on origin:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
335
336
337
338
339
340
341

``` bash
git push origin hotfix-4.0.1
```

**Don’t create a merge request**

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
342
Once the hotfix branch is on origin, this is how to get a local copy to verify/test/modify it:
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
343
344
345
346
347
348

``` bash
git pull
git checkout -b hotfix-4.0.1 origin/hotfix-4.0.1
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
349
## Git snippets
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
350
351
352

This is a small collection of little recipes for current git operations.

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
353
### Undo/Cancel the last commit
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
354

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
355
Simply use this command to completely undo the last commit and erase history.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
356
357
358
359
360

``` bash
git reset --hard HEAD~1
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
361
362
!!! Note
    `~` replaces the minus (`-`) sign, so this means reset to `HEAD - 1` commit.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
363

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
364
You can also simply use this command to undo the last commit but leave the history (and your files):
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
365
366
367
368
369

``` bash
git reset --soft HEAD~1
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
370
### Undo (uncommitted) changes
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
371

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
372
For instance, you have accidentally removed a file. To revert changes to modified
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
373
374
375
376
377
378
379
files:

``` bash
# Becareful: It resets the index and working tree. Any changes to tracked files in the working tree since the last commit are discarded.
git reset --hard
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
380
This is the same as `git reset –hard HEAD`.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
381

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
382
### Clean the current working tree
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
383
384
385
386
387
388
389
390
391
392

Use with care

``` bash
# check first what is going to happen
git clean -nd
# if ok, do it, f=force, d=remove directories
git clean -fd
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
393
### Rename a branch
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
394

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
395
`git branch -m compile-on-sid <newName>`
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
396

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
397
### Diff between two branches
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413

There are a lot of different ways to compare branches:

``` bash
git diff b1..b2
git diff b1...b2
git log b1..b2
git shortlog b1..b2
```

To get only the filenames that have changed:

``` bash
git diff --name-status b1..b2
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
414
See [this stackoverflow answers](http://stackoverflow.com/questions/822811/showing-which-files-have-changed-between-two-revisions)
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
415

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
416
### Solve a conflict after a pull
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
417
418
419
420
421
422
423

Just type:

``` bash
git mergetool
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
424
Running `git mergetool` will prompt the user for each conflict, and propose a default application. On Linux, we recommend to use kdiff3 as the git diff tool.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
425

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
426
### Copy commits from one branch to the current branch
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
427
428
429
430
431
432
433

Just type:

``` bash
git cherry-pick commit-hash
```

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
434
to apply the commit *commit-hash* (made on another branch) to your current branch.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
435

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
436
It is also possible to copy a range of commits from one branch to the current branch.
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
437

Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
438
### List the files modified by the last commit
Jean-Loup Haberbusch's avatar
Jean-Loup Haberbusch committed
439
440
441
442
443
444

Just type:

``` bash
git log -n 1 --name-only --pretty=
```