GIT what’s behind your usual operations
Tags: git, learning, beginners
Published: 2024-02-25 18:47:51

In my career as a developer and beyond, I have been able to see how much a guy approaching this world tends to focus his energy on learning Frameworks that will make him productive quickly. This approach has a big problem, it leads to neglecting the basics, both of programming and tools.

For this reason, I have recently set myself the goal of filling these gaps.

The first step was to "tell about" GIT starting from the common use of it. Often when asked "Do you know GIT?" we get a similar answer "Yes, I use it." My opinion is that using something is not the same as knowing it.


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/l265rkd2drgra5i4qvtk.png)

**Remote and Local Repository**
first of all we need to undestand our environment structure. When we work with GIT we have a distributed code. The code is on a Remote location and to develop we need to obtain assets present on it. To do this we can proceed cloning the remote repository to our local environment.
When we do this using che simple command "clone" GIT structured our environment creating three different, foundamental, elements.

- Working directory : where our working files are located.
- Local index (staging area): where Git tracks and saves changes.
- Local repository (.git dir): s the area that contains all of our commits

Another importat element is:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9tk7pe5ml6yel7nia7i5.png)

- Stash:is the area where Git stores changes that we don't want to commit and push, in short, set aside.

**Clone**
the first step to obtain the data present on a remote repository is to execute che clone command.
Basically you can run the command defining remote repository

```
git clone [repository url]
```
in this case git prepare your environment downloading the default branch repository (usualy main) in the actual directory.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/i3r29xl9u2rrouojl5vj.png)
you can also use a lot of options to clone a different branch, to stored files in a different directory, and so on. You can find all options in the ufficial git documentation.

https://git-scm.com/docs/git-clone

**Branch**
Often, during development, it is necessary to create a local branch based on the current control branch.
To do this, you can use the "_branch_" command.

```
git branch feature/myFeature develop
```

https://git-scm.com/docs/git-branch


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zf99na8edtg4j67raqag.png)

**Add and Commit**
When the developer creates or modifies some files, the data must be sent to the remote repository. To do this, the developer must first add them to the stage (local index) and then to the local _repository_.
The relative commands are:

```
git add [filename]
git commit -m "[commit message]"
```

if the dev creates a new file he has to use the "_add_" command to add the file in the stage, instead, when he modifies or deletes a file, the change is applied directly in the _stage_.

https://git-scm.com/docs/git-add
https://git-scm.com/docs/git-commit


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bhl31ug55q8eaosvi479.png)

**Push**
After committing, the developer must send the changes to the remote repository so that the changes are accessible to other team members. The specific command is "_push_".

```
git push <remote>
git push origin
```

https://git-scm.com/docs/git-push


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8ldup0yepdrnfg5d14f4.png)

**Fetch**
when other team member push modification, the developer can be need to see this infirmation without update it in his working directory. The command to do this is "_fatch_". It update only the Local Repository.

```
git fetch
```

https://git-scm.com/docs/git-fetch


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g9qmc4mtvldzn3k5qxwy.png)

**Pull**
After fetch, the developer can be update also the working directory, and the working branch, to use the new code. Simply he can run "_pull_". This command retrieve the data from the remote repository and store it in the working directory (and also update the local repository as fetch do)

```
git pull
```


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oxcc1jtrj2nwd7ylkfw0.png)

**Stash**
When you need to update the code in the working directory but have some uncommitted changes, you can preserve the code by moving it to the stash area. After the pull, you can retrieve the code and apply it more recently in the working directory.
Both actions are performed by the "_stash_" command.

```
git stash save "my_stash_name"
git stash pop
git stash apply
```

- apply command, saved stash overwrites files in the current working tree but leaves the stash history alone.
- pop command restores files but then deletes the applied stash.
https://git-scm.com/docs/git-stash


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/wplq008d8fju0bt2jme8.png)


I hope this article is helpful in clarifying the structure of GIT