Join us
Merge seems to be a very simple topic, but it on first glance. We’ll learn what’s difference between ^ and ~ in GIT and uncover the full potential of the GIT log. This article continuous series of “Advanced GIT”. We’ve already talked about References, Git code store areas and what’s git. Let’s continue our journey 🏇.
Merge commits are just commits
> git cat-file -p 5b5200bcd
tree 6f7df6413905f826d34d03db65db07ac44534835
parent f217ba90579095b77f463667538beb3f28273926
parent f63976bc4f2baa1fcfb0c6868fe398c289efe0b3
author Kenneth Reitz <me@kennethreitz.org> 1499378257 -0400
committer Kenneth Reitz <me@kennethreitz.org> 1499378257 -0400
Merge branch 'master' of github.com:kennethreitz/requests
ℹ️: Make notice that this commit has 2 parents
Fast forward
> git checkout master
> git merge feature
Updating 2733233..9703930
Fast-forward
index.txt | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
Fast forward occurs when there are no commits on the base branch after the feature branch was created.
No fast forward
git merge
--no-ff
— will force a merge commit, even when one isn’t necessary
Merge conflicts
Merge conflicts occur when you try to merge, but files have significantly changed. GIT will wait until conflicts are resolved
> git merge feature
Auto-merging feature
CONFLICT (add/add): Merge conflict in feature
GIT RERERE — Reuse Recorded Resolution
GIT can remember how you solved the previous merge conflict and in the next commit reuse the same resolution.
Useful for long-lived branches (like refactoring) or rebasing.
How to enable it:
git
config rerere.enabled
true
--
global
flag to enable in all projectsExample:
> git config rerere.enabled true
> git checkout master
> git merge feature
Auto-merging feature
CONFLICT (add/add): Merge conflict in file
Recorded preimage for 'file'
Automatic merge failed; fix conflicts and then commit the result.
### Fix the merge conflict in file.
❯ git add file
❯ git commit -m "Resolve conflict"
Recorded resolution for 'feature'.
[master aa7291a] Resolve conflict
When we’ve committed conflict resolution, GIT has recorded it.
Now, let’s undid merge and try again.
> git merge feature
Auto-merging feature
CONFLICT (add/add): Merge conflict in feature
Resolved 'feature' using previous resolution.
Automatic merge failed; fix conflicts and then commit the result.
> git add file
> git diff --staged
diff --git a/file b/file
index 587be6b..a354eda 100644
--- a/file
+++ b/file
@@ -1 +1 @@
-The old change
+This is how I resolved my conflict.
\ No newline at end of file
The resolution is automatically applied.
History & Diffs
Good commits are important
Informative commits help preserve the history of code
When they’re helpful:
Rules of a good commit message
fix
git-rebase: don't ignore unexpected command line arguments
Currently, git-rebase will silently ignore any unexpected command-line switches and arguments (the command-line produced by git rev-parse). This allowed the rev-parse bug, fixed in the preceding commits, to go unnoticed. Let's make sure that doesn't happen again. We shouldn't be ignoring unexpected arguments. Let's not.
A description of the current behaviour, and a summary of why the fix is needed. Don’t forget to warn of the side effects.
GIT Log
git
log
— the basic command that shows you the history of your repository
git log
commit 767c4fd32dbcff87f8e040156e1a95a59f4bf730 (HEAD -> master)
Author: d9nich <54186666+d9nchik@users.noreply.github.com>
Date: Mon Aug 22 09:51:42 2022 +0300
initial commit
“Why my app is slow? It has been working since yesterday” — typical situation at the workplace
Typical usage:
git
log
--
since
="yesterday"
git
log
--
since
="2 weeks ago"
git
log
--
name-status
--follow -- <file>
— log files that have been renamed or movedgit
log
--
grep
<regexp>
— Search for commit messages that match a regular expressionℹ️: You can combine different flags:
> git log --grep=mail --author=bob --since=3.weeks
GIT log diff-filter
Selectively include or exclude files that have been:
> git log --diff-filter=R --stat
5e1ced3 make session serializer extensible support serializing 1-item dicts
with tag as key refactor serializer into flask.json.tag module continues
#1452, closes #1438, closes #1908
flask/{json.py => json/__init__.py} | 13 ++-----------
1 file changed, 2 insertions(+), 11 deletions(-)
8cf32bc Adds in blueprints and an application factory
examples/flaskr/flaskr/{ => blueprints}/flaskr.py | 55 ++++++++++++++
+----------------------------------------
1 file changed, 15 insertions(+), 40 deletions(-)
92fa444 Moves largerapp into patterns dir and add test
examples/{ => patterns}/largerapp/setup.py
| 0
examples/{ => patterns}/largerapp/yourapplication/__init__.py
| 0
examples/{ => patterns}/largerapp/yourapplication/static/style.css
| 0
examples/{ => patterns}/largerapp/yourapplication/templates/index.html | 0
Referencing commits
^
or ^n
:
^
= ^1
: the first parent commit^n
: the n-th parent commit~
or ~n
:
~
= ~1
: the first commit back, following 1st parent~n
: number of commits back, following only 1st parentℹ️: ^
and ~
can be combined
Preface:
Then we have the following equality:
A = A^0
B = A^ = A^1 = A~1
C = A^2
D = A^^ = A^1^1 = A~2
E = B^2 = A^^2
F = B^3 = A^^3 = A^2^1
GIT show — look at the commit
git
show
<commit>
— show commit and contentsgit
show
<commit> --
stat
— show file changed in the commitgit
show
<commit>:<file>
— look at the file from the commitDiff
shows you change between:
git
diff
— unstaged changes
git
diff
--
staged
— staged changes
Diff branches
git
branch
--
merged
master
— Which branches are merged with the master, and can be removed?
git
branch
--
no-merged
master
— Which branches aren’t merged with the master yet?
In this article, we’ve looked at merging in deep, discussed “good” commits, and how we can reference commits. Next time we will talk about how we can fix mistakes. Subscribe to not miss any new updates.
Join other developers and claim your FAUN account now!
Student
@d9nichInfluence
Total Hits
Posts
Only registered users can post comments. Please, login or signup.