Tons of e-ink has been spilled all over the internet about the shape of git history.
Personally, I want to be able to view the project history from a higher level but I also want to view the implementation details of a feature.
Typically viewing the higher level means that I want to see the history at the feature or bug-fix level. Additionally, I want to see when each feature made it to the main branch. I can view the history at this level by following only the first-parent commits of the log e.g. using the --first-parent
flag of the git log
command.
git log --oneline --graph --first-parent
* 663653a (HEAD -> main, origin/main, origin/HEAD) Feature 3
* 6803a96 Feature 2
* bdcdfa5 Feature 1
* 2b6737e Initial commit
If you are using the Git Graph vscode extension you could activate this view by checking the Only follow the first parent of commits
option:
The resulting view is the following:
But, if I want to see the implementation details of a feature I can omit the --first-parent
flag
git log --oneline --graph
* 663653a (HEAD -> main, origin/main, origin/HEAD) Feature 3
* 6803a96 Feature 2
|\
| * 1189f63 Call hw 5
| * 1f33ff3 Call hw 4
|/
* bdcdfa5 Feature 1
|\
| * 7f48316 Call 2 more times
| * 9674fd6 Add console application
|/
* 2b6737e Initial commit
The corresponding Git Graph view is:
If the tool that you are using does not support semi-linear merges you could do it manually. Please keep in mind that there is no way to guarantee that the merge will produce a semi-linear result: someone else could have pushed changes to the main branch! Use the -no-ff
flag to force the creation of a merge commit in all cases, even when the merge could instead be resolved as a fast-forward.
My current git workflow is the following:
# pull the latest changes of the main branch
git pull
# create a feature/topic branch before starting new work
git switch -c feature-branch
# make some changes and commit often in the topic branch
git add .
git commit -m "Add a comment about this commit"
# periodically rebase your work onto main branch
git fetch origin
git rebase origin/main
# make more changes
git add .
git commit -m "Add a comment about the new changes"
# switch to main branch and merge the topic branch using the `--no-ff` flag
# don't forget to set the message of the merge commit using the `-m` flag
git switch main
git pull
git merge --no-ff -m "Add feature 5" feature-branch
See also
- Semi-linear-merge discussion in Stack Overflow (see several answers).
- Semi-linear merge in Azure devops
- Rebase, merge in BitBucket
- Semi-linear merge in BitBucket
- I'm not sure if GitHub supports semi-linear merges
Happy coding \m/
Top comments (0)