Skip to content

Git & GitHub cheat sheet

Igor Smirnov edited this page Sep 28, 2015 · 11 revisions

Introduction

Git

Git is a popular Version Control System. VCS is useful tool for teamwork: a lot of people can work in one project without disturbance of each other.

GitHub

GitHub is a Web-based Git repository hosting service. It offers all of the distributed revision control and source code management (SCM) functionality of Git as well as adding its own features. Unlike Git, which is strictly a command-line tool, GitHub provides a Web-based graphical interface and desktop as well as mobile integration. It also provides access control and several collaboration features such as bug tracking, feature requests, task management, and wikis for every project.

As you may noticed, our project use both Git and GitHub. Next you will see how to configure them for appropriate work.


How it works

Git

There is a special storage called "repository". It contains all enumerated versions (revisions) of projects, from first one to the latest. Allowed users can download ("clone") this repository to their local machine — this operation is called "cloning". Since cloned repository is equal to original one, user may do any operations (including modification, adding new files, etc) locally. Once user decides that his changes should be used by other team members, he performs commit. Commit is uploading of changes in the user's working copy to the repository. Every committed changes are received a unique revision number (SHA-1). To download these changes other users should update their working copies (pull updates to working directory).

The full description of GIT capabilities could be seen in official documentation

GitHub

GitHub makes Git easier to use in two ways. First, if you download the GitHub software to your computer, it provides a visual interface to help you manage your version-controlled projects locally. Second, creating an account on GitHub.com brings your version-controlled projects to the Web, and ties in social network features for good measure.


Setting Up GitHub And Git For The First Time

GitHub sign up page Sign up on GitHub

First, you’ll need to sign up for an account on GitHub.com.

GitHub's signup page

It’s as simple as signing up for any other social network. Please, use <name>.<surname>@phystech e-mail account for registration. Also, don't forget to point out you name and surname in account settings.

Please, send notification to [email protected] - I will add your account to project collaborators list.

GitHub profile example

