Git and Github. Simple recipes

When developing your own project, sooner or later, you have to think about where to store the source code and how to support working with multiple versions. In the case of working for the company, this is usually decided for you and you only need to support the adopted rules. There are several commonly used version control systems, and we will look at one of the most popular - this is Git and the Github service.

The Git system has emerged as a source control tool in the Linux operating system and has won many admirers in the Open Source environment.

Github service provides hosting (storage) of source texts both on a paid and on a free basis. This is one of the largest systems that Open Source users love. The main difference between the paid version is the ability to create private repositories (repositories) of source texts, and if you have nothing to hide, you can safely use the free version.

After you started work on the project and wrote some working prototype, you will have a desire to save the results of the work. It can also be useful if you want to continue working on another computer. The easiest solution is to save everything to a USB flash drive. This option works well, but if you have an Internet connection (and now whoever does not have one), then it’s convenient to use Git / Github systems.

This article will describe the basic scenarios for using Git / Github systems when working on a project in a Linux environment using the command line. All examples were tested on a system with Linux Ubuntu 14.04 and Git 1.9.1. If you use a different distribution, there may be differences.

Creating a local repository


Suppose your project is located in the / home / user / project folder . Before you save the source, you can see if there are any temporary files in the project folder and, if possible, delete them.

To view the folder, it is convenient to use the tree command , which will show not only the contents of each folder, but also the tree structure of the directories.

Temporary files often contain specific suffixes that make them easy to locate and subsequently delete. You can use the find command to search for such files . As an example, let's see how to find all the files that are generated by the Python compiler and have the extension .pyc

Go to the folder with the project / home / user / project :

cd /home/user/project

And we show the list of files with the extension .pyc :

find . -name *.pyc

This command will list all .pyc files in the current directory and its subdirectories. To delete the found files, just add the -delete switch to this command:

find . -name *.pyc -delete

It is highly recommended not to rush and immediately do not add this key. The first time you call the command to view the files and only making sure that nothing useful was added to the list, add the delete key.

Create a local repository in the project folder:

git init

After executing this command, a new folder named .git will appear . It will have several files and subdirectories. At the moment, the version control system does not yet see our files.

Adding files to the local repository


To add files, use the command:

git add readme

After the command is executed, the readme file will be added to the version control system (of course, if it was already this in the project). When you add a file, a hash value is generated that looks something like this:

9f2422325cef705b7682418d05a538d891bad5c8

The added files are stored in the .git / objects / xx / yyyyyyyy folder , with the first 2 digits of the hash used to indicate the directory, and the rest of the hash value is the file name. Our added file will be located here:

.git/objects/9f/2422325cef705b7682418d05a538d891bad5c8

What is easy to see with the command:

ls .git/objects

The file itself is an archive that can be easily unpacked and displayed on the screen, indicating the full value of the hash.

git cat-file -p 9f2422325cef705b7682418d05a538d891bad5c8

To add all files from the current directory, enter:

git add .

If you need to add files from the current directory and from all subdirectories, then use:

git add --all

In order to prevent temporary files from getting into the system, you can add them to a .gitignore file , which you need to create yourself and place in the project root directory (at the same level as the .git directory).

For example, if you add the following line * .pyc to the .gitignore file , then all files with the extension .pyc will not be added to the repository.

After adding files, all changes are in the so-called staging (or cached ) area . This is some temporary storage that is used to accumulate changes and from which the actual versions of projects ( commit ) are created.

To view the current status, you can use the command:

git status

After executing the command, we will see that our file is in the stage area :

new file:   readme

If you continue to make changes to the readme file , then after calling the git status command, you will see two versions of the file.

new file:   readme
modified:   readme

To add new changes, just repeat the command. The git add command not only adds new files, but also all changes to files that were added earlier.

git add readme

You can cancel the addition of the readme file in the staging area with the command:

git rm --cached readme

After the command is executed, the readme file will be marked as unchanged by the system.

Creating a project version


After we added the necessary files to the staging area, we can create a version of the project. With the command:

git commit -m "comment"

Each new version is accompanied by a comment.

After the commit, we can find two new objects inside the .git repository.

.git/objects/9f/2422325cef705b7682418d05a538d891bad5c8
.git/objects/65/7ab4c07bd3914c7d66e4cb48fe57f5c3aa7026
.git/objects/da/c6721c3b75fcb3c9d87b18ba4cef2e15e0a3d3

Let's see what's inside:

