Git Basics: tracking files and adding commits
Tags: git, beginners, programming, tutorial
Published: 2022-05-19 00:52:46

After downloading and installing [Git](https://git-scm.com/) 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"
```

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
```

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
```

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" 
```
- 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](https://dev.to/helderburato/patterns-for-writing-better-git-commit-messages-4ba0).

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](https://github.com/). 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
```

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
```
 
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](https://git-scm.com/book/en/v2/Git-Branching-Branches-in-a-Nutshell). 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.