Git is a very important tool in a programmer toolbox. It is one of the version-control systems. Because git has a lot of very cool and useful features, it is also the most common one. I recommend you start learning git. So, to help you with that challenge, today we’ll focus on the basic git commands. This article is the second one in the git series. Last time, I talked about what is git? and why as programmers we should use it daily. Now, I will talk about: How to create the first git repository? How to configure git locally and on an external server? What are the most important git commands and how to use them? We have lots to cover, so let’s start!
What is git?
Git is a distributed repository. This means that everyone in your team has the whole history of changes locally. You can save (commit) changes quickly without an Internet connection and you don’t need to synchronize with the rest of your team each time you change something. You can decide when you want to synchronize. The most important thing is that when something will go wrong on your local computer, your team still has a copy of the repository (in short repo). You will not lose your changes as long as someone on your team still has those changes locally. You don’t need to worry about the central repository, which can be broken at some point.
The last thing I would mention before we go to the details. Here, I will focus on pure git commands. There will be no information about external, graphic tools built on top of the git tool. Personally, in my work, I just use a command line. I like to have full control over things I do, and the command line gives me that. So, I never consider using external tools for git purposes. Plus, using the git command line gives me a better understanding of what is going on in each step of the process. I like that very much!
First, installation. To do that go to git documentation and follow the instructions there. After you finish this step, you can set up a basic configuration. When you’re using git, it’s very important, especially in teams, who did the change. So, you need to configure that locally on your computer. Here is the needed command:
git config --global user.name "Your Name" git config --global user.email firstname.lastname@example.org
I also recommend you to add colors to our command line. This will allow you to see different types of changes in different colors. For example, modified files will be in red.
git config --global color.ui true
Create first git repository
Git doesn’t track changes in your all files. You need to tell git which files in which directory are important to track. To do that you call:
This command will initialize local git repository. To allow other people in your team to see that repository, you need to put it to the server. There are many ways to do that. For example, you can use GitHub, Bitbucket, GitLab or even you can configure your git server. The most important here is to remember that git is not the same as GitHub. Git is a tool for managing different versions of code. This is also a communication protocol like https, ftp, or ssh. GitHub is a platform that uses git and allows us to create a remote repository.
Status of changes
You have your repository set. Now, you can try:
This command tells you which changes you already saved in your repository and which not. When you run it the first time all your changes shouldn’t be tack. That means none of your changes are saved in the repository. Let’s consider you have one Ruby file:
# hello_world.rb def hello "hello" end
This file isn’t tracked by git. In this situation you will see something like that:
git status # On branch master # # No commits yet # # Untracked files: # (use "git add <file>..." to include in what will be committed) # # hello_world.rb # # nothing added to commit but untracked files present (use "git add" to track)
Save your changes - first commit
Now is the time to tell git to track this file. To do that you need to call:
git add hello_world.rb
After this command, you can use
git status one more time and see the difference:
git status # On branch master # # No commits yet # # Changes to be commited: # (use "git rm --cached <file>..." to unstage) # # new file: hello_world.rb #
Your file is added to be tracked by git, but it’s not yet saved in the repository. To do that you need to commit. You can think about commit as putting something into a box. You have some stuff in your home, like pens, pencils or crayons. First, you decide which of them will go to your new Marie Kondo box. This is a
git add command. Then you put all of those chosen accessories to the box and you close that box. This is a commit. In one box you have one common category or one common piece. The same with commits. Let’s do that for your Ruby file:
git commit -m "Create a hello function"
git status shows:
git status # On branch master # nothing to commit (working directory clean)
hello_world.rb file are saved in the git repository. It’s time do to some tweaks in this file:
# hello_world.rb def hello "hello world" end
git status will look like this:
git status # On branch master # Changed but not updated: # # modified: hello_world.rb # no changes add to commit
If you want to create a new commit, you need to choose changes and add them to your new box:
git add hello_world.rb git commit -m "Change our hello function"
Synchronize changes with the remote repository
OK, you know how to create commits locally. Now, it’s time to push your code to remote git repository. This means that your changes will be no longer only on your computer, but also on some external server. I will show you, how to do that using GitHub, but you can also do the same on other platforms. First, you need to have a GitHub account. Then in GitHub find and use create new repository option. After you create this new repository, it’s time for your console:
git remote add origin link_to_your_remote_repository
This command will tell your local git tool, where is the remote repository. So,
git remote means that you save information about remote repository locally.
origin is the name for
link_to_your_remote_repository. Each time you will use this
origin name, this will mean your remote repository. If you are not sure what exactly put to this command, don’t worry, after you create your new repository, GitHub will show you all commands. You can copy it from there. Now it’s time to push your changes to this new remote repository:
git push origin master
This means push your code from branch
master to a remote repository with the name
origin. After this command, you will be asking about your GitHub user name and password. When you put this information to console, your code will be available on GitHub.
What is a git branch?
The git branch is a different version of your code. It’s like parallel universes with different stories. In one you can be a hero, in another one, you can be a normal citizen. Similar to your code. You can have many versions of your code, the main one is
master. I used this term in the previous section. This is just a name for a particular code version. If you create a new version of code, you should start a different branch. You give this branch a new name and you start coding. The branch can be also associated with trees. This is a good association. A tree has many branches like your code has many versions.
How to download my colleague changes?
First, your colleague needs to push these changes to the remote repository, the same as you did in the previous section. Then, you can pull those changes to your local repository. To do that you need to use the command:
git pull origin master
This means: pull all changes from branch
master on a remote repository with the name
origin to my local repository on local branch
master. You will be asking about your GitHub user name and password again. After the authorization, all changes will be downloaded.
Other articles in this series:
- What git tool is?
- How can I use git rebase?
- How to replace parent branch in git?
- How to use git rebase –onto?
If you want more information about the git tool, you can also check out the Git documentation.