Git. A quick start to using basic operations with explanations. A detailed introduction to working with Git Git getting started

It is natural for people to resist change. If you weren't introduced to Git when you first started working with version control systems, you probably feel more comfortable with Subversion (SVN).

People often say that Git is too difficult for beginners. However, I beg to differ on this.

In this tutorial I'll show you how to use Git in your projects. Let's say you're building a project from scratch and want to manage it using Git. Walking through the list of basic commands will give you an idea of ​​how to host your code in the cloud using GitHub.

In this article, we'll talk about the basics of Git - how to initialize your projects, how to manage new and existing files, and how to store your code in the cloud.

We won't touch on the relatively complex parts of Git, such as branching, since this tutorial is intended for beginners.

Installing Git

On the official Git website there is detailed information about installing it on Linux, Mac and Windows. In our case, we will use Ubuntu 13.04 for demo purposes, where we will install Git using apt-get:

sudo apt-get install git

Initial setup

Let's create a directory within which we will work. Alternatively, you can use Git to manage one of your existing projects; in this case you won't need to create a demo directory like below:

mkdir my_git_project cd my_git_project

The first step is to initialize Git in the directory. This can be done using the init command, which creates a .git directory containing all the Git-related information for your project.

git config --global user.name "Shaumik" git config --global user.email " [email protected]" git config --global color.ui "auto"

It is important to note that if you do not provide your name and address Email, then the default values ​​will be used. In our case, the default values ​​would be the username donny and the email address donny@ubuntu.

In addition, we set for color user interface set to auto , which will cause the output of Git commands to be color-coded.

The --global prefix before the commands is to avoid having to enter these configuration commands the next time we run a Git project on our system.

Preparing files for commit

The next step is to create the files in the directory. You can use, for example, text editor Vim. Note that if you are going to add Git to an already existing directory, you don't need to do this step:

Check repository status

Now that we have a few files in our repository, let's take a look at how Git handles them. In order to check the current status of the repository, you need to use the git status command:

Adding Files to Git for Tracking

On this moment we don't have files to track with Git. We need to add files specifically to Git in order to tell Git what to track.

Add files using the add command:

Checking the repository status again, we can see that one file has been added:

To add multiple files, you can use the following command entry (note that we added one more file for demonstration purposes):

git add myfile2 myfile3

You can use git add recursively, but be careful with this command. There are certain files (such as compiled files) that are typically stored outside of a Git repository.

If you use the add command recursively, it will add all such files if they exist in your repository.

Deleting files

But running a simple git rm command will not only remove the file from Git, but also from your local file system! To

Git has stopped tracking the file, but in your local system the file itself is saved, run the following command:

git rm --cached

Commit changes

Once you've hosted your files, you can commit them to Git. Think of a commit as an imprint certain point, which you can return to to access your repository at this point.

You can attach a message to each commit, which is added using the -m prefix:

git commit -m "My first commit"

Give your commits useful messages because this will help you identify what you changed in that commit.

Avoid overly general messages like " Bugs fixed" If you have a task tracker, you can add messages like " Fixed bug #234».

It is good practice to use the branch name or feature name as a prefix to the commit message. For example, " Asset management: added function for generating PDF files assets” is a meaningful message.

Git identifies commits by adding a long hexadecimal number to each commit. As a rule, you don't need to copy the entire line; the first 5-6 characters are enough to identify your commit.

Note that in the screenshot our first commit is defined by code 8dd76fc.

Further commits

Now let's change a few files after our first commit. After changing them, we will see that as a result of executing the git status command, Git has detected changes in the files that it monitors:

You can check changes to tracked files made in the last commit using the git diff command. If you want to view changes to a specific file, use the git diff command :

You need to add these files again to make changes to the tracked files for the next commit. You can add all tracked files by running the command:

You can avoid using this command by using the -a prefix to the git commit command, which will add all changes to the tracked files.

This process, however, is very dangerous as it can harm the project. For example, let's say you open a file and change it by mistake.

If you place files selectively, you will notice changes in each file. But if you prefix -a to your commit, all files will be committed and you will not be able to detect possible errors.

Once you've placed your files, you can start committing. I mentioned that each commit can have a message associated with it, which we enter using the -m prefix.

However, it is possible to enter a message on multiple lines using the git commit command, which opens an interactive writing form:

Project management

To view the history of your project, you can run the following command:

This will show the entire history of the project, which is a list of all commits and information on them. Commit information includes the commit hash, author, time, and commit message. There are various options for git log that you can explore once you've mastered the concept of a branch in Git.

To view detailed information about a specific commit and the files that were changed, run the following command:

