Please open notebook rsepython-s1r8.ipynb
Branches are incredibly important to why git
is cool and powerful.
They are an easy and cheap way of making a second version of your software, which you work on in parallel, and pull in your changes when you are ready.
import os
top_dir = os.getcwd()
git_dir = os.path.join(top_dir, 'learning_git')
working_dir=os.path.join(git_dir, 'git_example')
os.chdir(working_dir)
%%bash
git branch # Tell me what branches exist
* master
Let’s create a new branch, and make some changes.
%%bash
git checkout -b experiment # Make a new branch
Switched to a new branch ‘experiment’
%%bash
git branch
* experiment
master
We are able to make commits to the the new branch:
%%bash
git commit -am "Add Cadair Idris"
On branch experiment
Untracked files:
wsd.py
nothing added to commit but untracked files present
If we now switch to the ‘master’ branch:
%%bash
git checkout master # Switch to an existing branch
Your branch is up-to-date with ‘origin/master’.
Switched to branch ‘master’
We view the contents of file Wales.md:
%%bash
cat Wales.md
Mountains In Wales
==================
* Pen y Fan
* Tryfan
* Snowdon
* Glyder Fawr
* Fan y Big
and compare this to the version on branch ‘experiment’.
%%bash
git checkout experiment
Switched to branch ‘experiment’
cat Wales.md
Mountains In Wales
==================
* Pen y Fan
* Tryfan
* Snowdon
* Glyder Fawr
* Fan y Big
We can see that these are the same. ‘experiment’ is a replica of ‘master’ at the point of branching.
We need to let the server know there’s a new branch, to do this we use:
%%bash
git push -u origin experiment
Branch experiment set up to track remote branch experiment from origin.
To git@github.com:UCL/github-example.git
* [new branch] experiment -> experiment
We use --set-upstream origin
(Abbreviation -u
) to tell git that this branch should be pushed to and pulled from origin per default.
If you are following along, you should be able to see your branch in the list of branches in GitHub.
Once you’ve used git push -u
once, you can push new changes to the branch with just a git push.
If others checkout your repository, they will be able to do git checkout experiment
to see your branch content,
and collaborate with you in the branch.
To list the remote tracking branches:
%%bash
git branch -r
origin/experiment
origin/gh-pages
origin/master
Local branches can be, but do not have to be, connected to remote branches.
They are said to “track” remote branches. push -u
sets up the tracking relationship.
We can view the details of the commits in list view by using:
%%bash
git branch -vv
* experiment 5f73fff [origin/experiment] Merge branch ‘master’ of github.com:UCL/github-example
master 5f73fff [origin/master] Merge branch ‘master’ of github.com:UCL/github-example
In addition to using git diff
to compare to the state of a branch,
you can use git log
to look at lists of commits which are in a branch
and haven’t been merged yet.
%%bash
git log master..experiment
Git uses various symbols to refer to sets of commits.
The double dot A..B
means “ancestor of B and not ancestor of A”
So in a purely linear sequence, it does what you’d expect.
%%bash
git log --graph --oneline HEAD~9..HEAD~5
* d7d7243 Add wales
* 79b1285 Add Helvellyn
* cdd35b8 Include lakes in the scope
* 76322e5 Add lakeland
But in cases where a history has branches, the definition in terms of ancestors is important.
%%bash
git log --graph --oneline HEAD~5..HEAD
* 5f73fff Merge branch ‘master’ of github.com:UCL/github-example
|\
| * 3c4a02f Add another Beacon
* | a5f66ac Add Glyder
|/
* 54a5484 Merge branch ‘master’ of github.com:UCL/github-example
|\
| * b3d5585 Add a beacon
* | b7eba49 Translating from the Welsh
|/
* 4bc25e9 Merge branch ‘master’ of github.com:UCL/github-example
* aed6a4d Add Scotland
If there are changes on both sides, like this:
%%bash
git checkout master
Your branch is up-to-date with ‘origin/master’.
Switched to branch ‘master’
%%writefile Scotland.md
Mountains In Scotland
==================
* Ben Eighe
* Cairngorm
* Aonach Eagach
Overwriting Scotland.md
%%bash
git diff Scotland.md
diff –git i/Scotland.md w/Scotland.md
index 36f83a1..44eb7ea 100644
— i/Scotland.md
+++ w/Scotland.md
@@ -2,4 +2,5 @@ Mountains In Scotland
==================
* Ben Eighe
-* Cairngorm
\ No newline at end of file
+* Cairngorm
+* Aonach Eagach
\ No newline at end of file
%%bash
git commit -am "Commit Aonach onto master branch"
[master 2919c37] Commit Aonach onto master branch
1 file changed, 2 insertions(+), 1 deletion(-)
Then this notation is useful to show the content of what’s on what branch:
%%bash
git log --left-right --oneline master...experiment
< 2919c37 Commit Aonach onto master branch
Three dots means “everything which is not a common ancestor” of the two commits, i.e. the differences between them.
We can merge branches, and, just as we would pull in remote changes, there may or may not be conflicts.
%%bash
git branch
git merge experiment
experiment
* master
Already up-to-date.
%%bash
git log --graph --oneline HEAD~3..HEAD
* 2919c37 Commit Aonach onto master branch
* 5f73fff Merge branch ‘master’ of github.com:UCL/github-example
|\
| * 3c4a02f Add another Beacon
* a5f66ac Add Glyder
%%bash
git branch
experiment
* master
Let’s delete the branch ‘experiment’:
%%bash
git branch -d experiment
Deleted branch experiment (was 5f73fff).
%%bash
git branch
* master
The branch has been removed locally, but what about remotely?
%%bash
git branch --remote
origin/experiment
origin/gh-pages
origin/master
As we can see, it still exists.
To remove the remote branch:
%%bash
git push --delete origin experiment
# Remove remote branch
# - also can use github interface
To git@github.com:UCL/github-example.git
- [deleted] experiment
To check the remote branch has been deleted:
%%bash
git branch --remote
origin/gh-pages
origin/master
production
branch: code used for active workdevelop
branch: for general new codefeature
branches: for specific new ideasrelease
branches: when you share code with others
Make some changes on one branch, switch back to another, and use:
git checkout <branch> <path>
To quickly grab a file from one branch into another.
Using git checkout
with a path takes the content of files.
To grab the content of a specific commit from another branch, and apply it as a patch to your branch, use:
git cherry-pick <commit>
Next: Reading - Git Miscellany