DEV Community

Cover image for Git Basics: tracking files and adding commits
Gabriela Cruz
Gabriela Cruz

Posted on

Git Basics: tracking files and adding commits

After downloading and installing Git on your computer, you may be asking yourself how to start using it effectively. I wrote this guide for myself a few weeks ago and thought about sharing it in some public platform in order to maybe help someone else, so here we go!

Before all, we have to get our configs done. Those configurations will be useful for when, later on, we make our commits. They will be "stamped" by Git with the email and name we'll set up now, as a form of identification. This definition can be done by the following commands.

git config --local user.name "Your name here"
git config --local user.email "your@mail.here"
Enter fullscreen mode Exit fullscreen mode

The next step for setting our project up, is initializing git in its folder. Go to your project folder and, in its terminal, run the following code:

git init
Enter fullscreen mode Exit fullscreen mode

This code causes a git repository to be initialized in our folder. All changed content in this repository will have its changes seen by Git (but not monitored, for now). In Git Bash, the terminal starts reporting the currently working branch.

So, by now, you may be asking yourself what actually causes our file changes to be tracked by Git. Running the command git status, we are able to see our commits and our "untracked files".

Tracking my repo files

git add filename.ext
Enter fullscreen mode Exit fullscreen mode

Running the command above, our example file named FILENAME had its changes logged by git. Note that with each change this command needs to be run again!

With git add ., we log all files changed at once. That's really useful, since we may alter many files at once and instead running an add for each one, we can simply run git add . and voilĂ .

At that point, the command git status we saw above starts to show changes to be commited.

Commiting files

 git commit -m "new commit" 
Enter fullscreen mode Exit fullscreen mode
  • Note that text between commas can be replaced with any message. There's some patterns envolving commit messages and you can see some nice guide about it by checking out here.

The command above commits all changes we made until now. The -m flag with a message is optional, but it's always a good practice to add it with a small explanation of the alterations you made.

Remotely

So now we got our local commits and changes being tracked and registered by Git. How do we get this online?

For this step, we first need to create a repository manually on a code versioning website. The most used and particularly my favorite is Github. Once you have your account created, you can create a repository and use its link to "connect" it to your local repository by running the following code:

git remote add origin https://github.com/username/respository.git
Enter fullscreen mode Exit fullscreen mode

Now let's break down each of the command parameters we just saw.

git remote manages our tracked repositories.
The parameter add add a remote named origin for the repository url that goes at the end of the command.

Now that our remote repository and our local repository can see each other, note that our changes made locally cannot be seen in the remote repository yet. Running the following command:

git push -u origin main
Enter fullscreen mode Exit fullscreen mode

Now we are "pushing" our local content to our remote Github repository. Note that main is the name of our remote repository, so this may vary depending on which branch you want to push. If you're still confused about branches, feel free to dig deep into the Git documentation. I plan on adding new parts to this guide soon, but the documentation will always be your most reliable source of information.

All in all, Git might seem a little daunting at first, especially if you've never dealt with terminal commands and code versioning before. Gradually, you will get used to the syntax of the commands and make your commits almost automatically.

Top comments (1)

Collapse
 
ivis1 profile image
Ivan Isaac

Excellent read