saves a reference to it. If there’s a file that hasn’t been changed, then it stores a reference to the previous snapshot. We’ll get to how this is one of the most powerful and enabling features in Git when we dive deeper into the features Git provides.
There are three states for your files to be in when you’re working with Git. These three states are: modified, staged and committed.
When you make changes to a file in your working directory, it is then modified, but these modifications are not stored in the codebase. When you stage the file, these changes are marked to be saved in the next commit. When you commit the file, these changes are finally stored in your codebase.
There’s a fundamental thing to understand about Git, which is the fact that you can run two types of the repository. Usually, you have a local repository on your machine, that is your desktop or laptop, which is defined in your working directory, and a remote repository that is stored on the cloud.
The difference is, you can use the local repository and Git with all its features on your local machine, until and unless you want to share your code with others. It is only then that you need to make a remote repository of your code on GitHub/Gitlab etc and upload your code to that repository.
Once you do that, anyone with the access to your repository can download the code to his/her local machine and contribute to it.
Configuring Git: Getting Started
To get started, visit https://git-scm.com/ and download the appropriate version for your operating system. After running the installation, open Git bash or Git CMD. and CD ( change directory command) into an empty directory of your choice and run these commands. Replace the “John Doe” with your git username and “firstname.lastname@example.org” with your git account’s email id.
$ git config --global user.name "John Doe" $ git config --global user.email email@example.com
Here, git config is a command that asks your computer to make changes in config the file in the .git folder of the root of your system.
is a flag you pass to tell git that this is going to be your global git account and you are going to use this account to create git projects all over the computer. You can also use a git account specific for a directory. You can find details more about the initial setup and the necessary commands here.
Basic Git Commands
Now that you’re all set up and ready let’s run a
You’ll get an error that looks something like this. And of course, it’s natural, as we have not initialized a git repository inside that folder which represents our working directory.
In order to initialize Git, we will run the command
Now, if we run
again we should see that we’re on branch master (we’ll talk more about branches in a minute) and that this is the initial commit, and there’s nothing there to commit, as the project is empty for now.
Let’s create a text file and start creating some changes to simulate a real-world project. I’m going to do so inside of visual studio code, you can pick any IDE/editor of your choice, they all have similar features, but VS code is my favorite. Let’s add some text to the file:
Now, when we run the git status command we’ll notice that we get an untracked file.
This means we have some content inside of the project in the modified state and that we haven’t added yet to the staging area.
To add the files created and edited in the previous step to the staging area we will run the command:
If you have more than one file that you want to add the staging area, you can also run
which adds all files in the modifying area. Run git status again and you’ll find that the text file is now labeled as changes to be committed.
Now for the last step, let’s run the commit command to commit these changes. One thing about the commit command though, it has to be accompanied with a message, the reason behind that is that Git tries to enforce best practices, and it wouldn’t be hectic inside of a single project if changes were being made without a word of description said. So this is how we will run the git commit command:
git comm