Skip to content

Source Control Git Tutorial

tarimshahab edited this page Aug 23, 2015 · 8 revisions

Introduction

From the git webpage, Git is "a free and open source distributed version control system". This means that it helps you control different versions of your project, and easily jump to an older version if something goes wrong. It is better than having a files called 'project.c' , 'project_newer.c' , project_newer_2.c" to try to keep different versions of your project. While there are graphic user interfaces for using git, we will use the command line.

Installing git

Go to this site. Choose your OS, and you'll download the git installer. Run the installer and follow the instructions. Keep all the options default. After git is installed, search for and use Git Bash to open the command line. There are also GUIs for git, but its good to know the command line.

Common Commands

You can use various commands to pull/push and commit changes to a git repository.

  1. Two ways to get a repository:
    git clone [url] This commands clones a repository onto your local computer. (Make your way to the directory you want the git repo to be in using the cd [path] command in command line.) To clone the quickforms repository you would enter git clone https://github.com/uoForms/quickforms3.git

    git init [directory]. Allows you to create a completely new repo, rather than clone an existing one. Replace the [directory] with the path to your project to create a git repo for your project. If your project already has files, than you will need to add them (see below) to start tracking changes.

  2. git add [fileName] This command stages the given file, which means that the next time you commit, changes to this file will be recorded in the local repository. If you modify a file after adding it, make sure to add the file again or only the original changes will be updated. Ex git add READ_ME.txt

  3. git status This command will tell you the current 'status' of your project. It lists all the files you have staged to be committed, as well as any files you modified but haven't committed. Use this often.

  4. git commit -m "message" This command records the changes to your local repository. After you add the modified file(s), you should commit the changes. The "message" is a description of the changes your made.

  5. git remote show origin This command displays information about the remote repo called origin (origin is the shortname for the repo set up when you cloned from github)

  6. git pull After you've cloned a remote repo (the one on github), this commands goes to that remote repo and pulls down any changes you don't yet have. It then merges them to your local repo. (You can also use git fetch which gets all the changes but doesn't merge them so you can look through them and decide which to merge).

  7. git push origin master This command pushes all the modifications you've made to the remote repo (the one on github). Note that origin is the name of the remote repo that was set up when you cloned, and master is the master branch of that repo. You can push to another repo by using its name instead. If you try to push while there are changes on the remote repo that you don't have on your local repo, this command will fail.

Clone this wiki locally