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.

  1. cd ~/cs304
  2. mkdir personal-project
  3. cd personal-project
  4. git init
  5. echo "My personal project" > README.md
  6. ls
  7. git status
  8. git add README.md
  9. 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.

  1. git config --list
  2. git config user.email "your github username"
  3. git config user.name "your name"
  4. git config --list
  5. git commit -m 'added README' #will succeed

Let's do a bit more, just for practice:

  1. cp -r ~cs304/pub/downloads/flask-starter/* .
  2. ls
  3. git status
  4. git add app.py
  5. git status
  6. git commit -m "first version of the app.py file"
  7. git add templates/*
  8. git status
  9. git commit -m "all the templates"
  10. git status
  11. git add static/*
  12. git commit -m "first CSS file"
  13. git status
  14. git log
  15. ls -A    # Note the .git directory

Section 2 Using Branches as alternative versions

The idea here is to see branches as distinct versions

  1. git checkout -b foo   # create and switch to branch for feature 'foo'
  2. git branch
  3. echo "this is file foo" > foo.py
  4. ls
  5. git status
  6. git add foo.py
  7. git commit -m 'first version foo.py'
  8. git status
  9. git branch
  10. git checkout master
  11. git branch
  12. ls    # no foo.py!
  13. git checkout foo
  14. ls    # here it is!
  15. git branch
  16. git checkout master
  17. git merge foo
  18. ls


Section 3 Using Github and remote repositories

Learning how to work with Github, but still without collaborators

  1. Login to github and create a repository for your personal-project
  1. home page
  2. repositories
  3. click green "new" button
  4. name it using this form:
  5. click green "create repository" button
  1. 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
  1. git remote add origin https://github.com/your-account/your-repo.git
  2. git push -u origin master
  3. you'll have to type in your password. Eventually, we'll fix this.
  1. Go to Github and see the source code by clicking on the link to the repo
  2. Go to Tempest
  3. git push origin --all  # push all branches to github
  4. On Github, refresh the page, check the source code and switch between the two branches
  5. return to Tempest
  6. git checkout -b bar # new branch
  7. git status
  8. git branch # confirm we are on branch bar
  9. echo "This is bar" > bar.py
  10. git add bar.py
  11. git commit -m 'v1 of bar.py'
  12. git checkout master
  13. ls
  14. git merge bar
  15. ls
  16. 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:

  1. cd ~/cs304
  2. mkdir group-project
  3. cd group-project
  4. git init
  5. cp -r ~cs304/pub/downloads/flask-starter/* .
  6. git status
  7. git add *
  8. git status
  9. 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:

  1. git remote add origin https://github.com/captain-account/your-repo.git
  2. 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:

  1. Go to project settings
  2. Click on "manage access"
  3. give your password
  4. Click on green "invite collaborators" button
  5. add the github names of your collaborators
  6. (optionally) add my work github name: scott-anderson-wc

The teammates should login to github and accept the invitation.

  1. The notification is under the bell in the upper right
  2. click on it the notification
  3. click on the green "accept notification" button
  4. They can see the code on github
  5. 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.

  1. login
  2. cd cs304
  3. mkdir draft
  4. git clone https://github.com/captain-account/your-repo.git
  5. 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:

  1. echo "written by Ron" > ron.py
  2. git status
  3. git add ron.py
  4. git config user.email "rweasley@wellesley.edu"  # or github name
  5. git config user.name "Ron Weasley"
  6. git commit -m 'first version of ron.py'
  7. git pull origin master
  8. 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:

  1. 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:

  1. git checkout -b ron
  2. ls
  3. cat > shared.py

def foo():

    return 5

^d

  1. git add shared.py
  2. git commit -m 'defined foo to return 5'
  3. git checkout master
  4. git pull origin master
  5. git merge ron
  6. 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.

  1. git checkout -b her
  2. ls
  3. cat > shared.py

def foo():

    return 17

^d

  1. git add shared.py
  2. git commit -m 'my version of foo'
  3. git checkout master
  4. git pull origin master  # this pulls in Ron's code
  5. 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:

  1. cat shared.py
  2. git status
  3. code shared.py, resolve the conflicts and save
  4. git status
  5. git add shared.py
  6. git commit -m 'resolved conflict, foo should return 88'
  7. 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:

  1. git config user.name "Scott Anderson"
  2. git config user.email scott-anderson-wc  # your Git username
  3. ls -l ~/.ssh/  # the before picture
  4. ssh-keygen
  5. ls -l ~/.ssh/ # the after picture
  6. more ~/.ssh/id_rsa.pub

Copy that file to your clipboard, then go to Github, and

Go back to tempest and do

  1. echo "does this work" > newfile.py
  2. git add newfile.py
  3. git commit -m 'created newfile.py'
  4. git pull origin master
  5. 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:

  1. git config --list

We can retroactively change the URL like this:

  1. Go to github and Go to the page that allows you to download/clone the repo and grab the SSH url
  2. on Tempest, do:
  3. git remote -v
  4. git remote set-url origin git@github.com:<Username>/<Project>.git # paste here
  5. git remote -v
  6. git push origin master

Now the last step will work and will always from now on.

Section 7 Advice

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