Git Activity
Section 1 Using Git Locally
This sequence of commands shows how to work using git just by yourself, locally. No github and no collaborators. I'll demo this with the "cs304guest" account.
- cd ~/cs304
- mkdir personal-project
- cd personal-project
- git init
- echo "My personal project" > README.md
- ls
- git status
- git add README.md
- git commit -m 'added README' # this will fail
The last step will fail because it needs to know who we are, etc. Let's fix that.
- git config --list
- git config user.email "your github username"
- git config user.name "your name"
- git config --list
- git commit -m 'added README' #will succeed
Let's do a bit more, just for practice:
- cp -r ~cs304/pub/downloads/flask-starter/* .
- ls
- git status
- git add app.py
- git status
- git commit -m "first version of the app.py file"
- git add templates/*
- git status
- git commit -m "all the templates"
- git status
- git add static/*
- git commit -m "first CSS file"
- git status
- git log
- ls -A # Note the .git directory
Section 2 Using Branches as alternative versions
The idea here is to see branches as distinct versions
- git checkout -b foo # create and switch to branch for feature 'foo'
- git branch
- echo "this is file foo" > foo.py
- ls
- git status
- git add foo.py
- git commit -m 'first version foo.py'
- git status
- git branch
- git checkout master
- git branch
- ls # no foo.py!
- git checkout foo
- ls # here it is!
- git branch
- git checkout master
- git merge foo
- ls
Section 3 Using Github and remote repositories
Learning how to work with Github, but still without collaborators
- Login to github and create a repository for your personal-project
- home page
- repositories
- click green "new" button

- name it using this form:

- click green "create repository" button
- Go to Tempest and follow their incantation for "…or push an existing repository from the command line", making sure (for now) to choose the HTTPS method: namely
- git remote add origin https://github.com/your-account/your-repo.git
- git push -u origin master
- you'll have to type in your password. Eventually, we'll fix this.
- Go to Github and see the source code by clicking on the link to the repo
- Go to Tempest
- git push origin --all # push all branches to github
- On Github, refresh the page, check the source code and switch between the two branches
- return to Tempest
- git checkout -b bar # new branch
- git status
- git branch # confirm we are on branch bar
- echo "This is bar" > bar.py
- git add bar.py
- git commit -m 'v1 of bar.py'
- git checkout master
- ls
- git merge bar
- ls
- git push origin master # again, you'll have to give your username and password.
Section 4 Collaborators
Here, we'll create a directory for our project and add our teammates as collaborators. Use chat to choose a "captain" for today. The captain will do the following:
- cd ~/cs304
- mkdir group-project
- cd group-project
- git init
- cp -r ~cs304/pub/downloads/flask-starter/* .
- git status
- git add *
- git status
- git commit -m 'starting project' # you'll have to set username and such
Now, go to Github and create a repo and then return to Tempest and do the incantation to push it to Github:
- git remote add origin https://github.com/captain-account/your-repo.git
- git push -u origin master # have to supply Github username/password
Now, return to Github and add your teammates. Your teammates should have sent their github names (or you can do this later). For this example, I will use my personal github account, scott-anderson:
- Go to project settings
- Click on "manage access"
- give your password
- Click on green "invite collaborators" button
- add the github names of your collaborators
- (optionally) add my work github name: scott-anderson-wc
The teammates should login to github and accept the invitation.
- The notification is under the bell in the upper right
- click on it the notification
- click on the green "accept notification" button
- They can see the code on github
- They can click the green "clone or download" button and note the https or ssh url. We'll use the https url this time; later we'll replace it with the ssh url.
Now, the teammates can go to their tempest account and copy the repo.
- login
- cd cs304
- mkdir draft
- git clone https://github.com/captain-account/your-repo.git
- cd into the repository directory
I'll do this in my "anderson" account, as opposed to "cs304guest".
Note that this will work even without inviting collaborators, because your github repo is *public*. (Since it's public, make sure you don't put anything personal or private in it. Just CS 304 project code and such.)
But only collaborators will be able to modify the code, so that's next.
Next, a teammate should make a modification on Tempest. In this example, I'm imagining a team of Harry, Ron and Hermione, with Hermione the captain. So, Ron is a teammate, and he does:
- echo "written by Ron" > ron.py
- git status
- git add ron.py
- git config user.email "rweasley@wellesley.edu" # or github name
- git config user.name "Ron Weasley"
- git commit -m 'first version of ron.py'
- git pull origin master
- git push origin master # ron will have to give his github username/password
Let's switch back to github on the captain's account or the collaborator's account, and see the upload.
For Ron's teammates to get his contribution (which is on the master branch), they will do:
- git pull origin master
I'll demo that with the "cs304guest" account, pulling in the update from me/ron.
Note that because it's a public repo, you'll never have to give a username/password to do a pull, but you'll always have to give one on a push
Section 5 Merge Conflicts
Now, an important issue is a Merge Conflict. In this scenario, Hermione (the captain) and Ron will both edit shared.py but *incompatibly*
Ron does:
- git checkout -b ron
- ls
- cat > shared.py
def foo():
return 5
^d
- git add shared.py
- git commit -m 'defined foo to return 5'
- git checkout master
- git pull origin master
- git merge ron
- git push origin master
Hermione will do the same thing, except she will define her branch with her own name, and her version of foo will return a different value.
- git checkout -b her
- ls
- cat > shared.py
def foo():
return 17
^d
- git add shared.py
- git commit -m 'my version of foo'
- git checkout master
- git pull origin master # this pulls in Ron's code
- git merge her
When she gets to the git merge, she should see:
Auto-merging draft/shared.py
CONFLICT (add/add): Merge conflict in draft/shared.py
Automatic merge failed; fix conflicts and then commit the result
She should do:
- cat shared.py
- git status
- code shared.py, resolve the conflicts and save
- git status
- git add shared.py
- git commit -m 'resolved conflict, foo should return 88'
- git push origin master
Section 6 SSH Keys
Having to give our username/password all the time is a pain. Let's fix that. I'll do these steps as the cs304guest account:
- git config user.name "Scott Anderson"
- git config user.email scott-anderson-wc # your Git username
- ls -l ~/.ssh/ # the before picture
- ssh-keygen
- ls -l ~/.ssh/ # the after picture
- more ~/.ssh/id_rsa.pub
Copy that file to your clipboard, then go to Github, and
- Using your icon in the upper right, go to settings
- go to SSH and GPG keys
- click "new SSH key"
- title the key "my account on tempest"
- paste the public key.
Go back to tempest and do
- echo "does this work" > newfile.py
- git add newfile.py
- git commit -m 'created newfile.py'
- git pull origin master
- git push origin master # this will FAIL
That last step will fail because we used HTTPS to clone. How do we know? Look at the remote.origin.url in git config:
- git config --list
We can retroactively change the URL like this:
- Go to github and Go to the page that allows you to download/clone the repo and grab the SSH url
- on Tempest, do:
- git remote -v
- git remote set-url origin git@github.com:<Username>/<Project>.git # paste here
- git remote -v
- git push origin master
Now the last step will work and will always from now on.
Section 7 Advice
- Each team member should work in their own branch
- commit in fine-grained steps, so you can always go back to earlier, working versions
- git pull origin master *before* git push origin master
- deal with merge conflicts locally
Tutorials and References
https://product.hubspot.com/blog/git-and-github-tutorial-for-beginners
https://www.jquery-az.com/git-push-command/
https://github.com/Kunena/Kunena-Forum/wiki/Create-a-new-branch-with-git-and-manage-branches
https://stackabuse.com/git-merge-branch-into-master/
https://stackoverflow.com/questions/5330145/when-to-delete-branches-in-git
https://swcarpentry.github.io/git-novice/08-collab/index.html
https://help.github.com/en/github/authenticating-to-github/adding-a-new-ssh-key-to-your-github-account
https://stackoverflow.com/questions/14762034/push-to-github-without-password-using-ssh-key
https://linuxize.com/post/how-to-change-git-remote-url/
https://help.github.com/en/github/using-git/setting-your-username-in-git
https://stackoverflow.com/questions/2432764/how-to-change-the-uri-url-for-a-remote-git-repository