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
Introduction to Git –- Fall 2020
Lecture 5: Branches
Slides: https://hackmd.io/@hpc2n-git-2020/L5-branches
What is a Git branch?
Why use branches?
There are many uses for branches:
What is a Git branch?
Until now, we have worked with a repository that only have one branch, with the commits done one at a time:
In the above picture, the master branch points to a commit. The current position is HEAD.
What is a Git branch - basic concepts
Now we want to look at repositories with several branches:
Branches are used to create another line of development. They are "individual projects" within a git repository.
Usually, a branch is created to work on a new feature. Once the feature is completed, it is merged back with the master branch.
Branches: Creation
Creating a new branch does not change the repository, it just points out the commit.
Note that the branch is created from the current HEAD.
To create a new branch (called cool-feature in the following):
To move to another branch (switch):
If you wish to switch to a new branch that is not yet created, you can do so by adding the flag
-b
togit checkout
.To see which branch you are on:
Branches: merging, deletion
Example
This is on the master branch
We now merge the branches and check again
Now we can delete the new branch we had created, since all the content is now in the master branch.
In a somewhat nicer format, it looks like this:
We commit stuff to both branches
Merge 'cool-feature' to 'master'
Delete 'cool-feature'
Switching with uncommitted changes
As mentioned above, you switch between branches with:
What happens if you have uncommitted changes (and/or new files added) when you try to switch?
What if there is a conflict?
Example - new file
Here we create a new branch, switch to it, then add a new file. Then we switch back to the master branch without committing the changes:
Git warns that there is a file added in one branch but not the other, but the switch is allowed.
Example - modified file
If we make changes to the file in one of the branches but not on the other and do not commit it, then git will again warn:
Git warns that there is a file that is modified in one branch but not the other, but the switch is allowed.
Example - uncommitted, conflicting changes
Assume two branches, "cool-feature" and "morefeatures"
Switch to branch "cool-feature", add some text to a file, stage the file and commit it:
Switch to branch "morefeatures". Modify the same file, stage the file and commit it. Then try and switch back to the "cool-features" branch:
Now Git complains.
Handling uncommitted changes
So what can we do if there is a conflict?
Stashing, example
First do a
git status
in the branch where you may have uncommitted changes:You can see the dirty status.
To fix it, let us use
git stash
:Checking again with
git status
:You can now switch branches and work on something else.
Discarding uncommitted changes
If you do not want to stash your changes, but just get rid of them, you can use
git clean
.WARNING: This command will remove all non-tracked files in your current directory!
You can safely test which files will be removed by running:
Handling uncommitted changes - merging
--merge
(or-m
):Merging and merge conflicts
git branch
.Git can automatically try to merge when you give the command:
while standing on the branch you want to merge to.
Git has some merge strategies. The most commonly used are:
Merge conflicts, example
Here we create a merge conflict:
So Git complains
We can get some more information with the
git status
command:Looking inside the file myfile.txt:
Some "conflict dividers" have been added.
Resolving merge conflicts
Commands to help:
git status
git log --merge
git diff
git reset
If you made a mistake when you resolved a conflict and have completed the merge before realizing, you can roll back to the commit before the merge was done with the command
git reset --hard
.Workflow - merge goes well
Success!
Workflow - merge goes badly
Success!
Rebasing
Rebasing - illustration
Branch 'bugfix' was branched from 'master'
Rebasing 'bugfix' to the 'master' branch
Rebasing - continued
Assume a master branch and the branch "cool-features" and that you want to rebase the branch "cool-features" onto the master branch:
This works by
Rebasing vs. Fast-forward merge
Not the same! A rebase moves a branch from one base to another. A fast-forward merge moves a branch head from the current commit to a commit for a descendant.
Example:
Start
Rebase B onto C
FF merge C into A:
Cherry-picking
Basically, cherry-picking in Git means that you choose a commit from one branch that you apply to another.
Find the hash for the commit you want to apply, using
git log
.Then make sure you are on the right branch that you want to apply the commit to:
Now you execute the cherry-picking:
Exercises
Each of the exercises has a README.md file with explanations and descriptions of what to do. You can find all of them in the subdirectory 5.branches. You should do them in the below order: