This page has some exercises so you can experience different solutions/outcomes for a given task.
By providing these (partly trivial) examples, the page ensures that you will not miss any of the scenarios while discovering the wonders of GIT.
Preface
editSome of the examples are about how to deal with your work when other people are pushed in between.
For this you need a 2nd clone. See setup.
Setup
editThe following steps should be performed before going to any of the below items.
Initial setup and clone
Second clone
To simulate what someone else would have done while you where working. This can use the same user, the effect will be the same.
You can either "git clone" again, or just make a copy of the entire directory into which you have cloned.
You can then in that 2nd clone alter your username
git config user.name martin_2
Each example setup
editIn your first git folders, create a new branch
git switch -c martins-test-1
git push --set-upstream origin martins-test-1 ## tell the server there is a new branch
In your 2nd git folder
git remote update
git switch martins-test-1
Thus both your folders are at the same branch, and are up to date with the remote
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
Pushing your commit, if someone else made changes
editIf you followed the configuration your "git pull" should be configured to default to either "--ff-ony" or "--rebase".
The arguments given in to "pull" in the examples, will override the defaults
Rebase
edit| In Folder1 | In Folder2 (other person) |
|---|---|
| Setup new branches #Each example setup | |
|
Edit the file lcl/forms.pp
Your commit, follows the previous commit |
|
|
Edit the file ide/main.pp
|
|
Instead of the normal pull, we do a fetch. This will not resolve the problem, but we can see what it looks like.
|
|
| For the #Merge example stop here, and continue in the next section | |
|
You can see your commit still follows the same commit as before. 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. In order to get your commit to the server, your local commit must change it's parent to "test other" (this is called "rebase")
Now your commit is on top, and we have a flat line of commits (like svn)
|
|
Merge
editThis example is what we want to avoid. This creates a diverged history (sub-branches). The log is no longer linear.
| In Folder1 | In Folder2 (other person) |
|---|---|
|
Setup new branches #Each example setup Repeat the steps from #Rebase above until you have done "git fetch" and reached the note for the "merge example" |
|
|
Now run
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.
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. You can push this. But this creates a commit log, like you would never see in svn. |
|
Fast Forward
editIn this example you will not have made a commit. Only the other person will have made a commit
Fast-Forward (FF) can only happen if
- there are new commits from a someone else
- but you have not made a commit yourself
| In Folder1 | In Folder2 (other person) |
|---|---|
| Setup new branches #Each example setup | |
|
Edit the file lcl/forms.pp Do not commit the changes.
Your changes are relative to the last commit
|
|
|
Edit the file ide/main.pp
|
|
|
Fetch the data from the remote
Status will tell you you are behind the remote
Your changes are still relative to the last local commit. Indicated by "HEAD"
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. This catching up, is a fast forward.
|
|
Conflict Resolution
editGetting selected upstream changes
edit| In Folder1 | In Folder2 (other person) |
|---|---|
| Setup new branches #Each example setup | |
|
Edit the file lcl/forms.pp
|
|
|
Edit the file ide/main.pp somewere in the first 20 lines (no conflict with the main user in folder1)
|
|
|
Update from remote. Keep current commit as base for your changes
The commit "test other" changes several files. For testing you want to integrate the changes in file ide/main.pp, but no other changes.
Now you have a dummy commit with the changes to test. And your changes applied on top.
Now work with those changes, make further changes, eventually commit
So you now need to rebase your changes on the origin/martins-test-1 to commit them.
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
|
|