or
or
By clicking below, you agree to our terms of service.
New to HackMD? Sign up
Syntax | Example | Reference | |
---|---|---|---|
# Header | Header | 基本排版 | |
- Unordered List |
|
||
1. Ordered List |
|
||
- [ ] Todo List |
|
||
> Blockquote | Blockquote |
||
**Bold font** | Bold font | ||
*Italics font* | Italics font | ||
~~Strikethrough~~ | |||
19^th^ | 19th | ||
H~2~O | H2O | ||
++Inserted text++ | Inserted text | ||
==Marked text== | Marked text | ||
[link text](https:// "title") | Link | ||
 | Image | ||
`Code` | Code |
在筆記中貼入程式碼 | |
```javascript var i = 0; ``` |
|
||
:smile: | ![]() |
Emoji list | |
{%youtube youtube_id %} | Externals | ||
$L^aT_eX$ | LaTeX | ||
:::info This is a alert area. ::: |
This is a alert area. |
On a scale of 0-10, how likely is it that you would recommend HackMD to your friends, family or business associates?
Please give us some advice and help us improve HackMD.
Do you want to remove this version name and description?
Syncing
xxxxxxxxxx
Git: Branches, merging, rebasing
Wojciech Hardy
link: https://hackmd.io/@WHardy/RR25-git4
Branches
Part of the typical workflow
Source: Harness
Branches – advantages
Main branch keeping a clean history of releases
Room for experimentation without breaking anything
Simultaneous work for many people
Easy to reverse work on a feature and continue with main
Separate teams can work at the same time on: larger patches, hotfixes, new features, optimisation, etc.
Branches can have branches
(Source: Git Better)
Branches - how do they work?
Recall
master
ormain
branch is an indicatorYou create a new branch using
git branch <branch-name>
The new branch is a new indicator with its own
staging area
andworking directory
Scroll down to see what happens step-by-step
We start with one branch:
main
Recall:
HEAD
points to 'where we currently are'main
points to the latest commit on branchWe create a new branch that we name "feature" with
git branch feature
This creates a new indicator pointing to where we are.
But
HEAD
still points to themain
branch.We use
git checkout feature
to get to the new "feature" branchThe
HEAD
indicator goes to thefeature
branch.Commands
git branch [-l]
list/create/delete branchesgit checkout
move the HEAD pointer (switch between branches)git fetch
sync cached branches with remotegit rebase [x]
make a branch [x] start from a different pointgit merge [x]
combine two branches (by joining [x] to the current)Ok, let's do it, setup:
dev1
create a short history of commits (minimum two) and push themdev2
pull the informationLet's practice branching
feature1
git branch
option to do that)git checkout <branch-name>
)What's the new workflow?
You can add commits only to one of the branches at a time.
When you switch between branches, Git 'refreshes' your staging area and working directories to match
HEAD
.Let's start with a simple commit history:
New commits affect only the current branch
New commits affect only the current branch
In a big project, e.g. if you're the one working on the
feature
, changes tomain
might not be yours (other people push their features/fixes, etc. in the meantime).Let's switch to the
feature
branch withgit checkout feature
And add some commits while in there
One more
At any point you can return to the
main
withgit checkout main
Let's try working with branches
feature1
branch, add a file, stage and commit it. Then do apush
.Read the message and follow the hints.
main
branch and check what's going on in your working directory (e.g. in File explorer in Windows or Finder in MacOS).What happens? (put it somewhere as part of a new commit and push it)
git pull
and checkgit branch -l
git checkout feature1
and checkgit branch -l
againLet Dev2 do some work
main
branchfeature1
branchgit status
on themain
branch, push the changesgit status
on thefeature1
branch, push the changesThere are two main ways of putting the results back together
You might recall
merge
from how we unitedmain
withorigin/main
.While in
main
dogit merge feature
Recall:
merge
creates a new commit.You can still work on your
feature
branchIt doesn't include the merge (unless you also do
git merge main
while on the branchfeature
).Now that we know the basics, let's look again
Can you trace what's been going on in this project?
(Source: Git Better)
Let's go back to the terminal
feature1
branch to themain
branch usinggit merge <branch-name>
. Push.Conflict resolution is the same as we once did.
The second approach is rebasing
Rebasing is making your branch start from a different point
Let's first move to the branch we want to move (
git checkout feature
)Then
git rebase main
Important:
rebase
destroys original commits and creates new ones! Probably not a good thing if the original ones were shared with others or in a remote repository!Also, you don't need to rebase only within the same branch
Assume you start with this:
Possible troublesome situations:
feature1
branch proved a dead end, butfeature2
is promisingfeature2
is actually unrelated tofeature1
main
branch introduced something relevant tofeature2
The solution is to rebase
feature2
tomain
git checkout feature2
git rebase --onto main feature1 feature2
git rebase --onto <new-base> <old-base> <moved-branch>
Let's go back to the large graph
Can you see how
rebase
could come in handy for the lowerfeature
branch?(Source: Git Better)
Other fun stuff: detaching HEAD
Check this tutorial for an easy step-by-step description (the image comes from there)

Or try this one if you like X-Men - it's actually helpful (if you watched Days of Future Past)!
Typically HEAD points to the branch ("you're currently here").
Answering questions from last week
You can point it to an older commit instead:
git checkout 87ec91d
This puts you in a "detached HEAD state".
Answering questions from last week
Here you can:
git checkout master
(and turn it into a new branch if you want to keep it)
Assignment - imagine this is your repository
You do the following:
What's the new situation? Draw it on a piece of paper or send me a photo :)
Helpful links
For more on rebasing.
Read Merging vs rebasing on Atlassian for a nice description of both approaches (also their individual pages).
Also, take a look at
git bisect
, to understand when rebasing might be helpful.Also, take a look at
git stash
.