Git and GitHub: Revision Control

Git Tutorials Playlist

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


Android Studio, WebStorm, other JetBrains IDEs

The playlist below explains how to use the JetBrains IDEs for git and github. Watch them if you are using Android Studio, or WebStorm, or PyCharm, or any one of the Jetbrains IDEs. They are all very similar when it comes to git.


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.com"

Initial Workflow

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

GitFlow Overview

The git commands you need are the following.

To create a branch named jmvidal do

git branch jmvidal

To checkout my branch

git checkout jmvidal

To keep updated on the shared code, do this often

git checkout master
git pull

To merge the jmvidal 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 jmvidal
git push

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

git checkout jmvidal
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 jmvidal branch do

git branch -d jmvidal

To delete my tracking branch origin/jmvidal do

git branch -d -r origin/jmvidal

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

git push origin --delete jmvidal


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 jmvidal 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

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

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.
  • A nice git GUI, that will let you do complex things, is SourceTree. Get it. Its free. You probably will not need it, but if things get messed up, it can save you a couple of hours of googling. Another nice one is gitkraken.
  • 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.
  • try.github.io a fun interactive site.
  • learnGitBranching another interactive site, about branching
  • feature-branch workflow