You could stop there and GitHub would work fine. But if you want to work on your project on your local computer, you need to have Git installed. In fact, GitHub won’t work on your local computer if you don’t install Git. Below you will see receipts for Linux and Windows (source: https://git-scm.com/book/en/v2/Getting-Started-Installing-Git)

Installing on Linux

If you want to install Git on Linux via a binary installer, you can generally do so through the basic package-management tool that comes with your distribution. If you’re on Fedora for example, you can use yum:
$ sudo yum install git

If you’re on a Debian-based distribution like Ubuntu, try apt-get:
$ sudo apt-get install git

For more options, there are instructions for installing on several different Unix flavors on the Git website, at http://git-scm.com/download/linux.

Note: Our project uses GitHub for code hosting. To work with it Linux users might be required to generate SSH keys. Follow this procedure if you face any troubles with cloning project repository at first time

Installing on Windows

There are also a few ways to install Git on Windows. The most official build is available for download on the Git website: http://git-scm.com/download/win. Note that this is a project called Git for Windows, which is separate from Git itself; for more information on it, go to https://git-for-windows.github.io/.

Another easy way to get Git installed is by installing GitHub Desktop: https://desktop.github.com/. The installer includes a command line version of Git as well as the GUI. It also works well with Powershell (for Windows), and sets up solid credential caching and sane CRLF settings. This option is preferable for project members.

Don't forget to set up authentication to GitHub from Git as well.

Git basic operations

Since Git was designed with a big project like Linux in mind, there are a lot of Git commands. However, to use the basics of Git, you’ll only need to know a few terms. They all begin the same way, with the word git.

git init

Initializes a new Git repository. Until you run this command inside a repository or directory, it’s just a regular folder. Only after you input this does it accept further Git commands.

git config

Short for “configure,” this is most useful when you’re setting up Git for the first time. Use this commands to specify your credentials (use credentials from your GitHub account):
git config --global user.name "Your Name Here"
git config --global user.email "[email protected]"

git help

Forgot a command? Type this into the command line to bring up the 21 most common git commands. You can also be more specific and type git help init or another term to figure out how to use and configure a specific git command.

git status

Check the status of your repository. See which files are inside it, which changes still need to be committed, and which branch of the repository you’re currently working on.

git log

Show repository's commit history - Date, commit message, commit id (SHA-1), user credentials. For example, if you want to view information about 10 latest commits, simply use git log -n 10.

git add

This does not add new files to your repository. Instead, it brings new files to Git’s attention. After you add files, they’re included in Git’s “snapshots” of the repository.

git commit

Git’s most important command. After you make any sort of change, you input this in order to take a “snapshot” of the repository. Usually it goes git commit -m “Message here” The -m indicates that the following section of the command should be read as a message.

git branch

Working with multiple collaborators and want to make changes on your own? This command will let you build a new branch, or timeline of commits, of changes and file additions that are completely your own. Your title goes after the command. If you wanted a new branch called cats, you’d type git branch cats.

git checkout

Literally allows you to check out a repository that you are not currently inside. This is a navigational command that lets you move to the repository you want to check. You can use this command as git checkout master to look at the master branch, or git checkout cats to look at another branch.

git merge

When you’re done working on a branch, you can merge your changes back to the master branch, which is visible to all collaborators. git merge cats would take all the changes you made to the cats branch and add them to the master.

git push

If you’re working on your local computer, and want your commits to be visible online on GitHub as well, you push the changes up to GitHub with this command.

git pull

If you’re working on your local computer and want the most up-to-date version of your repository to work with, you “pull” the changes down from GitHub with this command.

git clone

Special command to which you should pay attention, since our project relies heavily on its usage
Documentation - http://git-scm.com/docs/git-clone

Cloning is operation of copying a repository on local drive
git clone <repository-url> [<local-folder>]

For example, to clone our project repository to your harddrive type in command line:
git clone [email protected]:MIPT-ILab/mipt-mips-2015.git my_ilab_project
and you will get the latest version of the repository (called HEAD) in my_ilab_project folder. This will be a full copy of repository, hosted on GitHub.


Flow example

For practice, do a tiny exercise: add your name in members.txt file. It can be made by following flow:

  1. Sign up on GitHub

  2. Install Git / GitHub application (see wiki)

  3. In terminal/Git Shell clone our repo using this command:
    git clone [email protected]:MIPT-ILab/mipt-mips-2015.git

  4. Open members.txt in text editor. Enter your credentials as follows: №. Name Surname - e-mail (e.g. 3. Igor Smirnov - [email protected])

  5. Check how your repo status changed using git status command. Note, that git identified changes in members.txt file and suggest you either add this changes to staging area or dismiss them (using git checkout -- members.txt). Add changes to staging area and commit your changes using git commit -m. Don't forget to add nice descriptions of your changes!

  6. You successfully commit your first changes to local repository, but they are still not visible to others (You can verify this by visiting project's GitHub page). To change this use git push command.

    a. If you faced with merge conflit this means that someone else pushed his changes before you - you don't have latest revision of repo and can't proceed fast-forward commit to it! Check documentation on how to deal with that.

    b. If you successfully pushed your changes visit GitHub project page to verify this (you may need to wait for a minute or two to see your changes)


Forking on GitHub

source: Fork a repo, Syncing a fork

A fork is a copy of a repository. Forking a repository allows you to freely experiment with changes without affecting the original project.

To fork our project simply follow this steps:

  1. On GitHub, navigate to our repo
  2. In the top-right corner of the page, click Fork. Fork button

That's it! Now, you have a fork of the original mipt-mips-2015 repository.

Keep your fork synced

In order to keep your repo up to date, it's good practice to regularly sync your fork with the upstream (original) repository, since during our course we will update main repo with Assignments (practical tasks). To do this, you'll need to use Git on the command line. Follow this steps to keep your fork synced:

  1. Set up Git

  2. Create a local clone of your fork: in your terminal/Git Shell use command (Note that you should use path to your repo):
    git clone https://github.com/YOUR-USERNAME/NAME-OF-FORKED-REPO

  3. In your repo's directory type git remote -v. You'll see the current configured remote repository for your fork:

  4. Now add our mipt-mips-2015 repository to remotes:
    git remote add upstream https://github.com/MIPT-ILab/mipt-mips-2015.git

  5. To verify the new upstream repository you've specified for your fork, type git remote -v again. You should see the URL for your fork as origin, and the URL for the original repository as upstream.

  6. Follow this link when you want to sync two repositories.

Clone this wiki locally