Basics on Git and Github

Key Facts

Basic Commands

Remote Repository

In order to share code with others, we need to set up a remote repository. A popular free Git remote repository for open-source software is Github. Register for an account and the first thing to do is to set up a SSH key in the "account setting" menu under the "SSH Keys" tab. The goal for this step is so that Github can correctly authenticate the push (uploading the repository). Click "add new key" and copy your public key (id_rsa.pub) in the box. On Linux, RSA key pairs can be found under ~/.ssh/ or generated with ssh-keygen.

Before pushing a local repository to a remote repository, we need to add the target remote repository. Go to the working directory and issue a command:

    git remote add origin git@github.com:you_git_user/your_project_name.git

This basically says that we want to add a new remote repository called "origin" and the URL of the repository is as specified in the last argument. This URL can be found after a repository is created on Github.

To actually push a local repository, use

    git push -u origin master

which means we want to push (upload) the master branch (default branch) to "origin".

It may be possible that a push operation is rejected possibly because the master branch on the remote repository (called "origin/master") has a newer content than the local repository. In this case, it is necessary to do git fetch to pull the new content to the local repository, do the merging between master and origin/master branches, and try to push again. By default, git fetch will fetch all branches.

Branch and Merge

In Git, a branch is just a tagged snapshot. Internally a branch is a pointer to a commit snapshot. Branches are useful in many ways: testing a new idea, implementing new features, etc.

Source: