GIT Exercises: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | This page has some exercises so you can experience different solutions/outcomes for a given task. | |
| 2 | 2 | ||
| 3 | 3 | By providing these (partly trivial) examples, the page ensures that you will not miss any of the scenarios while discovering the wonders of GIT. | |
| 4 | 4 | ||
| 5 | 5 | ## Preface | |
| 6 | 6 | ||
| 7 | 7 | Some of the examples are about how to deal with your work when other people are pushed in between. | |
| 8 | 8 | ||
| 9 | 9 | For this you need a 2nd clone. See setup. | |
| 10 | 10 | ||
| 11 | 11 | ## Setup | |
| 12 | 12 | ||
| 13 | 13 | The following steps should be performed before going to any of the below items. | |
| 14 | 14 | ||
| 15 | 15 | Initial setup and clone | |
| 16 | 16 | ||
| 17 | 17 | [[SVN to GIT Cheatsheet|SVN_to_GIT_Cheatsheet#Setup]] | |
| 18 | 18 | ||
| 19 | 19 | Second clone | |
| 20 | 20 | ||
| 21 | 21 | To simulate what someone else would have done while you where working. This can use the same user, the effect will be the same. | |
| 22 | 22 | ||
| 23 | 23 | You can either "git clone" again, or just make a copy of the entire directory into which you have cloned. | |
| 24 | 24 | ||
| 25 | 25 | You can then in that 2nd clone alter your username | |
| 26 | 26 | ||
| 27 | 27 | ` git config user.name martin_2` | |
| 28 | 28 | ||
| 29 | 29 | ### Each example setup | |
| 30 | 30 | ||
| 31 | 31 | In your first git folders, create a new branch | |
| 32 | 32 | ||
| 33 | 33 | ` git switch -c martins-test-1`\ | |
| 34 | 34 | ` git push --set-upstream origin martins-test-1 ## tell the server there is a new branch` | |
| 35 | 35 | ||
| 36 | 36 | In your 2nd git folder | |
| 37 | 37 | ||
| 38 | 38 | ` git remote update`\ | |
| 39 | 39 | ` git switch martins-test-1` | |
| 40 | 40 | ||
| 41 | 41 | Thus both your folders are at the same branch, and are up to date with the remote | |
| 42 | 42 | ||
| 43 | 43 | > **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 | |
| 44 | 44 | ||
| 45 | 45 | ## Pushing your commit, if someone else made changes | |
| 46 | 46 | ||
| 47 | 47 | > **Note:** If you followed the configuration your "git pull" should be configured to default to either "--ff-ony" or "--rebase".\ | |
| 48 | 48 | > The arguments given in to "pull" in the examples, will override the defaults | |
| 49 | 49 | ||
| 50 | 50 | ### Rebase | |
| 51 | 51 | ||
| 52 | - | ||
| 53 | - | ||
| 54 | - | ||
| 55 | - | ||
| 56 | - | ||
| 57 | - | ||
| 58 | - | ||
| 59 | - | ||
| 60 | - | ||
| 61 | - | ||
| 62 | - | ||
| 63 | - | ||
| 64 | - | ||
| 65 | - | ||
| 66 | - | ||
| 67 | - | ||
| 68 | - | ||
| 69 | - | ||
| 70 | - | ||
| 71 | - | ||
| 72 | - | ||
| 73 | - | ||
| 74 | - | ||
| 75 | - | ||
| 76 | - | ||
| 77 | - | ||
| 78 | - | ||
| 79 | - | ||
| 80 | - | ||
| 81 | - | ||
| 82 | - | ||
| 83 | - | ||
| 84 | - | ||
| 85 | - | ||
| 86 | - | ||
| 87 | - | ||
| 88 | - | ||
| 89 | - | ||
| 90 | - | ||
| 91 | - | ||
| 92 | - | ||
| 93 | - | ||
| 94 | - | ||
| 95 | - | ||
| 96 | - | ||
| 97 | - | ||
| 98 | - | ||
| 99 | - | ||
| 100 | - | ||
| 101 | - | ||
| 102 | - | ||
| 103 | - | ||
| 104 | - | ||
| 105 | - | ||
| 106 | - | ||
| 107 | - | ||
| 108 | - | ||
| 109 | - | ||
| 110 | - | ||
| 52 | + | ****In Folder1**** | |
| 111 | 53 | ||
| 54 | + | ****In Folder2 (other person)**** | |
| 55 | + | ||
| 56 | + | Setup new branches #Each example setup | |
| 57 | + | ||
| 58 | + | \| Edit the file lcl/forms.pp | |
| 59 | + | ||
| 60 | + | ` git commit -m 'test changes' lcl/forms.pp` | |
| 61 | + | ||
| 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"` | |
| 65 | + | ||
| 66 | + | Your commit, follows the previous commit | |
| 67 | + | ||
| 68 | + | \| | |
| 69 | + | ||
| 70 | + | \| | |
| 71 | + | ||
| 72 | + | \| Edit the file ide/main.pp | |
| 73 | + | ||
| 74 | + | ` git commit -m 'test other' ide/main.pp`\ | |
| 75 | + | ` git push` | |
| 76 | + | ||
| 77 | + | \| | |
| 78 | + | ||
| 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)`'` | |
| 82 | + | ||
| 83 | + | Instead of the normal pull, we do a fetch. This will not resolve the problem, but we can see what it looks like. | |
| 84 | + | ||
| 85 | + | ` git fetch` | |
| 86 | + | ||
| 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"` | |
| 92 | + | ||
| 93 | + | \| | |
| 94 | + | ||
| 95 | + | For the #Merge example stop here, and continue in the next section | |
| 96 | + | ||
| 97 | + | \| You can see your commit still follows the same commit as before. | |
| 98 | + | ||
| 99 | + | But 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. | |
| 100 | + | ||
| 101 | + | In order to get your commit to the server, your local commit must change it's parent to "test other" (this is called "rebase") | |
| 102 | + | ||
| 103 | + | ` git pull --rebase`\ | |
| 104 | + | ` Successfully rebased and updated refs/heads/martins-test-1.` | |
| 105 | + | ||
| 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"` | |
| 110 | + | ||
| 111 | + | Now your commit is on top, and we have a flat line of commits (like svn) | |
| 112 | + | ||
| 113 | + | ` git push` | |
| 114 | + | ||
| 115 | + | \| | |
| 116 | + | ||
| 112 | 117 | ### Merge | |
| 113 | 118 | ||
| 114 | 119 | > **Warning:** This example is what we want to avoid. This creates a diverged history (sub-branches). The log is no longer linear. | |
| 115 | 120 | ||
| 116 | - | ||
| 117 | - | ||
| 118 | - | ||
| 119 | - | ||
| 120 | - | ||
| 121 | - | ||
| 122 | - | ||
| 123 | - | ||
| 124 | - | ||
| 125 | - | ||
| 126 | - | ||
| 127 | - | ||
| 128 | - | ||
| 129 | - | ||
| 130 | - | ||
| 131 | - | ||
| 132 | - | ||
| 133 | - | ||
| 134 | - | ||
| 135 | - | ||
| 136 | - | ||
| 137 | - | ||
| 138 | - | ||
| 139 | - | ||
| 140 | - | ||
| 141 | - | ||
| 142 | - | ||
| 143 | - | ||
| 144 | - | ||
| 145 | - | ||
| 146 | - | ||
| 147 | - | ||
| 148 | - | ||
| 149 | - | ||
| 121 | + | ****In Folder1**** | |
| 150 | 122 | ||
| 123 | + | ****In Folder2 (other person)**** | |
| 124 | + | ||
| 125 | + | Setup new branches #Each example setup | |
| 126 | + | ||
| 127 | + | Repeat the steps from #Rebase above until you have done "git fetch" and reached the note for the "merge example" | |
| 128 | + | ||
| 129 | + | \| | |
| 130 | + | ||
| 131 | + | Now run | |
| 132 | + | ||
| 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(-)` | |
| 137 | + | ||
| 138 | + | The --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. | |
| 139 | + | ||
| 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` | |
| 147 | + | ||
| 148 | + | As 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. | |
| 149 | + | ||
| 150 | + | You can push this. | |
| 151 | + | ||
| 152 | + | But this creates a commit log, like you would never see in svn. | |
| 153 | + | ||
| 154 | + | \| | |
| 155 | + | ||
| 151 | 156 | ### Fast Forward | |
| 152 | 157 | ||
| 153 | 158 | > **Note:** In this example you will not have made a commit. Only the other person will have made a commit | |
| 154 | 159 | ||
| 155 | 160 | Fast-Forward (FF) can only happen if | |
| 156 | 161 | ||
| 157 | 162 | - there are new commits from a someone else | |
| 158 | 163 | - but you have not made a commit yourself | |
| 159 | 164 | ||
| 160 | - | ||
| 161 | - | ||
| 162 | - | ||
| 163 | - | ||
| 164 | - | ||
| 165 | - | ||
| 166 | - | ||
| 167 | - | ||
| 168 | - | ||
| 169 | - | ||
| 170 | - | ||
| 171 | - | ||
| 172 | - | ||
| 173 | - | ||
| 174 | - | ||
| 175 | - | ||
| 176 | - | ||
| 177 | - | ||
| 178 | - | ||
| 179 | - | ||
| 180 | - | ||
| 181 | - | ||
| 182 | - | ||
| 183 | - | ||
| 184 | - | ||
| 185 | - | ||
| 186 | - | ||
| 187 | - | ||
| 188 | - | ||
| 189 | - | ||
| 190 | - | ||
| 191 | - | ||
| 192 | - | ||
| 193 | - | ||
| 194 | - | ||
| 195 | - | ||
| 196 | - | ||
| 197 | - | ||
| 198 | - | ||
| 199 | - | ||
| 200 | - | ||
| 201 | - | ||
| 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 | - | ||
| 165 | + | ****In Folder1**** | |
| 227 | 166 | ||
| 167 | + | ****In Folder2 (other person)**** | |
| 168 | + | ||
| 169 | + | Setup new branches #Each example setup | |
| 170 | + | ||
| 171 | + | \| Edit the file lcl/forms.pp Do **not** commit the changes. | |
| 172 | + | ||
| 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` | |
| 181 | + | ||
| 182 | + | Your changes are relative to the last commit | |
| 183 | + | ||
| 184 | + | ` git log --oneline -n1`\ | |
| 185 | + | ` c739dd68a7 (HEAD -> martins-test-1) Foo bar"` | |
| 186 | + | ||
| 187 | + | \| | |
| 188 | + | ||
| 189 | + | \| | |
| 190 | + | ||
| 191 | + | \| Edit the file ide/main.pp | |
| 192 | + | ||
| 193 | + | ` git commit -m 'test other' ide/main.pp`\ | |
| 194 | + | ` git push` | |
| 195 | + | ||
| 196 | + | \| Fetch the data from the remote | |
| 197 | + | ||
| 198 | + | ` git fetch` | |
| 199 | + | ||
| 200 | + | Status will tell you you are behind the remote | |
| 201 | + | ||
| 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` | |
| 211 | + | ||
| 212 | + | Your changes are still relative to the last local commit. Indicated by "HEAD" | |
| 213 | + | ||
| 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"` | |
| 217 | + | ||
| 218 | + | ` git pull --ff-only`\ | |
| 219 | + | ` Updating c739dd68a7..bc20668e78`\ | |
| 220 | + | ` Fast-forward`\ | |
| 221 | + | ` ide/main.pp | 2 ++`\ | |
| 222 | + | ` 1 file changed, 2 insertions(+)` | |
| 223 | + | ||
| 224 | + | Now 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.\ | |
| 225 | + | This catching up, is a fast forward. | |
| 226 | + | ||
| 227 | + | - No local branches commits where changed/moved/added. Only the branch pointer moved | |
| 228 | + | - Your changes are still there, and are now relative to the top commit | |
| 229 | + | ||
| 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"` | |
| 233 | + | ||
| 234 | + | \| | |
| 235 | + | ||
| 228 | 236 | ## Conflict Resolution | |
| 229 | 237 | ||
| 230 | 238 | ### Getting selected upstream changes | |
| 231 | 239 | ||
| 232 | - | ||
| 233 | - | ||
| 234 | - | ||
| 235 | - | ||
| 236 | - | ||
| 237 | - | ||
| 238 | - | ||
| 239 | - | ||
| 240 | - | ||
| 241 | - | ||
| 242 | - | ||
| 243 | - | ||
| 244 | - | ||
| 245 | - | ||
| 246 | - | ||
| 247 | - | ||
| 248 | - | ||
| 249 | - | ||
| 250 | - | ||
| 251 | - | ||
| 252 | - | ||
| 253 | - | ||
| 254 | - | ||
| 255 | - | ||
| 256 | - | ||
| 257 | - | ||
| 258 | - | ||
| 259 | - | ||
| 260 | - | ||
| 261 | - | ||
| 262 | - | ||
| 263 | - | ||
| 264 | - | ||
| 265 | - | ||
| 266 | - | ||
| 267 | - | ||
| 268 | - | ||
| 269 | - | ||
| 270 | - | ||
| 271 | - | ||
| 272 | - | ||
| 273 | - | ||
| 274 | - | ||
| 275 | - | ||
| 276 | - | ||
| 277 | - | ||
| 278 | - | ||
| 279 | - | ||
| 280 | - | ||
| 281 | - | ||
| 282 | - | ||
| 283 | - | ||
| 284 | - | ||
| 285 | - | ||
| 286 | - | ||
| 287 | - | ||
| 288 | - | ||
| 289 | - | ||
| 290 | - | ||
| 291 | - | ||
| 292 | - | ||
| 293 | - | ||
| 294 | - | ||
| 295 | - | ||
| 296 | - | ||
| 297 | - | ||
| 298 | - | ||
| 299 | - | ||
| 300 | - | ||
| 301 | - | ||
| 302 | - | ||
| 240 | + | ****In Folder1**** | |
| 241 | + | ||
| 242 | + | ****In Folder2 (other person)**** | |
| 243 | + | ||
| 244 | + | Setup new branches #Each example setup | |
| 245 | + | ||
| 246 | + | \| Edit the file lcl/forms.pp\ | |
| 247 | + | Edit the file ide/main.pp somewhere from line 100 down | |
| 248 | + | ||
| 249 | + | ` git diff --stat`\ | |
| 250 | + | ` ide/main.pp | 5 +++++`\ | |
| 251 | + | ` lcl/forms.pp | 4 ++++`\ | |
| 252 | + | ` 2 files changed, 9 insertions(+)` | |
| 253 | + | ||
| 254 | + | ` git log --oneline -n 1`\ | |
| 255 | + | ` c739dd68a7 (HEAD -> martins-test-1) Foo Bar"` | |
| 256 | + | ||
| 257 | + | \| | |
| 258 | + | ||
| 259 | + | \| | |
| 260 | + | ||
| 261 | + | \| Edit the file ide/main.pp somewere in the first 20 lines (no conflict with the main user in folder1)\ | |
| 262 | + | Also edit the file lcl/grids.pas | |
| 263 | + | ||
| 264 | + | ` git commit -m 'test other' -a`\ | |
| 265 | + | ` git push` | |
| 266 | + | ||
| 267 | + | \| Update from remote. Keep current commit as base for your changes | |
| 268 | + | ||
| 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` | |
| 273 | + | ||
| 274 | + | The commit "test other" changes several files. For testing you want to integrate the changes in file ide/main.pp, but no other changes. | |
| 275 | + | ||
| 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` | |
| 280 | + | ||
| 281 | + | Now you have a dummy commit with the changes to test. And your changes applied on top. | |
| 282 | + | ||
| 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(+)` | |
| 290 | + | ||
| 291 | + | Now work with those changes, make further changes, eventually commit | |
| 292 | + | ||
| 293 | + | ` git commit -m 'my changes' -a`\ | |
| 294 | + | ` [martins-test-1 9aba7218d1] my changes`\ | |
| 295 | + | ` 2 files changed, 9 insertions(+)` | |
| 296 | + | ||
| 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` | |
| 303 | + | ||
| 304 | + | So you now need to rebase your changes on the origin/martins-test-1 to commit them. | |
| 305 | + | ||
| 306 | + | ` git pull --rebase`\ | |
| 307 | + | ` Successfully rebased and updated refs/heads/martins-test-1.` | |
| 308 | + | ||
| 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` | |
| 313 | + | ||
| 314 | + | The "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 | |
| 315 | + | ||
| 316 | + | ` git rebase -i ` | |
| 317 | + | ||
| 318 | + | \| | |
| 303 | 319 | ||
| 304 | 320 | --- | |
| 305 | 321 | *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).* | |
| 306 | 322 | Category: Version Control |
