Demonstration¶
In this notebook we're going to try to demonstrate using some things with git
. Usually you'd do this in the terminal, not in a notebook, but it'll work for our purposes. First we'll make a directory to work in
(Note because we're executing this in a notebook, sometimes we need to use the '!' symbol to execute shell commands.)
You can click here to open this notebook in Google Colab, which will let you execute these cells!
# Note I'm loading the autoreload extension so that we can load functions from two git branches
%load_ext autoreload
%autoreload 2
mkdir demo
cd demo
/Users/ch/HST/repos/astronomy_workflow/docs/notebooks/1.0-basics/demo
! git init
Initialized empty Git repository in /Users/ch/HST/repos/astronomy_workflow/docs/notebooks/1.0-basics/demo/.git/
ls -a
./ ../ .git/
You can see we now have a .git
directory, which is going to do all the version controlling for us. Let's make a .gitignore file, and make a silly function in example.py
. Here we're using the %%writefile
magic command, which will let us write out the text in the cell to a file. Here we'll write a .gitignore
file, and an example.py
%%writefile .gitignore
.gitignore
__pycache__
Writing .gitignore
%%writefile example.py
def my_func(name:str):
print(f"Hello, {name}!")
Writing example.py
We can check that the function works by importing it
import example
example.my_func('Christina')
Hello, Christina!
Looks great, let's see what git
thinks
! git status
On branch master
No commits yet
Untracked files:
(use "git add <file>..." to include in what will be committed)
example.py
nothing added to commit but untracked files present (use "git add" to track)
git
thinks that a file has been changed, and it isn't tracking that file. We can tell it to track the file with add
! git add example.py
! git status
On branch master
No commits yet
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: example.py
Great, now, it knows about the file. Note that we're on the default master
branch. If we're happy with our function, we can commit it.
! git commit -m 'My first function'
[master (root-commit) d92de99] My first function 1 file changed, 2 insertions(+) create mode 100644 example.py
Commits need a message to go with them, and the -m
flag lets us put the message in the same line. Great! This file is now committed, and if we ever need to roll back to this version we'll be able to. I think we need a new function. Because this is a bit change, we should open a new branch before developing it.
! git checkout -b new-feature
Switched to a new branch 'new-feature'
Ok, now we can add our new functionality
%%writefile example.py
def my_func(name:str, language:str='Python'):
print(f"Hello, {name}! I like using {language}!")
Overwriting example.py
! git status
On branch new-feature
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git restore <file>..." to discard changes in working directory)
modified: example.py
no changes added to commit (use "git add" and/or "git commit -a")
git
knows we've changed the file. Let's add it and commit it. This time, I'm going to use git add .
to add all the files in the CWD.
! git add .
! git commit -m 'changed my_func to accept language keyword'
[new-feature a9b41ea] changed my_func to accept language keyword 1 file changed, 2 insertions(+), 2 deletions(-)
Great! Now we have a new commit on this branch! If we want to work with the old function, we can switch to the master
branch.
import example
example.my_func('christina')
Hello, christina! I like using Python!
! git checkout master
import example
example.my_func('christina')
Switched to branch 'master' Hello, christina! I like using Python!
Great, we can switch between branches to use different functionality. Maybe now we'd like to merge our work on new-feature
onto master
. Let's check we're on the right branch
! git status
On branch master nothing to commit, working tree clean
Looks like we're on master, in that case let's merge!
! git merge new-feature
Updating d92de99..a9b41ea Fast-forward example.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-)
There were no conflicts, and it seems like the merge was successful. Let's check
import example
example.my_func('christina', 'julia')
Hello, christina! I like using julia!
Yep, that looks great. Ok now we can remove out old new-feature
branch, since we've already merged it in.
! git branch -d new-feature
Deleted branch new-feature (was a9b41ea).
Great! Now we might want to push these changes online to a GitHub repo. If we go to GitHub and click New Repository, we'll have to fill out this form:
Now that we have the remote repository set up, we can add the remote to our git
.
! git remote add origin https://github.com/christinahedges/demonstration.git
Let's check it worked
! git remote -v
origin https://github.com/christinahedges/demonstration.git (fetch) origin https://github.com/christinahedges/demonstration.git (push)
Looks great. git
calls the first branch master
by default, but that has some problematic connotations, so GitHub and others recommend we change the name to main
.
! git branch -M main
! git status
On branch main nothing to commit, working tree clean
Now we can push our main
branch to the remote we set up, named origin
:
git push -u origin main