Sign in Join
Wiki copytemporary

GIT Exercises: what changed

Revision 2 to 3. Removed lines have a minus sign; added lines have a plus sign.

OldNewLine
11This page has some exercises so you can experience different solutions/outcomes for a given task.
22
33By providing these (partly trivial) examples, the page ensures that you will not miss any of the scenarios while discovering the wonders of GIT.
44
55## Preface
66
77Some of the examples are about how to deal with your work when other people are pushed in between.
88
99For this you need a 2nd clone. See setup.
1010
1111## Setup
1212
1313The following steps should be performed before going to any of the below items.
1414
1515Initial setup and clone
1616
1717[[SVN to GIT Cheatsheet|SVN_to_GIT_Cheatsheet#Setup]]
1818
1919Second clone
2020
2121To simulate what someone else would have done while you where working. This can use the same user, the effect will be the same.
2222
2323You can either "git clone" again, or just make a copy of the entire directory into which you have cloned.
2424
2525You can then in that 2nd clone alter your username
2626
27-`   git config user.name  martin_2`
27+```text
28+ git config user.name martin_2
29+```
2830
2931### Each example setup
3032
3133In your first git folders, create a new branch
3234
33-`  git switch -c martins-test-1`\
34-`  git push --set-upstream origin martins-test-1  ## tell the server there is a new branch`
35+```text
36+ git switch -c martins-test-1
37+ git push --set-upstream origin martins-test-1 ## tell the server there is a new branch
38+```
3539
3640In your 2nd git folder
3741
38-`  git remote update`\
39-`  git switch martins-test-1`
42+```text
43+ git remote update
44+ git switch martins-test-1
45+```
4046
4147Thus both your folders are at the same branch, and are up to date with the remote
4248
4349> **Note:** In the example you work on "martins-test-1". But it is setup the same as your trunk branch would be. Therefore the same will apply when you work on trunk
4450
4551## Pushing your commit, if someone else made changes
4652
4753> **Note:** If you followed the configuration your "git pull" should be configured to default to either "--ff-ony" or "--rebase".\
4854> The arguments given in to "pull" in the examples, will override the defaults
4955
5056### Rebase
5157
5258****In Folder1****
5359
5460****In Folder2 (other person)****
5561
5662Setup new branches #Each example setup
5763
5864\| Edit the file lcl/forms.pp
5965
60-`   git commit -m 'test changes' lcl/forms.pp`
66+```text
67+ git commit -m 'test changes' lcl/forms.pp
68+```
6169
62-`   git log --oneline --graph --all -n 2`\
63-`      * 3b007fdc38 (HEAD -> martins-test-1) test changes`\
64-`      * c739dd68a7 (origin/master, origin/HEAD) Revert "Test a change in forms"`
70+```text
71+ git log --oneline --graph --all -n 2
72+ * 3b007fdc38 (HEAD -> martins-test-1) test changes
73+ * c739dd68a7 (origin/master, origin/HEAD) Revert "Test a change in forms"
74+```
6575
6676Your commit, follows the previous commit
6777
6878\|
6979
7080\|
7181
7282\| Edit the file ide/main.pp
7383
74-`   git commit -m 'test other' ide/main.pp`\
75-`   git push`
84+```text
85+ git commit -m 'test other' ide/main.pp
86+ git push
87+```
7688
7789\|
7890
79-`   git push`\
80-`       ! [rejected]              martins-test-1 -> martins-test-1 (fetch first)`\
81-`      error: failed to push some refs to '`[`https://gitlab.com/freepascal.org/lazarus_testconversion.git`](https://gitlab.com/freepascal.org/lazarus_testconversion.git)`'`
91+```text
92+ git push
93+  ! [rejected] martins-test-1 -> martins-test-1 (fetch first)
94+ error: failed to push some refs to 'https://gitlab.com/freepascal.org/lazarus_testconversion.git'
95+```
8296
8397Instead of the normal pull, we do a fetch. This will not resolve the problem, but we can see what it looks like.
8498
85-`   git fetch`
99+```text
100+ git fetch
101+```
86102
87-`   git log --oneline --graph --all -n 3`\
88-`      * 46a817b6b3 (origin/martins-test-1) test other`\
89-`      | * 3b007fdc38 (HEAD -> martins-test-1) test changes`\
90-`      |/`\
91-`      | * c739dd68a7 (origin/master, origin/HEAD) Revert "Test a change in forms"`
103+```text
104+ git log --oneline --graph --all -n 3
105+  * 46a817b6b3 (origin/martins-test-1) test other
106+  | * 3b007fdc38 (HEAD -> martins-test-1) test changes
107+  |/
108+  | * c739dd68a7 (origin/master, origin/HEAD) Revert "Test a change in forms"
109+```
92110
93111\|
94112
95113For the #Merge example stop here, and continue in the next section
96114
97115\| You can see your commit still follows the same commit as before.
98116
99117But on the server (origin/martins-test-1) there is the other commit, and it too has the same parent. The history on the server is fixed. We do not allow for it to be changed. So that commit will keep its parent.
100118
101119In order to get your commit to the server, your local commit must change it's parent to "test other" (this is called "rebase")
102120
103-`   git pull --rebase`\
104-`      Successfully rebased and updated refs/heads/martins-test-1.`
121+```text
122+ git pull --rebase
123+ Successfully rebased and updated refs/heads/martins-test-1.
124+```
105125
106-`   git log --oneline --graph --all -n 3`\
107-`      * 2cbe840e7e (HEAD -> martins-test-1) test changes`\
108-`      * 46a817b6b3 (origin/martins-test-1) test changes`\
109-`      * c739dd68a7 (origin/master, origin/HEAD) Revert "Test a change in forms"`
126+```text
127+ git log --oneline --graph --all -n 3
128+ * 2cbe840e7e (HEAD -> martins-test-1) test changes
129+ * 46a817b6b3 (origin/martins-test-1) test changes
130+ * c739dd68a7 (origin/master, origin/HEAD) Revert "Test a change in forms"
131+```
110132
111133Now your commit is on top, and we have a flat line of commits (like svn)
112134
113-`    git push`
135+```text
136+ git push
137+```
114138
115139\|
116140
117141### Merge
118142
119143> **Warning:** This example is what we want to avoid. This creates a diverged history (sub-branches). The log is no longer linear.
120144
121145****In Folder1****
122146
123147****In Folder2 (other person)****
124148
125149Setup new branches #Each example setup
126150
127151Repeat the steps from #Rebase above until you have done "git fetch" and reached the note for the "merge example"
128152
129153\|
130154
131155Now run
132156
133-`   git pull --no-ff --no-rebase`\
134-`      Merge made by the 'recursive' strategy.`\
135-`       ide/main.pp | 3 ---`\
136-`       1 file changed, 3 deletions(-)`
157+```text
158+ git pull --no-ff --no-rebase
159+ Merge made by the 'recursive' strategy.
160+ ide/main.pp | 3 ---
161+ 1 file changed, 3 deletions(-)
162+```
137163
138164The --no-ff --no-rebase overrides any configs you may have made, and gets you the default behaviour of merging. Git will open an editor for you, and ask for a commit message for the merge. The merge creates a new commit.
139165
140-`   git log --oneline --graph --all -n 4`\
141-`      *   044d9288b7 (HEAD -> martins-test-2) Merge branch 'martins-test-2' of `[`https://gitlab.com/freepascal.org/lazarus_testconversion`](https://gitlab.com/freepascal.org/lazarus_testconversion)` into martins-test-2`\
142-`      |\`\
143-`      | * 379b633c88 (origin/martins-test-2) test other`\
144-`      * | cfa3def67c test changes`\
145-`      |/`\
146-`      * 2cbe840e7e foo bar`
166+```text
167+ git log --oneline --graph --all -n 4
168+  * 044d9288b7 (HEAD -> martins-test-2) Merge branch 'martins-test-2' of https://gitlab.com/freepascal.org/lazarus_testconversion into martins-test-2
169+  |\
170+  | * 379b633c88 (origin/martins-test-2) test other
171+  * | cfa3def67c test changes
172+  |/
173+  * 2cbe840e7e foo bar
174+```
147175
148176As you can see the two commits are in different sub-branches (they are actually side by side). And a new merge commit has been added.
149177
150178You can push this.
151179
152180But this creates a commit log, like you would never see in svn.
153181
154182\|
155183
156184### Fast Forward
157185
158186> **Note:** In this example you will not have made a commit. Only the other person will have made a commit
159187
160188Fast-Forward (FF) can only happen if
161189
162190- there are new commits from a someone else
163191- but you have not made a commit yourself
164192
165193****In Folder1****
166194
167195****In Folder2 (other person)****
168196
169197Setup new branches #Each example setup
170198
171199\| Edit the file lcl/forms.pp Do **not** commit the changes.
172200
173-`   git status -uno`\
174-`      On branch martins-test-1`\
175-`      Your branch is up to date with 'origin/martins-test-1'.`\
176-`      `\
177-`      Changes not staged for commit:`\
178-`        (use "git add <file>..." to update what will be committed)`\
179-`        (use "git restore <file>..." to discard changes in working directory)`\
180-`              modified:   lcl/forms.pp`
201+```text
202+ git status -uno
203+ On branch martins-test-1
204+ Your branch is up to date with 'origin/martins-test-1'.
205+
206+ Changes not staged for commit:
207+ (use "git add <file>..." to update what will be committed)
208+ (use "git restore <file>..." to discard changes in working directory)
209+ modified: lcl/forms.pp
210+```
181211
182212Your changes are relative to the last commit
183213
184-`   git log --oneline -n1`\
185-`      c739dd68a7 (HEAD -> martins-test-1) Foo bar"`
214+```text
215+ git log --oneline -n1
216+ c739dd68a7 (HEAD -> martins-test-1) Foo bar"
217+```
186218
187219\|
188220
189221\|
190222
191223\| Edit the file ide/main.pp
192224
193-`   git commit -m 'test other' ide/main.pp`\
194-`   git push`
225+```text
226+ git commit -m 'test other' ide/main.pp
227+ git push
228+```
195229
196230\| Fetch the data from the remote
197231
198-`   git fetch`
232+```text
233+ git fetch
234+```
199235
200236Status will tell you you are behind the remote
201237
202-`   git status -uno`\
203-`      On branch martins-test-1`\
204-`      Your branch is behind 'origin/martins-test-1' by 1 commit, and can be fast-forwarded.`\
205-`        (use "git pull" to update your local branch)`\
206-`      `\
207-`      Changes not staged for commit:`\
208-`        (use "git add <file>..." to update what will be committed)`\
209-`        (use "git restore <file>..." to discard changes in working directory)`\
210-`              modified:   lcl/forms.pp`
238+```text
239+ git status -uno
240+ On branch martins-test-1
241+ Your branch is behind 'origin/martins-test-1' by 1 commit, and can be fast-forwarded.
242+ (use "git pull" to update your local branch)
243+
244+ Changes not staged for commit:
245+ (use "git add <file>..." to update what will be committed)
246+ (use "git restore <file>..." to discard changes in working directory)
247+ modified: lcl/forms.pp
248+```
211249
212250Your changes are still relative to the last local commit. Indicated by "HEAD"
213251
214-`   git log --oneline -n2 origin/martins-test-1`\
215-`      bc20668e78 (origin/martins-test-1) test other`\
216-`      c739dd68a7 (HEAD -> martins-test-1) Foo bar"`
252+```text
253+ git log --oneline -n2 origin/martins-test-1
254+ bc20668e78 (origin/martins-test-1) test other
255+ c739dd68a7 (HEAD -> martins-test-1) Foo bar"
256+```
217257
218-`   git pull --ff-only`\
219-`      Updating c739dd68a7..bc20668e78`\
220-`      Fast-forward`\
221-`       ide/main.pp | 2 ++`\
222-`       1 file changed, 2 insertions(+)`
258+```text
259+ git pull --ff-only
260+ Updating c739dd68a7..bc20668e78
261+ Fast-forward
262+ ide/main.pp | 2 ++
263+ 1 file changed, 2 insertions(+)
264+```
223265
224266Now the local branch "martins-test-1" has caught up to the remote branch. It includes the new commits. And it switched to the top commit.\
225267This catching up, is a fast forward.
226268
227269- No local branches commits where changed/moved/added. Only the branch pointer moved
228270- Your changes are still there, and are now relative to the top commit
229271
230-`   git log --oneline -n2 --graph origin/martins-test-1`\
231-`      bc20668e78 (HEAD -> martins-test-1, origin/martins-test-1) test other`\
232-`      c739dd68a7 Foo bar"`
272+```text
273+ git log --oneline -n2 --graph origin/martins-test-1
274+ bc20668e78 (HEAD -> martins-test-1, origin/martins-test-1) test other
275+ c739dd68a7 Foo bar"
276+```
233277
234278\|
235279
236280## Conflict Resolution
237281
238282### Getting selected upstream changes
239283
240284****In Folder1****
241285
242286****In Folder2 (other person)****
243287
244288Setup new branches #Each example setup
245289
246290\| Edit the file lcl/forms.pp\
247291Edit the file ide/main.pp somewhere from line 100 down
248292
249-`   git diff --stat`\
250-`      ide/main.pp  | 5 +++++`\
251-`      lcl/forms.pp | 4 ++++`\
252-`      2 files changed, 9 insertions(+)`
293+```text
294+ git diff --stat
295+ ide/main.pp | 5 +++++
296+ lcl/forms.pp | 4 ++++
297+ 2 files changed, 9 insertions(+)
298+```
253299
254-`   git log --oneline -n 1`\
255-`      c739dd68a7 (HEAD -> martins-test-1) Foo Bar"`
300+```text
301+ git log --oneline -n 1
302+ c739dd68a7 (HEAD -> martins-test-1) Foo Bar"
303+```
256304
257305\|
258306
259307\|
260308
261309\| Edit the file ide/main.pp somewere in the first 20 lines (no conflict with the main user in folder1)\
262310Also edit the file lcl/grids.pas
263311
264-`   git commit -m 'test other' -a`\
265-`   git push`
312+```text
313+ git commit -m 'test other' -a
314+ git push
315+```
266316
267317\| Update from remote. Keep current commit as base for your changes
268318
269-`   git fetch`\
270-`   git log --oneline -n2  origin/martins-test-1`\
271-`      810b4f1bd4 (origin/martins-test-1) test other`\
272-`      c739dd68a7 (HEAD -> martins-test-1) Foo Bar`
319+```text
320+ git fetch
321+ git log --oneline -n2 origin/martins-test-1
322+ 810b4f1bd4 (origin/martins-test-1) test other
323+ c739dd68a7 (HEAD -> martins-test-1) Foo Bar
324+```
273325
274326The commit "test other" changes several files. For testing you want to integrate the changes in file ide/main.pp, but no other changes.
275327
276-`   git stash`\
277-`   git restore --source=origin/martins-test-1  ide/main.pp`\
278-`   git commit -m 'dummy commit'  ide/main.pp`\
279-`   git stash pop`
328+```text
329+ git stash
330+ git restore --source=origin/martins-test-1 ide/main.pp
331+ git commit -m 'dummy commit' ide/main.pp
332+ git stash pop
333+```
280334
281335Now you have a dummy commit with the changes to test. And your changes applied on top.
282336
283-`   git log --oneline -n2`\
284-`      63e2dcec9f (HEAD -> martins-test-1) dummy commit`\
285-`      c739dd68a7 Foo Bar`\
286-`   git diff --stat`\
287-`      ide/main.pp  | 5 +++++`\
288-`      lcl/forms.pp | 4 ++++`\
289-`      2 files changed, 9 insertions(+)`
337+```text
338+ git log --oneline -n2
339+ 63e2dcec9f (HEAD -> martins-test-1) dummy commit
340+ c739dd68a7 Foo Bar
341+ git diff --stat
342+ ide/main.pp | 5 +++++
343+ lcl/forms.pp | 4 ++++
344+ 2 files changed, 9 insertions(+)
345+```
290346
291347Now work with those changes, make further changes, eventually commit
292348
293-`   git commit -m 'my changes' -a`\
294-`      [martins-test-1 9aba7218d1] my changes`\
295-`       2 files changed, 9 insertions(+)`
349+```text
350+ git commit -m 'my changes' -a
351+ [martins-test-1 9aba7218d1] my changes
352+ 2 files changed, 9 insertions(+)
353+```
296354
297-`   git log --oneline -n5 --graph --all`\
298-`      * 9aba7218d1 (HEAD -> martins-test-1) my changes`\
299-`      * 63e2dcec9f dummy commit`\
300-`      | * 810b4f1bd4 (origin/martins-test-1) test other`\
301-`      |/`\
302-`      * c739dd68a7 Foo Bar`
355+```text
356+ git log --oneline -n5 --graph --all
357+  * 9aba7218d1 (HEAD -> martins-test-1) my changes
358+  * 63e2dcec9f dummy commit
359+  | * 810b4f1bd4 (origin/martins-test-1) test other
360+  |/
361+  * c739dd68a7 Foo Bar
362+```
303363
304364So you now need to rebase your changes on the origin/martins-test-1 to commit them.
305365
306-`   git pull --rebase`\
307-`      Successfully rebased and updated refs/heads/martins-test-1.`
366+```text
367+ git pull --rebase
368+ Successfully rebased and updated refs/heads/martins-test-1.
369+```
308370
309-`   git log --oneline -n3`\
310-`      f0d6afd90d (HEAD -> martins-test-1) my changes`\
311-`      810b4f1bd4 (origin/martins-test-1) test other`\
312-`      c739dd68a7 Foo Bar`
371+```text
372+ git log --oneline -n3
373+  f0d6afd90d (HEAD -> martins-test-1) my changes
374+  810b4f1bd4 (origin/martins-test-1) test other
375+  c739dd68a7 Foo Bar
376+```
313377
314378The "dummy commit" is gone. Because it was identical to existing changes. Of course you should check this. If it is still there you need to drop it with
315379
316-`   git rebase -i `
380+```text
381+ git rebase -i
382+```
317383
318384\|
319385
320386---
321387*From the Free Pascal wiki, page "GIT Exercises", as it stood on 2021-12-29 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*
322388Category: Version Control

Back to history