What is Git?
I’m gonna kick this off by emphasizing on the importance of Git stating that, I cannot imagine a world, where software development is done without a version control system like Git (it used to…).
Git is an open source distributed version control system, which is built for any size projects, small to very large; it’s very easy to learn and use; very small in footprint and with a very fast performance.
Basically, what it does is to keep track of any file changes, notifying us on any conflicts that might occur.
Imagine you are working within a team, and you are about to make a change on a source code file at the same time another team member is.
Which change do you keep? How do you become aware of this conflict?
Git job is to notify both of you about the different versions of the code; then you get the opportunity to discuss the conflict and find a solution.
Enough with the theory, let’s start with the basics.
For this post, I assume you are already into software development and have a test project (or maybe just a .txt file to edit and test with Git, would do as well).
Though, I’d recommend creating a new project for testing, as I do further below in the post.
First, we need to download and install Git on our local machine. You may find the appropriate version of Git for your OS and download it here.
After you download Git, run the executable and follow the instructions (Next, Next, Next, …) and you should be up and running in seconds.
Once you are done with the installation, open your terminal or command prompt and navigate to the root folder of a test project.
If you are new to Git I’ll advise creating a new test project that you don’t mind losing any files, until you get familiar with Git basics.
Now let’s confirm our Git works by checking its version with the following console command
If you got the version of your installed Git, then you are good to go!
Let’s initialize our new project Git repository
Now let’s create few files to work within our project. I’m going to create a readme file ‘Readme.md’ to describe our project and a text file named ‘exclude.txt’.
Exclude Files & Folders
Before we continue on Git operations, I want to introduce you to one very important file in Git, the ‘.gitignore’ file.
We use ‘.gitignore’ file to tell Git which files or folders we do not wish Git to monitor. That being said let’s create our ‘.gitignore’ file
Now you can write in ‘.gitignore’ file, the name of any file or folder within your project, to exclude from Git system.
In my example, I excluded the ‘exclude.txt’ file. Let’s have a look.
Currently, in our root project folder, we have the ‘Readme.md’, the ‘exclude.txt’ and the ‘.gitignore’ files. Let’s create another text file for our example. I’ll just name it ‘original.txt’.
Now, let’s check the Git status, where we get different information about our project Git repository. Just run the below command.
Here we get a lot of different information from Git, but for now, let us just focus on the red marked lines.
Firstly notice that the ‘exclude.txt’ file is not shown anywhere here, and that is because we added it to our ‘.gitignore’ file! Yay, exclusion works!
Now, the red files we see here are the three of the files we created earlier, which are marked by Git as Untracked files.
That means Git cannot monitor any changes done to those files. In order to allow Git to track changes for those files we need to add them in our Git repository we initialized earlier.
We can add files to our Git repo one by one or just telling Git to add all Untracked files in our project directory. This is called staging files.
If we check again the git status now we’ll see those three files in green fonts and categorized as ‘Changes to be committed’.
If we are done with making changes to our files, we may now proceed and commit those changes to Git.
What commit does, is to save those changes to our local Git repository. And we do that with the below command.
After the commit is done, if you check the status of Git you will see nothing there. Git will tell you there is nothing to commit and that the tree (meaning the project directories and files) is clean.
Now, let us test our version control by making some changes to our committed files. I’m going to write some more text to the ‘original.txt’ file and then check the Git status again.
In the status, you can now see that the ‘original.txt’ file is again marked in red!
This means you currently have a different version of that file from the one you have previously saved in Git repository.
If you agree with the changes on that file, you may then proceed with the same procedure of adding and then committing the file changes into your local Git repository.
Another handy tool is the command to monitor the local git log, which shows us our previous commits and where we currently are.
At this point, is natural to be wondering, “ok what if I accidentally commit some changes? Is there any undo command?”.
The answer is Yes. We can undo/reset our Git repository to an earlier version of our code removing any accidental changes we might have committed.
Below I have listed a few basic commands for resetting our git repository.
Add Remote Repository
Until now, all of the above operations have been done to our local Git repository. How do we share our code though with a team? The whole point of using Git anyway is that. To be able to collaborate with others on the same code base more efficiently.
We can do that by using a remote repository. We can set our remote repositories either in a local server or a cloud server.
For this post I’m going to use Github, which is the most popular cloud git repository to date (recently acquired by Microsoft).
So let’s share our test project in our Github account (for this I will assume you have already created a Github account and set up your profile there – its very easy don’t worry, just follow the instructions there).
At your Github account, click on the green button at the top left of the page, with the text ‘New’.
Now give the repository a name (usually your project name). You may additionally, provide a brief description of the project, and then click on ‘Create Repository’ button.
Now you are redirected to an empty remote repository, where we are going to share our test project to.
The only thing we now need from here is the URL of that remote repository. So, write the URL down and add a ‘.git’ at the end of the URL, in my case is ‘https://github.com/xnorcode/TestProject.git’.
After getting the remote git repo URL, we must set it to our local git with the below command.
Push to remote
Now that we have set our remote git repo on Github, we can proceed to share our local code base to the remote repo. Let’s push our code with the below command
And now we have shared our code with a public git repository on Github, on which we can invite others to collaborate with.
Sync Remote and Local Repos
One more command that we need to know of while collaborating with others on a remote git repo, is how to sync our local repo with the remote one.
So, for example, a team member made some changes to his local repo and then pushed it to the remote repo. What do we do?
We need to sync our local repo with the new remote version, to avoid working on an out-of-date version of the code.
There are different and more complex ways we can achieve this, but for now, I’ll just show you how to download the latest code from the remote git repo with the below commands
In this post, I covered all basic operations to get you started with Git. It is a must tool for any software engineer of today and I highly recommend that you learn and start using it if you are not yet.
Worth to mention that, there are different ways you may utilize Git, and not only for versioning your code. Be creative! 🙂
If you are familiar with Git and already know all of this, stay tuned for the next post with more advanced stuff!
That being said, I hope you liked this post and find it useful! Also, feel free to follow me and stop by to say hi on social media!