2023. Installing and Using Git
Git, GitHub, and SourceTree


Use Git and GitHub as source control tool to manage code revisions.

1. What is Git?

Git is a distributed revision control and source code management system with an emphasis on speed.

2. Installing Git

2.1 Installing Git on Mac OS

Install Homebrew first if it hasn’t been installed yet on your Mac.

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Install Git with Homebrew

$ brew install git

Check versions

$ git --version
git version 2.11.0 (Apple Git-81)

Configure your Git username and email using the following commands:

$ git config --global user.name "Johnny"
$ git config --global user.email "csgeek@mail.com"

2.2 Installing Git on Ubuntu

Update packages

$ sudo apt-get update
$ sudo apt-get upgrade
$ sudo apt-get install -f

Install Git

$ sudo apt-get install git
$ sudo git --version
git version 2.7.4

3. Creating Repository on GitHub

To push your local git repository to remote git server, we need to use GitHub.

3.1 Creating GitHub Account

If you haven’t already, go to https://github.com/ to register and create a new account.

3.2 Repositories on GitHub

After login, you will see the page like below. For my GitHub, there are total 9 repositories. Seven of them are mine. One of them(EthanHao/Depaul) is shared by another GitHub Account. And another repository(github-example) is forked from some one’s repository. image

3.3 Creating New Repository

Click the green ‘New repository’ button. Provide name and description, select public or private. image

After creation, some instructions show how to submit files to this repository through command line. Next, we will do that. image

4. Pushing Local File to GitHub

4.1 Creating Local Repository

$ mkdir GitTutorial
$ cd GitTutorial/
$ vi hello.txt // input 'Hello Git!'
$ git init
Initialized empty Git repository in /Users/johnny/GitHub/GitTutorial/.git/
$ git status -s
?? hello.txt
$ git add hello.txt
$ git status -s
A  hello.txt
$ git commit -m "first commit"
[master (root-commit) 92b8723] first commit
 1 file changed, 1 insertion(+)
 create mode 100644 hello.txt

4.2 Pushing to GitHub

$ git remote add origin https://github.com/jojozhuang/github-test.git
$ git push -u origin master
Username for 'https://github.com': jojozhuang
Password for 'https://jojozhuang@github.com':
Counting objects: 3, done.
Writing objects: 100% (3/3), 228 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To https://github.com/jojozhuang/github-test.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

4.3 Checking on GitHub

File ‘hello.txt’ is added into repository ‘github-test’. image
Click on the file, check the content of it. image

5. Pulling Files From GitHub

Below commands show how to pull the files from GitHub repository ‘github-test’ to local Mac.

$ cd PullTest/
$ ls
$ git clone https://github.com/jojozhuang/github-test.git
Cloning into 'github-test'...
remote: Counting objects: 3, done.
remote: Total 3 (delta 0), reused 3 (delta 0), pack-reused 0
Unpacking objects: 100% (3/3), done.
$ ls
github-test
$ cd github-test/
$ ls
hello.txt

Next time, use the following commands to get latest commits from the same repository.

$ cd PullTest/
$ git pull origin master

6. Using Git with SourceTree

SourceTree is a free Git client for Windows or Mac. Instead of using command line, it provides an nice visual Git GUI.

6.1 Installing SourceTree

Go to https://www.sourcetreeapp.com/, download the installer, and follow the wizard to install it.

6.2 Cloning Repository from GitHub

New Repository->Clone From URL image

Input Source URL, which is the url of your repository on GitHub. And specify the local path and name. image

Click the Pull button on the top, then click the Ok button in the prompt dialog. image

File is pulled from GitHub. image

Check file on mac. image

6.3 Pushing Files to GitHub

Open the hello.txt file, add ‘Pushed by SourceTree!’ to the end, save the file. Switch to SourceTree, it detects the new changes. image

Stage the file by clicking the checkbox just in front the file name. Then, click on the Commit button on the top left, input the commit description, and commit. image

The staged file is committed. Click the Push button. You may be asked for GitHub username and password for the first time you push. image

After the push, check the history. You see the second commit is there. image

Switch to GitHub, find the file, you see that the content is changed. And click the commit description to see the difference of this commit. image

7. References