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 2023
Lecture 5: Branches
Slides: https://hackmd.io/@git-fall-2023/L5-branches#/
Repetition
Not starting with "R"
checkout
- go/move HEAD to branch or specific commit (hash)clean
- clear unstaged files--dry-run
to check what will happendiff
- compare--staged
- commit vs stagedHEAD
- commit vs working treelog
- history of commit tree--graph
- graphically see the branchesshow
- shows info for commitstash
- temporarily stores the staged changes to the working treeswitch
- go to branch only (more clear for this use)tag
- tag (like version number), naming commits (not branches)Repetition
Starting with "R"
rebase -i HEAD~3
- interactively rebase last 3 commitssquash
- summarize last 3 commitsreflog
- log of commits that changes the headreset
- ascheckout
but takes options to also do updates<filename>
- unstages the filerestore
- restore file in work dir--staged
- unstagesrevert
- makes inverse of the previous commit. The commit tree is not modified, rather two cancelling commits.rev-parse --short
- find short hash for references, likeHEAD~~
rm
- remove from repoObjectives
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. (Time goes rightwards)
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. (Time goes rightwards)
Usually, a branch is created to work on a new feature. Once the feature is completed, it is merged back with the master branch.
(Time goes right)
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):
or…
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 - Type along if you wish
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.
Comment: It is good practice to keep old branches for understanding of the development. Deletion could however be done for very evident mistakes or insignificant issues.
In a somewhat nicer format, it looks like this:
We commit stuff to both branches
(Time goes leftwards)
Merge 'cool-feature' to 'master'
Delete 'cool-feature'
(Time goes leftwards)
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
We continue in the same repository!
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 (
A
) in one branch but not the other, but the switch is allowed.Example - modified file
We continue in the same repository!
First commit the
newfile.txt
in the cool-feature branch to clean the environment.If we make changes to the file in one of the branches (go back to
cool-feature
) 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 (
M
) in one branch but not the other, but the switch is allowed.Example - uncommitted, conflicting changes
We continue in the same repository!
Assume two branches, "cool-feature" and "morefeatures"
Create the branch "morefeatures" without switching to it
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 and do not allow the switch.
Handling uncommitted changes
So, what can we do if there is a conflict?
Stashing
The command "stash" can be described as a drawer where you store uncommitted changes temporarily.
After stashing your uncommitted changes you can continue working on other things in a different branch.
The uncommitted changes that are stored in the stash can be taken out and applied to any branch, including the original branch.
Stashing, example (no type-along this time)
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.
Working with stashes (repetition)
You can have several stashes stored. To see them, use
Example:
Working with stashes - continued (repetition)
When you have done what you needed before committing the stashed changes you can reapply a stash (select branch first), using
which will apply the most recent stash. If you want to apply a different stash, you can name it.
Example:
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.
Merge strategies
The most commonly used
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'
(Time goes leftwards)
Rebasing 'bugfix' to the 'master' branch
(Time goes leftwards)
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
(Time goes leftwards)
Rebase B onto C
FF merge C into A:
(Time goes leftwards)
Cherry-picking (advanced)
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:
Cherry-picking — illustration (advanced)
Apply the commit Y to the master branch (called Y´)
(Time goes leftwards)
Take aways
git branch
git checkout
orgit switch
git merge
rebase
- like merge but end result is just one branchConflict?
stash
or discard (clean
) the changes before switching branch or do acheckout --merge
.Workflow merge
$ git merge <other-branch>
Conflict?
$ git merge --continue <other-branch>
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:
Fast-forward Merge (OK): This exercise will show an example where git can do a fast-forward merge. The exercise is in the subdirectory "1.merge-ok"
Recursive/ORT Merge (OK): In this exercise you will see an example where git can automatically merge two branches. This time git will use the recursive merge. The exercise can be found in the subdirectory "2.merge-ok-recursive"
Exercises
Merge (BAD): This exercise gives an example of a merge that cannot be done automatically with the merge command. The exercise can be found in the subdirectory "3.merge-bad"
Rebasing (OK): In this exercise you will try the command rebase and see that it succeeds. The exercise can be found in the subdirectory "4.rebase-ok"
Rebasing (BAD): This exercise again gives an example of rebasing two branches, but in this case the rebase fails. The exercise can be found in the subdirectory "5.rebase-bad"