git cat-file -t 657ab4c07bd3914c7d66e4cb48fe57f5c3aa7026

The -t switch indicates the type of object. As a result, we see:

commit

For the second object:

git cat-file -t dac6721c3b75fcb3c9d87b18ba4cef2e15e0a3d3

Result:

tree

For the very first file:

git cat-file -t 9f2422325cef705b7682418d05a538d891bad5c8

We see:

blob

If we continue to study the contents of these files, we will find a tree structure. From each commit, you can follow the links for all changed files. For practical use, this is not very necessary, but it may be easier to understand what happens when working with the Git system.

The very first version cannot be undone. It can only be fixed. If you want to add changes to the latest version, then after executing the commit command , add the necessary changes and call:

git commit -m "comment" --amend

Or so:

git commit --amend --no-edit

The --no-edit switch is needed so as not to re-enter the comment.

You can view the changes you made with the last commit:

git show

Or so:

git show --name-only

The key --name-only is needed to show only the names of the changed files. Without it, a list of all changes will be displayed for each changed file.

If you continued to work and changed only those files that were already added to the system with the git add command , you can commit with one command:

git commit -a -m "comment"

To view a list of all commits, use the command:

git log

Or so:

git log --oneline

The --oneline key is needed to reduce the amount of information displayed on the screen. With this key, each commit is shown on one line. For instance:

2b82e80 update
657ab4c first

In order to view the changes for a specific commit, just add the hash value of the commit to the git show command , which can be obtained using the previous command.

git show 657ab4c 

To cancel the last commit (except for the very first one), you can use the following command:

git reset HEAD~1

In order to delete all files in a folder that are not related to the project and are not saved in the repository, you can use the command:

git clean -df

Creating a repository on Github


Until now, we worked with a local repository, which was stored in a folder on the computer. If we want to be able to save the project on the Internet, create a repository on Github. First you need to register on github.com under the name myuser (in your case, it can be any other name).

After registration, click the "+" button and enter the name of the repository. Select the type Public (the repository is always Public for the free version) and click Create .

As a result, we created a repository on the Github website. On the screen we will see instructions on how to connect our local repository to the newly created one. Some of the teams are already familiar to us.

Add a remote repository (via SSH) under the name origin ( you can use any other name instead of origin ).

git remote add origin git@github.com:myuser/project.git

We can view the result of the addition using the command:

git remote -v

If everything was done correctly, then we will see:

origin git@github.com:myuser/project.git (fetch)
origin git@github.com:myuser/project.git (push)

To unregister a remote repository, enter:

git remote rm origin

This may be necessary if you want to change SSH access to HTTPS . After that, you can add it again, for example, under the name github and the HTTPS protocol .

git remote add github https://github.com/myuser/project.git

The following command will bring in all the changes that have been made to the local repository on Github.

git push -u github master

The -u switch is used to establish a connection between the remote github repository and your master branch . You can transfer all further changes to the remote repository with a simplified command.

git push

Transferring the repository to another computer


Once the repository has been created on Github, it can be copied to any other computer. To do this, use the command:

git clone https://github.com/myuser/project.git

The result of this command will be the creation of the project folder in the current directory. This folder will also contain the local repository (i.e. the .git folder ).

You can also add the name of the folder in which you want to place the local repository.

git clone https://github.com/myuser/project.git 

Work with one repository from different computers


Several developers can work with one repository from different computers, or you yourself if, for example, you are working on the same project at home and at work.

To get updates from a remote repository, use the command:

git pull

If you changed your local files, the git pull command will throw an error. If you are sure that you want to overwrite local files with files from a remote repository, then run the commands:

git fetch --all
git reset --hard github/master

Instead github substitute the name of your remote repository, you register a team the git the push -u .

As we already know, in order to post changes to the remote repository, use the command:

git push

If the remote repository contains files with a newer version than your local one, the git push command will throw an error. If you are sure that you want to overwrite the files in the remote repository despite the version conflict, then use the command:

git push -f

Sometimes you need to postpone your current changes and work on files that are in the remote repository. To do this, postpone the current changes with the command:

git stash

After executing this command, your local directory will contain the same files as the last commit. You can download new files from the remote repository with the git pull command and after that return your changes that you postponed with the command:

git stash pop

Conclusion


We examined the basic scenarios of working with Git and Github systems. Each command above has significantly more keys and, accordingly, opportunities. Studying them gradually will give you the opportunity to easily protect your sources and concentrate more directly on development.

Also popular now: