Version control systems for today are almost mandatory for any project. One of the most popular version control systems is Git. Consider working with Git in conjunction with GitHub – the largest hosting server for the deployment of IT-projects of joint development. GitHub allows publishing open source projects for free.
In this article, we start to work with Git and GitHub on the Windows operating system.
What we need, in order to start our work with Git:
- Download the Git distribution for windows:
- Install Git on the user’s computer
- All installing options can be set “by default”
- After installation Git on the local computer, you need to make its interaction with the GitHub server. This can be done with several protocols, but we will only consider data exchanging using the ssh. Only this protocol allows both to receive project data from the server, and to send changed data to the server.
- First, we need to generate ssh keys to authorize on GitHub. This can be done using the Putty program.
- Run puttygen.exe
- Create a private key. This is the user key.
- Press “Generate”
- Move the mouse to generate a unique key
- For better protection of the key, you can enter a password, but then this password will need to be entered everytime you start working with Git.
- In the main menu choose Conversions – Export OpenSSH Key
- Save the key in a file with *.pem extension
- Press “Generate”
- Create a public-key. This key will be set on the GitHub server.
- Open the site https://github.com/ in your browser
- Create a developer account (if you do not already have one).
- Sign into your account
- Profile – Settings – SSH and GPG keys
- New SSH key
- Fill the fields with:
- Title: empty
- Key: copy here the text from puttygen.exe from the field “Public key for pasting into OpenSSH authorized_keys file”
- Save the public key.
- Profile – Settings – SSH and GPG keys
- First, we need to generate ssh keys to authorize on GitHub. This can be done using the Putty program.
- All the work with Git will be conducted from the command line console. For convenience, you can install a graphical shell, like TortoiseGit, but there’s nothing too complicated from working with the Git console.
- Run the Git Bash command shell by clicking on the shortcut created during the installation.
- To get started with Git, you need to do minimal initial settings
Checking the current settings:
1 |
git config --list |
First, you need to specify the user name and email (you need to specify your own):
1 2 |
git config --global user.name "Ivan Ivanov" git config --global user.email ivan_ivanov@yandex.ru |
- To exchange data between the local Git and the remote GitHub server, you need to configure the ssh-agent autorun when the Git Bash shell starts and register the previously created private key for authorization.
Create a .profile file in the c:/users directory/user_name by executing the command in Git Bash:
1 |
echo >> ~/.profile |
Add the following text to the created file: (starting the ssh-agent and registering the private key from the specified path). You need to specify the correct path to the private-key created earlier.
1 2 3 |
#! /bin/bash eval `ssh-agent -s` ssh-add d:/git/git_keys/ivanov.pem |
The setup is completed, now we can start working on the project.
- Create a new empty project on the GitHub site and clone it to the local user’s computer:
Sign into the GitHub and create a new project. In order to get its local copy on the user’s computer – you need to clone it. Create a directory for your projects on the local computer. Move to it in the Git Bash shell:
1 |
cd d:/Projects/Ivanov/ |
From the GitHub project get the path for cloning. Go to the project on GitHub and click the “Clone or Download” button, select “use SSH” and save the proposed path. It will look like something this: git@github.com: Ivanov/Test_project.git
Next, in the Git Bash shell, you need to execute the cloning command, specifying the project path.
1 |
git clone git@github.com: Ivanov/Test_project.git |
As a result, the Test_project directory will be created in your projects place on the local computer and project files from the GitHub will be downloaded to it.
- Let’s start working on the project itself:
Move to the project directory:
1 |
cd d:/Projects/Ivanov/Test_project/ |
To get information about the state of the remote repository (project on the Git Hub website) execute the following command:
1 |
git remote show origin |
Whenever you start working on a project, first of all, you need to get all the changes from the remote repository (in our case – the project on the GitHub website). This is necessary to work with the current version of the project code:
1 |
git pull origin |
After the data in the local project directory is updated, you can start working with the code – make changes, additions, create and delete files, etc.
- After you finish working with the project code, you must send all the changes to the remote repository.
In order to see if there were any changes that you would send to the remote repository execute:
1 |
git status |
If a new file was created (its name will be in the “untracked” section when executing git status), add this file to git (index it):
1 |
git add filename.ext |
Fixing changes in the local copy of the project (with a blank comment, the changes will not be fixed):
1 |
git commit -a -m 'comment' |
Sending the committed changes to the remote repository:
1 |
git push origin |
- Excluding files from working with Git.
If you want certain specified files not to be sent to the remote repository – create a “.gitignore” file in the project directory and add it to the project:
1 2 3 |
cd d:/Projects/Ivanov/Test_project/ echo >> .gitignore git add .gitignore |
Put the files and directories paths to this file, which should not be uploaded to the remote repository. For example, for a Python project (PyCharm IDE), the contents of .gitignore can be such (the specified directories will be ignored):
1 2 |
.idea/ __pycache__/ |
Commit .gitignore and send it to the repository:
1 2 |
git commit -a -m 'gitignore updated' git push origin |
- If the project is published, anyone can not make any changes (except the simplest ones) in it not to damage the published code. So any work with the published project should be based on the use of “branches”. “Branch” is a separate copy of all project files, into which you can make changes and which can be further merged with the main project after all changes completion, or deleted without consequences for the main code of the project if necessary.
Create a new branch (Git will switch to it). Instead of “branch_name”, you must specify a unique name for the new branch.
1 |
git checkout -b branch_name |
Now we must commit and upload to the remote repository only this branch (in the remote repository this branch will be automatically created):
1 2 |
git commit -a -m 'comment' git push origin branch_name |
When the work with this branch is finished, we need to merge this branch with the main code. To do this, switch to the branch to which you want to merge (master), merge current and finished branches, delete the finished branch, commit changes, update the code on the remote repository, and delete the finished branch on the remote repository:
1 2 3 4 5 6 |
git checkout master git merge branch_name git branch -d branch_name git commit -a -m 'comment' git push origin git push origin: branch_name |
If you do not need to build is this branch to the main code, you can simply delete it without merging:
1 2 3 |
git branch -d branch_name git commit -a -m 'comment' git push origin: branch_name |