git show

Where this is the hexadecimal number associated with the commit. Since this tutorial is aimed at beginners, we won't cover how to go back to the state of a specific commit or how to manage branches.

Hosting code in the cloud

Now that you've learned how to manage the code on your system, it's time to host the code in the cloud.

Distributed version control systems (DVCS) are gradually replacing centralized ones. If you haven't used one of them yet, now is the time to try.

In this article I will try to show how you can quickly start experimenting with git using the github.com website.

This article will not discuss the differences between different DVCS. Also, working with git will not be discussed in detail; there are many good sources on this topic, which I will provide at the end of the article.

So, the site github.com is positioned as a web project hosting service using the git version control system, as well as social network for developers. Users can create an unlimited number of repositories, each of which is provided with a wiki, an issue tracking system, the ability to conduct code reviews and much more. GitHub is currently the most popular service of this kind, ahead of Sourceforge and Google Code.

For open-souce projects, use of the site is free. If you need to have private repositories, you can upgrade to a paid plan:

Let's start with registration. Follow the link github.com/signup/free and enter your data.
After registration, we are taken to the Dashboard of our account:

Now we don’t have a single repository, and we can either create a new repository, or fork from an existing someone else’s repository and lead our own development branch. Then, if desired, you can propose your changes to the author of the original repository (Pull request).

But first, let's install git and configure it to work with the site.

If you are working on Windows, download and install msysgit. This is the console version of git for Windows (further the story will be based on the example of this OS).
Instructions for MacOS X (eng)
Instructions for Linux (eng)
There should be no problems, just click Next everywhere. After installation, select from the Git Bash Explorer context menu:

Or via Git Bash.lnk in the folder with the installed program:

We enter our data and line break settings in the console:
git config --global user.name "your name"
git config --global user.email "your email"
git config --global core.autocrlf true
git config --global core.safecrlf true

By the way, I recommend taking a good interactive course on using git from the console. The course is completed in a few hours and provides the necessary basic skills.

For those who prefer gui, there are several such tools for working with git on Windows. The two main ones are SmartGit (cross-platform) and TortoiseGit. Both are good, and which one to use is a matter of taste. I will describe working with TortoiseGit.
For poppies there is also a choice of giu.

  • The official client from GitHub is still quite crude in my opinion.
  • GitX - I personally didn’t like it
  • GitBox - most follows the mac-way, I highly recommend trying it

About git in Russian:
“A successful branching model for git” - translation of a good English article
githowto.com interactive course on working with git from the console
“Why git” + discussion
“Git for those migrating from SVN” + discussion

Github is a very famous platform for storing, distributing and managing source code open projects. This service is used by many developers around the world, including large companies such as Microsoft, RedHat and many others, as well as hundreds of developers of many popular projects.

The platform provides opportunities not only for viewing code and its distribution, but also version history, collaborative development tools, tools for providing documentation, issuing releases and feedback. And the best part is that you can host both public and private projects on Gihub. In this article we will look at how to use Github to host your project. So to speak, github for beginners.

So, let's say you have your own project and you want to place its code on Github in the public domain so that other users can view it and participate in the development. The first thing you need to do is create an account.

1. Account creation

To create new account open on the website home page GitHub and immediately you can enter data for a new account. You need to provide a username, email and password:

When you are finished entering, press the button "Sign Up Free":

In the next step, you need to select the repository type. Public repositories are free, but if you want to create a private repository, the code from which will be available only to you, you will have to pay $7 per month.

Your account is ready and you will be redirected to a page where you can create your first project. But before you can do this, you need to confirm your Email address. To do this, open your Mailbox and follow the link in the email from Github.

No github setup is needed, just a few clicks are enough.

2. Creating a repository

On the page that opens, this is the main page for authorized users, click the button "Start a project":

You can immediately initialize the repository by creating a Readme file by checking the box "Initialize this repository with a README" at the bottom of the page. You can also select a license:

When ready, select "Create project", will be created new project with a README file containing a description and a license file.


3. Adding branches

Github branches allow you to work with multiple versions of a project at the same time. By default, when creating a repository, the master branch is created, this is the main working branch. You can create additional branches, for example, in order to test software before it is published to the master branch. This way, you can simultaneously develop the product and provide a stable version to users. You can also create separate branches for the program version for different systems.

The current branch is indicated in the upper left corner after the word "Branch". To create a new branch, simply expand this list and start typing its name:

The site itself will prompt you to create a new thread, select "Create branch".

Immediately after creation, you will be working with the newly created branch.

4. File changes and commits

