When developing your own project, sooner or later, you have to think about where to store the source code and how to support work with several versions. In the case of work for a company, this is usually solved for you and you only need to maintain the accepted rules. There are several commonly used version control systems, and we’ll look at one of the most popular ones - Git and the Github service.
Git appeared as a source control tool in the Linux operating system and won many fans in the Open Source environment.
The Github service provides hosting (storage) of source texts both on a paid and free basis. This is one of the largest systems that users love Open Source. The main difference of the paid version is the ability to create private repositories (repositories) of the source code, and if you have nothing to hide, you can safely use the free version.
')
After you have started work on the project and wrote some working prototype, you will have a desire to save the results of the work. This can also be useful if you want to continue working on another computer. The simplest solution is to save everything on a flash drive. This option works well, but if there is an internet connection (and now anyone has one), then it is convenient to use Git / Github systems.
This article will describe basic scenarios for using Git / Github systems when working on a project in Linux using the command line. All examples were tested on a Linux system with Ubuntu 14.04 and Git 1.9.1. If you use another distribution, there may be differences.
Creating a local repository
Suppose your project is in the
/ home / user / project folder. Before you save the source, you can see if there are temporary files in the project folder and, if possible, delete them.
To view the folder, it is convenient to use the
tree command, which shows not only the contents of each folder, but also a tree structure of directories.
Often, temporary files contain specific suffixes by which they are easy to detect and subsequently delete. To find such files you can use the
find command . As an example, let's see how to find all the files that are generated by the Python compiler and have the extension
.pycGo to the folder with the project
/ home / user / project :
cd /home/user/project
And show the list of files with the extension
.pyc :
find . -name *.pyc
This command will list all files with the
.pyc extension 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 not to add this key right away. The first time to call the command to view the files and just making sure that nothing useful to add the delete key has appeared in the list.
Create a local repository in the project folder:
git init
After executing this command, a new folder will appear with the name
.git . It will contain several files and subdirectories. At the moment, the version control system still does not see our files.
Add files to local repository
To add files, use the command:
git add readme
After executing the command, the
readme file will be added to the version control system (of course, if it was already in that project). When a file is added, a hash value is generated, which looks 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 specify 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 is easy to unpack and display, indicating the full value of the hash.
git cat-file -p 9f2422325cef705b7682418d05a538d891bad5c8
In order 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, use:
git add --all
To prevent temporary files from
entering the system, you can add them to a
.gitignore file, which you need to create yourself and place in the root directory of the project (on the same level as the
.git directory).
For example, if
you add the following line in the
.gitignore file to
* .pyc , then all files with the
.pyc extension 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 are created (
commit ).
To view the current status, you can use the command:
git status
After executing the command, we will see that the
stage area contains our file:
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 adding the
readme file to the
staging area with the command:
git rm --cached readme
After executing the command, the
readme file will be marked as unmodified by the system.
Creating a project version
After we have added the necessary files to the
staging area, we can create a project version. Using the command:
git commit -m "comment"
Each new version is accompanied by a comment.
After the commit, we will be able to 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 examine the contents of these files, we will find a tree structure. From each commit, you can follow the links through all the modified 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 can not be canceled. It can only be fixed. If you want to add changes to the latest version, then after you execute the
commit command, add the necessary changes and call:
git commit -m "comment" --amend
Or so:
git commit --amend --no-edit
The key -
no-edit is needed in order not to re-enter the comment.
You can view the changes you made with the latest 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 have already been 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 in one line. For example:
2b82e80 update 657ab4c first
In order to view the changes for a particular commit, it is enough to add a hash value to the
git show command, which can be obtained using the previous command.
git show 657ab4c
To cancel the last commit (except the first one), you can use the following command:
git reset HEAD~1
In order to delete all files in the folder that do not belong 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 have been working with a local repository, which was saved 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 could be any other name).
After registration, press the
"+" button and enter the name of the repository. Select the type of
Public (the repository is always
Public for the free version) and click
Create .
As a result, we created a repository on Github. On the screen we will see instructions on how to connect our local repository with the newly created one. Some teams are already familiar to us.
Add a remote repository (via SSH) under the name
origin (instead of
origin, you can use any other name).
git remote add origin git@github.com:myuser/project.git
We can view the result of adding with the command:
git remote -v
If everything was done correctly, 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 . Then 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 next command will put all the changes that were made in 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 a remote repository with a simplified command.
git push
Transferring the repository to another computer
Once the repository has been created on Github, you can copy it to any other computer. To do this, use the command:
git clone https://github.com/myuser/project.git
The result of this command is the creation of the
project folder in the current directory. This folder will also contain a local repository (that is, a
.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 <myfolder>
Work with one repository from different computers
Several developers or you yourself can work with one repository from different computers, for example, if you are working on the same project at home and at work.
To receive updates from a remote repository, use the command:
git pull
If you change your local files, the
git pull command will generate an error. If you are sure that you want to overwrite local files with files from a remote repository, then run the following commands:
git fetch --all git reset --hard github/master
Replace github with the name of your remote repository you registered with
git push -u .
As we already know, in order to put the changes on the remote repository, use the command:
git push
If the remote repository contains files with a newer version than you have in the local one, then the
git push command will generate an error. If you are sure that you want to overwrite files in the remote repository despite the version conflict, use the command:
git push -f
Sometimes it becomes necessary 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 for the last commit. You can download new files from a remote repository with the
git pull command and then return your changes that you postponed with the command:
git stash pop
Conclusion
We looked at the basic scenarios for working with Git and Github systems. Each command above has significantly more keys and, accordingly, capabilities. A gradual study of them will give you the opportunity to easily guard your sources and concentrate more directly on the development.