DEV Community

Cover image for GIT what’s behind your usual operations
Alberto Gianfranco Meleleo
Alberto Gianfranco Meleleo

Posted on

GIT what’s behind your usual operations

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

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

  • 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]
Enter fullscreen mode Exit fullscreen mode

in this case git prepare your environment downloading the default branch repository (usualy main) in the actual directory.

Image description
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
Enter fullscreen mode Exit fullscreen mode

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

Image description

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]"
Enter fullscreen mode Exit fullscreen mode

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

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
Enter fullscreen mode Exit fullscreen mode

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

Image description

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
Enter fullscreen mode Exit fullscreen mode

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

Image description

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
Enter fullscreen mode Exit fullscreen mode

Image description

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
Enter fullscreen mode Exit fullscreen mode
  • 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

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

Top comments (0)