Any changes to files on Github are made using commits. A commit is accomplished by making the fixes themselves and describing those fixes. This is necessary so that you know what and when you changed, and also makes it easy to track the team's work. The word commit can be translated as “fix”. That is, we can make changes to several files and then commit them. Let's change the README file as an example. To do this, find the button with a brush on the right side of the panel and click on it:

A text editor will open where you can enter the corrections you need:

After you have done everything you need, you need to fill out the field "Commit" at the bottom of the page. Briefly describe what has changed, and then click the button "Commit changes":

These changes will be made to the current branch of the project, since we are currently working with testing, the changes will be sent there.

5. Creating Pull Requests

GitHub for beginners may seem very complicated precisely because of such features, but it is very convenient once you figure it out. A Merge Request or Pull Request is a feature whereby any developer can ask another, such as the creator of a repository, to review their code and add it to the main project or branch. The Merge Request tool uses the diff comparison tool, so you can see all the changes, they will be underlined in a different color. Pull Request can be created immediately after creating a commit. Let's send a Pull Request from our testing branch to the main branch. First open the tab "Pull Request".

Click here "Create Pull Request":

In this window you can view all changes; now we see that the line has been added:

6. Review and approve merge requests

Now, on the same Pull Requests tab we see the newly created merge request and all we have to do is accept it by clicking "Merge Pull Request":

But if this request came from another person, you must check what he changed there and whether it is necessary. To do this, just click on the request description and you will see the already familiar change view window:

The code will then be imported into the master branch and the testing branch can be safely deleted.

7. Bug reports

Another convenient thing is that you can use GitHub not only for developing and managing code, but also for feedback from users. On the tab "Issue" Users can post messages about problems they encountered while using your product. Open the tab "Issues", and click on the button "New issue":

8. Releases

The last thing we'll look at today is releases. When the product has reached a certain stage, you can release a release so that users and you can be sure that everything is stable there and no one broke anything with an incorrect Pull Request in the Master. First you need to go to the main page of the project, then to the tab "Releases":

On this page you need to specify the version in the field "Tag Version", then the release name and a short description. If you have compiled archives with binaries, then you also need to attach them here. Then click "Create Release":

After creating the release, the following page will be created:

conclusions

In this article, we looked at how to use GitHub to host and manage your project. The whole system is in English, so basic knowledge languages ​​are very desirable, but even without them, working with github will not be very difficult. I hope this information was useful to you. If you are interested in how to work with Git from the command line, see the article for beginners.

This describes the practical part of using Git - installing it and registering it on the GitHub.com server.

GitHub.com is a service that offers storage of your code and data using a version control system Git. GitHub provides a free plan for storing 300MB of plaintext data. This means that any Internet user can download your data. You can also host repositories that are closed to others on GitHub by paying $7 per month. On a free GitHub account, by default, no one can change your data (they can only read it). But you can dictate which users of the GitHub system have write rights.

The article explains in detail how to configure Git on Windows OS and Linux OS.

Installing Git on Linux

I think there is no point in explaining to Linux users how to install Git - this is done differently on each system. On a Debian system (which is what I have), to install Git, you can use the command:

apt-get install git

Installing Git on Windows

Let's go to official page Git http://git-scm.com, click on Download for Windows. In the window that opens, click on Full installer for official Git. We launch the resulting exe file.

During the installation process you will be asked the following question:

I recommend choosing "Run Git from the Windows Command Prompt". All other options can be left as default. After installing Git, you must reboot or log out and log back in for the changes to the system PATH variable to take effect.

If we get version information, then Git is installed and working. If we receive information that the git program was not found, we figure out what we did wrong.

Setting up SSH keys

Before registering with GitHub, you must first generate an SSH encryption key. This key is required to quickly establish a connection to GitHub without entering a password. Without such a key, GitHub simply will not work.

Attention!

When the key is generated, you will be asked for a password. This is the access password to the private key, which is stored only on your machine and nowhere else. This password is set for maximum security, although you can do without it. You need to know that by setting a password for the private key, you will need to enter this password every time you connect to the GitHub server. Thus, when setting a password, all the convenience of using encryption keys disappears.

MyTetra users: interface for working with command line, which is used to call git during synchronization, cannot accept character input. Therefore, if you set a password, synchronization will not work.

Setting up SSH keys on Linux

IN operating system Linux first needs to look in the ~/.ssh directory. If there are files id_rsa and id_rsa.pub, then these are SSH keys. If there is no such directory or such files, then the keys need to be generated. We give the command:

