Skip to content

Git and GitHub: Revision Control

Git Tutorials Playlist

Below are my lectures on git. It is a playlist of videos. Watch them all.

Initial Setup

Set you name and email. Make sure the email you use is also listed in your github account.

git config --global user.name "Your Full Name"
git config --global user.email "youremail@email.sc.edu"

If your email in git does not match one of the emails associated with your github account, then when you push and look at the log in github your commit will not have an avatar and your name will be be clickable. If you see that blank next to your commit it means you did it wrong. Setup your git email as above. Make sure the same email address is one of the ones in your github profile. Then do another commit and push it. Make sure you now get an avatar and you can click on your username.

Initial Workflow

If you are learning git+GitHub for the first time, I recommend everyone only use the master branch. Just make sure you frequently commit+pull+push your changes. Some teams do this for the entire first semester (490) as they get comfortable with git.

IDE Support

I highly recommend you use git and maybe even parts of GitHub from inside your favorite IDE. It is much easier this way. They all support git and many also help you implement the GitFlow workflow. Below are the official documentation for some of the most popular IDEs.

GitFlow Overview

The git commands you need are the following. Let's say we are adding a logout button to our app.

To create a branch named logout_button do:

git branch logout_button

To checkout that branch

git checkout logout_button

To keep updated on the shared code, do this often

git checkout master
git pull

To merge the logout_button branch into master, without doing a Pull Request at GitHub, we do:

git checkout master
git pull #this should be a fast-forward merge
git merge logout_button
git push

To share the logout_button branch with others, so they can see what I am doing now

git checkout logout_button
git push

To pull in my teammate's branch from github to my laptop, the first time I do

git checkout --track origin/his-branch-name

I now have a branch called his-branch-name in my laptop which tracks the same named branch at github. Later on, if he adds more changes to his branch, pushes them, and I want to download them, I just:

git checkout his-branch-name
git pull

To delete my local logout_button branch do:

git branch -d logout_button

To delete my tracking branch origin/logout_button do

git branch -d -r origin/logout_button

To delete my logout_button branch at github do (this can also be done at github.com with just a click)

git push origin --delete logout_button

More advanced git tips

If you are in the middle of something, don't want to commit, but want to pull in the master branch just stash your code and pop it later. Assume that we are in the logout_button branch, then:

git stash #stash away everything, even my un-committed changes
git co master
git pull #do what you need to do, then when you are done
git stash pop #bring back my un-committed changes.

You have changed some files but decided all those changes are wrong. You just want everything to go back to where you where on your most recent commit. Simply

git reset --hard</pre>

However, if you also added files to your directory that you did not commit, the above will not delete those files. To delete all files in your git directory that are not in your git repo you use

git clean -fd

You did one or more commits that you regret, you want to get rid of them and just go back to a previous time. First find the commit you want to go back to using

git log
commit 87c379822da3bb45e37ac20b633ef75a03b7c92a
...other info
commit c3c0ecc10f968807b8cde449ec64907b79780a02
...other info
commit 5da03e94098a7d59f3586ea07ff7b4276b6fa4f3
...other info
commit 0318d2b5642dc01a359dbdc325e74f24d674634b</pre>

to go back to commit 5da03e94 just

git reset --hard 5da03e94

Your current branch and HEAD will now be at 5da03e94. It will be like the other two commits never happened.

Cloning the github wiki

To clone the github wiki git repo (that is, your wiki) locally you just add .wiki in the repo name. For example, if the repo is called demo and you use:

git clone git@github.com:SCCapstone/demo.git

to clone it, then you can clone its wiki using

git clone git@github.com:SCCapstone/demo.wiki.git

To add images, just add them in a new directory and then push. see this stackoverflow question for details.

Resources

  • The GitHub Guides have lots of video and text tutorials.
  • GitHub Skills has interactive lessons for learning git and GitHub.
  • Udacity has a free online class on How to Use Git and GitHub.
  • Read the Pro Git book. If you read it now, you will save yourself years of misery and depression. Also, you will be able to 'commit' without fear and, thus, pass this class.
  • Learn Git Branching is an interactive site that teaches you how branching and merging work.

Last update: September 14, 2023