Merging, branching and rebasing
The setup script is located in the git.zip file
in the exercsises folder
.
Run ./branches.sh
for setup!
Introduction
This exercise has a git lga
and git po
alias configured:
# git lga will show all commits over all branches
git config --local alias.lga ="log --graph --all --pretty=oneline"
# git po will print the content of a git object
git config --local alias.po="cat-file -p"
Run git branch
to see what branches are available
anotherbranch
ffbranch
* main
rebasesource
rebasetarget
Cherry-Pick
Now, we perform a cherry pick.
Instructions
- Make sure you are on the "main" branch
- Create and switch to "cherry" branch
(
git switch -c cherry
) - Run
git lga
- Pick the commit with the message "cherry-pick me" and perform a
cherry pick (
git cherry-pick commitHash
)
Questions
- On which branch is the original commit located?
- Run
git lga
. What are the commit hashes for this commit on "cherry" and on the origin branch? - Do they differ? If so, any idea why?
- Inspect both commit with
git cat-file -p commit-hash
or by using the configured alias. Do they point to the same tree? - Inspect the tree(s), what is the hash of the blob(s) for the file `cherry-pick.txt
- Have a look at the reflog (
git reflog
). Can you spot the cherry pick?
Answers
- ffbranch, rebasetarget
- they differ, we also have two commits
- the parents changed
- Both commits have different trees
- Both trees link to the same blob for
cherry.txt
Merging (Fast-Forward)
Next we perform a merge of branch "ffbranch" into "main". This will be a fast-forward merge.
Instructions
- Make sure you are on the "main" branch
- Run
git lga
and make note where the HEAD point to - Run
git diff main..ffbranch
to see what changes you gong to pick - Run
git diff ffbranch...main
to check for "commits" on main but not on "ffbranch" - Merge
ffbranch
into main
Questions
- Which merge mode was used?
- Look at the log: Can you spot a merge commit? And where does the HEAD point to?
Answers
- fast-forward
- no, all the "main" history is now the same as "ffbranch". All commits of "main" were on "ffranch".
Merging
This time we will perform a merge resulting in a merge commit. We will merge "anotherbranch" into "ffbranch". There will be conflicts.
Instructions
- Make sure you are on the "ffbranch" branch
- Run
git diff ffbranch..anotherbranch
to see what changes you gong to pick - Run
git diff anotherbranch...ffbranch
to check for "commits" on main but not on "ffbranch" - Merge "anotherbranch" into "ffbranch"
- Optional: Run
git merge --abort
to abort the merge, then start it again. - Run
git diff
to inspect the conflicts in the combined diff format. - Resolve the conflict in "ff.txt" by not accepting the changes from "anotherbranch" and stage the file
- Stage the file "additional.txt" as it is
- Run
git status
, two files should be staged - Run
git merge --continue
and remove the comments from the "Conflicts" details - Run
git log
Questions
- What type of commit is the current commit of "ffbranch"?
- Inspect this commit using
git cat-file -p
or thegit po
alias (Hint: you can usegit po HEAD
) - Have a look into the conflicted files,
(
cat additional.txt
,cat ff.txt
) What do you see? - How many parents does this commit have?
Rebase
Last but not least we will perform a rebase. We will rebase "rebasetarget" onto "rebasesource". The histories of this branch are identical to "ffbranch" and "anotherbranch" before the merge from above.
Instructions
- Make sure you are on "reabasetarget"
- Run
git rebase reabasesource
- Optional: Run
git rebase --abort
to abort the rebase. Then start it again. - Column1. Add "additional.txt" without resolving the config
(
git add additional.txt
) git rebase --continue
- Edit "ff.txt" to only keep the changes from the "add explainer" commit
- Add "ff.txt" (
git add ff.txt
) - Run
git bease --continue
to complete this rebase
Questions
- Have a look into the conflicted files,
(
cat additional.txt
,cat ff.txt
) What do you see? - Check
man git rerere
- could this help when rebasing and fixing conflicts?