Instead of [email protected] you need to indicate your email. During the key generation process you will be asked where to put the files; in response, simply press Enter. When prompted for a password, simply press Enter. After generation, the files id_rsa and id_rsa.pub should appear in the ~/.ssh directory; they will be useful to us in the future.

Setting up SSH keys on Windows

In the operating room Windows system An SSH key generator is included with Git. To generate keys, you need to run the file C:\Program Files\Git\Git bash.vbs. It can be launched as a regular exe file. The Git Console program will open. In it you need to give the command:

ssh-keygen -t rsa -C " [email protected]"

Be careful, copy-paste is buggy in this console, it’s easier to enter the command manually. We indicate your mailbox as your email. Upon request " Enter file in which to save the key" simply press Enter. When prompted for a password, " Enter passphrase " and " Enter same passphrase again " simply press Enter. During the process of generating keys, approximately the following information will be displayed in the console:

Generating public/private rsa key pair.
Enter file in which to save the key (/c/Documents and Settings/username/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Documents and Settings/username/.ssh/id_rsa.
Your public key has been saved in /c/Documents and Settings/username/.ssh/id_rsa.pub.
The key fingerprint is:
51:db:73:e9:31:9f:51:a6:7a:c5:3d:da:9c:35:8f:95 [email protected]

After executing this program, in the directory C:\Documents and Settings\username\.ssh There will be files id_rsa and id_rsa.pub, they will be useful to us in the future.

Register on GitHub.com

Now everything is ready for registration. Let's go to home page GitHub.com. The interface is a little confusing, so I’ll give you a couple of screenshots of where to click what. The design and layout can be changed at any time, so I am describing the logic of actions at the moment.

IN top menu find the item " Pricing and Signup" and click on it:

The selection page will open tariff plan. Select a free account" Create a free account":

Installing an SSH key on GitHub

Immediately after registration, you must register your public encryption key (public SSH key) in the GutHub system. To add a key, you need to click " in the upper right corner Account Settings":

In the window that opens, click on the menu item " SSH Public Keys", and press " Add Another Public Key". Two fields will appear - the name of the key ( Title) and the contents of the key ( Key).

In field Title You can write the name of the computer on which the public key was generated. You can write in Russian.

In field Key you need to insert the contents of the file id_rsa.pub. Do you remember what directory they are in? We go to this directory, open the file id_rsa.pub with any text editor (exactly with the extension .pub, do not confuse it). Select all the text, copy, and paste it into the field on the GitHub page Key.

After adding the key, the computer can connect to GitHub through the git program and no errors should occur.

Creating a repository on GitHub

Now it's time to create your first GitHub repository. The repository can be considered simply as a directory in which the synchronized files and subdirectories will be located. You need to create a repository in the GitHub web interface, and you can fill it with files and work with it using the git program on your computer.

To create a repository, you need to click " in the upper right corner Dashboard". In the window that opens you will see the item " Create A Repository":

So, we don’t need this point! This item does not open the repository creation dialog, but a help page. Instead of clicking on this item, look for an inconspicuous link below on the page " Create A Repository". It will open the dialog for adding a new repository.

In the dialog for adding a new repository, you need to fill in at least the project name field " Project Name". It is better not to use the Cyrillic alphabet in the project name, since the project name is in fact the name of the directory. To avoid problems, it is better that the project name contains only the Latin alphabet. After clicking the " Create Repository", the repository will be created.

A working link to the repository in the GitHub system is formed as follows. If you are registered as username and your repository is called reponame , then you can use the following links to access this repository:

In Git syntax:

[email protected]:username/reponame.git

In Https syntax:

https:// [email protected]/username/reponame.git

Working with a repository on GitHub using the Git program

From this moment on, the dance around the GitHub web interface can be considered complete. Further you can work only using the git program.

First, you need to do a little configuration of the git program: specify your username and email to the local git system. This is done with the following commands, which can be executed from any directory:

git config --global user.name "YourFullName"
git config --global user.email [email protected]

where instead of YourFullName you need to write your name, and instead of [email protected]- your email. These values ​​are used for GitHub login. Therefore, in place of YourFullName you need to indicate your login on GitHub, and in place [email protected] you need to specify the email that you entered when generating encryption keys.

After these settings, you can upload your files to the repository. Go to the directory with your project and give the commands:

git commit -a -m "first commit"

git remote add origin [email protected]:username/reponame.git

git push -u origin master

After these commands, copies of the files in the directory in which these commands were executed are created on the GitHub server. Then you can make commits, upload changes to the GitHub server, and read changes from the server. But that's a completely different story.

Resistance to change is a fundamental human trait. If Git didn't exist when you started working with version control systems, it's highly likely that you started with Subversion. People often say that Git is too difficult for beginners. However, I beg to differ with you.

In this article, I'll tell you how you can use Git to work with your projects. Let's assume that you are creating a project from scratch and want to use Git as your version control system. After introducing the basic commands, we'll look at how you can publish your code to GitHub.

This article will cover the basics - how to initialize a project, how to manage new and existing files, and how to store your code in the cloud. We'll skip some complicated stuff like branching since this article is aimed at beginners.

Installing Git

On the official Git website there is at various systems- Linux, Mac, Windows. In our case, we will be using Ubuntu 13.04, and we will be installing Git via apt-get.

Sudo apt-get install git

Initial configuration

Let's create a directory in which we will work. You can also use Git to work on an existing project, in which case you won't have to create a demo directory as described below.

Mkdir my_git_project cd my_git_project

The first step is to initialize the Git repository in the project directory. You can do this with the init command, which creates a .git directory with all the information about your project.

Git config --global user.name "Shaumik" git config --global user.email " [email protected]" git config --global color.ui "auto"

It is worth noting that if you do not specify your address and name, the default values ​​will be used instead. In our case, the default values ​​will be donny and donny@ubuntu.

We also set the interface color to auto so that the output of Git commands will be colored. We add the --global prefix to these commands so that these values ​​are used system-wide and do not need to be set on a project-by-project basis.

Preparing files for commit

The next step is to create some files. You can use any text editor for this. Note that if you are initializing Git on an existing project, you do not need to do this step.

Checking the status of the repository

Now that you have files in your project, let's look at how Git handles them. To check the current status of the repository, use the git status command

Adding files to Git

At this point, Git is not keeping track of any of our files. You need to specifically add files to Git for this to happen. To do this, we will use the add command.

Git add my_file

Having checked the status of the repository, we see that one of the files has already been added to it.

To add multiple files, we use the following (note that we added the first file earlier, so we only add the remaining two).

Git add myfile2 myfile3

It is possible to use git add recursively, but be careful with this command. There are some files (such as compiled programs) that should not be added to version control. If you use git add recursively, such files will also end up in the repository.

Deleting files

Let's imagine that you accidentally added a file to the repository that should not have been there. Or you want to remove a file from the version control system. In general, the git rm command will not just remove a file from the repository, but will also physically remove it from disk. To make Git stop tracking a file but keep it on disk, use the following command:

Git rm --cached [filename]

Commit the changes

Once you have added all the necessary files, you can commit them to Git. Think of a commit as a snapshot of the state of the project at a certain stage, which you can return to at any point in time and see the state of the project at that moment. Each commit has a message associated with it, which is specified as an argument after the -m prefix

Git commit -m "My first commit"

Please indicate a message that will contain useful information, since they help to understand what exactly was changed within a given commit. Avoid any general messages such as “Bug Rules”. If you have a bug tracker, you can specify a message like “Bug #123 fixed.” Good practice- indicate the name of the branch or improvement in the message. For example, “Asset management - added the ability to generate PDF based on an asset” is a clear and intelligible message.

Git identifies a commit with a long hexadecimal number. Usually, there is no need to copy the entire line; the first 5-6 characters are enough to identify a specific commit. From the screenshot you can see that our commit is identified by the number 8dd76fc.

Further commits

Let's change a few files after we've committed them. After we have changed them, git status will report that we have changed files.

You can see what has changed in these files since the previous commit using the git diff command. If you want to view changes for a specific file, you can use git diff<файл> .

It is necessary to index the changes and commit them. All changed project files can be added to a commit with the following command:

You can avoid using this command by adding the -a option to git commit . This command will index all changed files and commit them. But this approach can be quite dangerous, as you can mistakenly commit something you didn’t want to. For example, let's say you opened a file and accidentally changed it. When indexing changed files, you will be notified of changes in each file. But if you commit all the changed files without looking at help. git commit -a , then all files will be committed, including those that you did not want to commit.

Once you have indexed the files, you can start committing. As mentioned earlier, you can specify a message for a commit using the -m switch. But you can also specify multi-line comments using the git commit command, which opens a console editor for entering a comment.

Project management

To view the project history, you can use the following command:

It will display the complete history of the project in the form of a list of commits and information about them. The commit information contains the commit hash, author, time, and commit message. There are many types of git log command that you will need to become familiar with when using Git branching. To view the details of a specific commit and the files changed, run the following command:

Git show<хеш_коммита>

Where<хеш_коммита>- hexadecimal number associated with the commit. Because this manual Intended for beginners, we won't look at how to return the state to the time of a specific commit, or how to manage branches.