Git branches¶
Questions
- How to not ruin working code when adding new features?
- How to work on parallel tracks (many developers, versions)?
- How to fix mistakes?
Learning objectives of 'Branches'
-
learners can
- explain and evaluate the usefulness of branches
- create, switch and delete a
git
branch locally
Instructor notes
Prerequisites are:
- git basics
Lesson Plan:
- Total 50 min
- Theory 20
- Exercise 20
- Discussions 10 min
References
-
The main git book
-
'git best practices'
- Tsitoara, Mariot, and Mariot Tsitoara. "Git best practices." Beginning Git and GitHub: A Comprehensive Guide to Version Control, Project Management, and Teamwork for the New Developer (2020): 79-86.
- Tepavac, Igor, et al. "Version Control Systems, Tools and Best Practices: Case Git." CASE 27-Razvoj poslovnih i informatičkih sustava. 2015.
Branching and merging¶
Software development is often not linear:
- We typically need at least one version of the code to "work" (to compile, to give expected results, ...).
- At the same time we work on new features, often several features concurrently. Often they are unfinished.
- We need to be able to separate different lines of work really well.
Isolated tracks of work.
The strength of version control is that it permits the researcher to isolate different tracks of work, which can later be merged to create a composite version that contains all changes.
- We see branching points and merging points.
- Main line development is often called
master
ormain
. -
Other than this convention there is nothing special about
master
ormain
, it is just a branch. -
A group of commits that create a single narrative are called a branch.
- There are different branching strategies, but it is useful to think that a branch tells the story of a feature, e.g. "fast sequence extraction" or "Python interface" or "fixing bug in matrix inversion algorithm".
Three ways to name a branch
- Issues
- Features
- Personal
Typical workflows¶
One typical workflow:
git branch new-feature # create branch, switch to it
git add/commit # work, work, work, ..., and test
git switch master # once feature is ready, switch back to master
git branch # make clear which branch you are in
git merge new-feature # merge work to present branch
git branch -d new-feature # remove branch
See also
Graph alias
An important alias
- We will now define an alias in Git, to be able to nicely visualize branch structure in the terminal without having to remember a long Git command.
This will enable you to use git graph
for short
- It will now give you something like this:
$ git graph
* 000b440 (HEAD -> main) rm print
| * 4d4acaf (modularity) 4 modular files
|/
| * 2d4e252 (jupiter) add jupiter
|/
* b9465e4 (origin/main) planet.py documentation
* 6a416b5 add folders and planet code
gitGraph
commit id: "add folders and planet code"
commit id: "add planet.py documentation"
branch jupiter
checkout jupiter
commit id: "add jupiter"
checkout main
branch modular
checkout modular
commit id: "4 modular files"
checkout main
commit id: "rm print"
Show unstaged/uncommitted modifications
- When you are done editing the files, try
git diff
:
- You can use arrows or enter to scroll the output and quit with
q
. - You will see some things like this.
Output from 'diff'
diff --git a/ingredients.txt b/ingredients.txt
index 4422a31..ba8854f 100644
--- a/ingredients.txt
+++ b/ingredients.txt
@@ -2,3 +2,4 @@
* 1 chili
* 1 lime
* 2 tsp salt
+* 1/2 onion
diff --git a/instructions.txt b/instructions.txt
index 7811273..2b11074 100644
--- a/instructions.txt
+++ b/instructions.txt
@@ -4,3 +4,4 @@
* squeeze lime
* add salt
* and mix well
+* enjoy!
Test¶
- What is a branch?
- What is the problem that branches alleviate?
- What is the name/names of the most important branch?
- What is our git branching setup?
- What is the goal of that setup?
- Why do we use that setup instead of a different one?
- What is the purpose of each of those branches?
- Does creating a branch create a new version? Why?
- Does changing a branch change the content of your local computer? Why?
- Does deleting a branch create a new version? Why?
git push doesn't work with new branch
- you always have to pull before a push:
- You may need to be more detailed:
Exercises¶
Exercise 1: create, switch and delete a git
branch¶
Learning objectives
- Create, switch and delete a
git
branch - Build up experience using git without troubleshooting
gitGraph
commit id: "Before start"
commit id: "Branching version"
branch sven
checkout sven
commit id: "Modify my file"
commit id: "Delete branch" type: HIGHLIGHT
checkout main
commit id: "Another commit"
- For our shared GitHub repository, create a branch with your first name that is
unique, e.g.
sven
,sven_svensson
orsven_svensson_314
. You may branch of frommain
ordevelop
(if it exists). You may use the web interface (easiest!) or use the command line - On your local computer:
- update the repository
- switch to the new branch
- change the content of the repository, for example,
by creating a file in
learners/[your_name]/[your_name]_is_on_[your_branch_name]
- push your changes online.
- On GitHub, verify that your changes on your branch can be found online
- On your local computer, switch to the main branch, as we'll delete the branch you are on now
- Delete your branch (i.e. the one with the unique name). You may use the web interface (easiest!) or use the command line
- On your local computer, update your code
Answers
- For our shared GitHub repository, create a branch with your first name that is unique, e.g.
sven
,sven_svensson
orsven_svensson_314
. You may branch of frommain
ordevelop
(if it exists). You may use the web interface (easiest!) or use the command line
Click on 1, type your branch name at 2 (in this case, richel
), then click 3.
Done!
- On your local computer:
- update the repository
On your local computer, navigate to the folder of the shared project and update:
- On your local computer:
- switch to the new branch
Switch to the new branch, for example, richel
, by doing:
- On your local computer:
- change the content of the repository, for example, by creating a file in
learners/[your_name]/[your_name]_is_on_[your_branch_name]
This can be any change you'd like. To create a file under Linux (and maybe this works on other operating systems too), one can do:
After the change, commit these:
- On your local computer:
- push your changes online.
Do:
And your code may end up online.
If that does not work, do:
and try pushing again, maybe multiple times, as many people are pushing to the shared repo.
- On GitHub, verify that your changes on your branch can be found online
Make sure you look at the correct branch, as displayed at 1. Then your commit message shows up at 2.
- On your local computer, switch to the main branch, as we'll delete the branch you are on now
Switch to the main branch, for example, main
, by doing:
- Delete your branch (i.e. the one with the unique name). You may use the web interface (easiest!) or use the command line
Click on 'Branches', as shown in the image above.
Click on garbage bin, as shown in the image above.
The branch will now be deleted, as shown in the image above.
- On your local computer, update your code
Do:
Need a video?
See a video here
Summary¶
- Now we know how to save snapshots:
- And that is what we do as we program.
- Other very useful commands are these:
git init # initialize new repository
git add # add files or stage file(s)
git commit # commit staged file(s)
git status # see what is going on
git log # see history
git diff # show unstaged/uncommitted modifications
git show # show the change for a specific commit
git switch -b wild-idea # create branch, switch to it, work, work, work ...
git switch main # realize it was a bad idea, back to main/master
git branch -D wild-idea # it is gone, off to a new idea
git merge
Overview workflow
Parts to be covered!
- ☑ Source/version control
- Git
- We have a starting point!
- GitHub as remote backup
- branches
- ☑ Planning
- ☑ Analysis
- ☑Design
- ☑ Testing
- Different levels
- ☐ Collaboration
- GitHub
- pull requests
- ☐ Sharing
- ☑ open science
- ☐ citation
- ☑ licensing
- ☐ Documentation
- ☑ in-code documentation
Keypoints
- Initializing a Git repository is simple:
git init
. - Commits should be used to tell a story.
- Git uses the .git folder to store the snapshots.
- Don’t be afraid to stage and commit often. Better too o
- A branch is a division unit of work, to be merged with
- A tag is a pointer to a moment in the history of a proj
- A repository can have one or multiple remotes (we will
- Local branches often track remote branches.
- A remote serves as a full backup of your work.