Skip to content

Instantly share code, notes, and snippets.

@wenzeslaus
Last active December 27, 2015 21:39
Show Gist options
  • Save wenzeslaus/2cb7dbc9cdbb4a3867a2 to your computer and use it in GitHub Desktop.
Save wenzeslaus/2cb7dbc9cdbb4a3867a2 to your computer and use it in GitHub Desktop.
Git introduction and cheat sheet

Basic Git introduction

If there is a existing remote repository, clone the repository (supposing SSH access, using GitHub as an example):

git clone git@github.com:repositoryowner/repositoryname.git

Do all the changes in the repositoryname directory created by the git clone command.

Once you edit some files, use git commit to commit them. Use -a flag to commit all or specify the filenames to commit just some of them. Use -m "some commit message" to describe the changes you made. The fastest way to commit might be, for example:

git commit -am "updated for GRASS GIS 7"

If you want to provide a longer commit message, just omit the -m "..." and you will get an editor (nano text terminal editor by default on GNU/Linux) where you can write a commit message.

If you created a new file, you have to tell Git to start tracking it:

git add some_file.txt

If you want to delete or move files, use git rm and git mv.

To see what files where changed, added or deleted use git status.

To review the changes in the files use git diff.

The commit itself records the change locally but does not put the change to the remote repository. Once you are ready to share your changes with others use git push to put the changes to the remote repository. All commits which were not pushed yet will by pushed.

To get the changes of other contributors use git pull. You always have to pull before you push. It is often necessary to commit your changes before git pull otherwise Git will not be able to merge the incoming changes. If you get an editor with a message about merging, it is enough just to use the generated message by exiting the editor (using Ctrl+X in case the editor is nano). If the automatic merge is not possible, Git will put the conflicting parts into the files and mark them. Consult the further steps with other contributors.

Example repositories:
* https://github.com/pgRouting/pgrouting
* https://github.com/WhiteHouse/2016-budget-data

GitHub

https://github.com/

It allows to create free of charge Gist repositories which are visible to people with the URL. There Gits are useful for smaller projects. They can be cloned using Git (but I'm not aware of actual limitations of Gits; it would be better to check some documentation or terms of use).

GitHub Enterprise

We are not focusing on paid option in this document, however the main audience of this document has free (already paid) access to the this service, so we will have a detail look also at this option.

GitHub Enterprise is a GitHub instance inside an organization which is paying GitHub for a license. Depending on organization, the usage including read access will be limited to the organization. However, with high probability, this is usually true and access can be limited in organization itself. Sharing projects with you collaborators, especially those out of the organization, could be then cumbersome or impossible.

For example, NCSU GitHub (https://github.ncsu.edu) is this case and has all the drawbacks above.

The advantage of this option is that one can have unlimited number of private repositories with unlimited number of collaborators. However, size limits may apply (to repositories, not collaborators) depending on an organization.

When you need to collaborate with various people this is not an option for you, although there are still ways of sharing which Git itself provides. The advantage for you is that you can create private repositories to test various Git or GitHub features and it is not visible.

Gitorious

https://gitorious.org/

It's fully open source (AGPL), anybody can install it on his or her own server, so you are independent on a provider. There is nothing special about having own Gitorious in an organization, it is just another sever application.

They discussed their terms of use with the Free Software Foundation.

Bitbucket

https://bitbucket.org

Can import a repository from any Git online repository 9 (including those which requires authorization) and other online repositories. Can also import all account repositories from GitHub (probably only github.com; but not tested).

Dropbox, Ubuntu One, ownCloud or similar services including your own shared network folder

All these file cloud services are similar and free of charge to a certain extent, and all support all major platforms. Files are by default private but you can share them with unlimited number of people (at least I haven't heard about any limitation).

However, the ownCloud service is different because ownCloud is a free and open source project and there are different providers with different pricing (including free of charge plans). There always should be a possibility (supported by the software itself) to migrate smoothly from one provider to another.

All examples suppose that you are using unix-like command line (so, use Git Bash on MS Windows).

Using Git together with Dropbox

Note that this works also for other services and also any file-based backup ways such as USB memory sticks.

Create a bare repository in the Dropbox directory (this one will act as a remote repository):

mkdir my_project
cd my_project
git init --bare

If you already have some project with some files in a directory, go to that directory and run git commands to initialize the repository:

cd project_dir
git init
git add *
git commit -am "initial commit"
git remote add origin ~/Dropbox/my_project
git push -u origin master

Note that git add * will add all files in the directory. If you want to add only some specify their names instead of using *.

For the other (basic) workflow, you just need:

git add some_filename
git commit -am "describe changes here"
git push

All commands shall be executed in the directory with the project, not in the directory created in Dropbox directory.

Migrating repository to GitHub from another hosting option

Get the URL from the old repository host and do a bare clone of the repository (using --bare option):

git clone --bare the/repository/url/at/old/host

Create a (remote) repository at GitHub. Create the repository as empty repository without any files (without .gitignore, README (README.md/README.rst) and LICENCE files). Get repository URL from GitHub project.

Go to the directory with bare clone of your repository:

cd repository-name.git

Push the changes using the --mirror option to upload everything (all branches, tags etc.):

git push --mirror the/repository/url/at/new/host

Alternatively, if you don't want to migrate all branches but just master branch, you can use:

git remote set-url origin the/repository/url/at/new/host
git push -u origin master

Now you can remove the bare clone:

cd ..
rm -rf repository-name.git

It is advisable to mark the old remote repository as not active, read only or private in some way, add note to description or to README file. You can consider also removing the old repository to avoid confusion (the disadvantage is that you loose one backup but anyway this backup will be outdated soon).

If you or your colleagues have the existing clone (local working copy) of the repository, you can change the remote repository URL to the new one (assuming the remote repository is named origin in most cases):

git remote set-url origin the/repository/url/at/new/host

Alternatively, you can clone the repository from the new location and remove you old clone. Just be sure that you don't have any changes in you old clone (both committed and uncommitted) because you will loose them removing the old clone. While changing remote repository URL is safe because you just continue in the same directory with all committed or uncommitted changes.

Alternative guide is available here: https://help.github.com/articles/importing-an-external-git-repository.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment