To be able to start new development lines, we need to create a branch.
In Git, branches work like movable labels:
HEAD
refers toHEAD
is attached to them, they move along with HEAD
Branches are created with git branch branch_name
⬇️ git branch new-experiment
⬇️
HEAD
does not attach to the new branch by default,
an explicit checkout
is required.
Creating new branches allows to store changes made when we are in DETACHED_HEAD state.
⬇️ git checkout HEAD~4
⬇️
➡️ Next: git branch new-experiment
➡️
⬇️ git branch new-experiment
⬇️
HEAD
is still detached though, we need to attach it to the new branch for it to store our commits
➡️ Next: git checkout new-experiment
➡️
⬇️ git checkout new-experiment
⬇️
⬇️ [changes] + git add
+ git commit
⬇️
$\Rightarrow$ HEAD
brings our branch forward with it!
As you can imagine, creating a new branch and attaching HEAD
to the freshly created branch is pretty common
As customary for common operations, a short-hand is provided: git checkout -b new-branch-name
new-branch-name
from the current position of HEAD
HEAD
to new-branch-name
⬇️ git checkout -b new-experiment
⬇️
Reunifying diverging development lines is much trickier than spawning new development lines
In other words, merging is much trickier than branching
In Git, git merge target
merges the branch named target
into the current branch (HEAD
must be attached)
⬇️ git merge master
⬇️
Consider this situation:
new-experiment
to also have the changes from C7
, to C10
(to be up to date with master
)master
contains all the commits of new-experiment
new-experiment
to point it to C6
Git tries to resolve most conflicts by itself
In case of conflict on one or more files, Git marks the subject files as conflicted, and modifies them adding merge markers:
<<<<<<< HEAD
Changes made on the branch that is being merged into,
this is the branch currently checked out (HEAD).
=======
Changes made on the branch that is being merged in.
>>>>>>> other-branch-name
git add
git commit
git commit --no-edit
can be used to use it without editingAvoiding merge conflicts is much better than solving them
Although they are unavoidable in some cases, they can be minimized by following a few good practices: