DEV Community πŸ‘©β€πŸ’»πŸ‘¨β€πŸ’»

Cover image for [EN] Git. Guide from scratch
David Rojo
David Rojo

Posted on • Updated on

[EN] Git. Guide from scratch

In this article we will cover everything from creating a new repository to publishing it on a server and some commands that I use on a daily basis that may be useful.

Installed Git is a must.

1. ΒΏWhat is Git?

Git is the most widely used version control system in use nowadays, it has become the de facto standard for most companies, so it is important to know how to manage it efficiently.

A versioned project in Git is composed of a minimum of four areas:

  • Working Area: contains files that have been created, modified or deleted.

  • Stash Area: files that have been modified and we've set aside to do something with them in the future.

  • Staging Area: contains the files that we want to be added to the repository.

  • Local Repository: This is the modification history of our repository, it is made up of different branches that contain all the changes that have been made to a project.

The branches are copies of the project starting from the same codebase, their function is to implement changes to our repository.

Additionally, we can have one more area, called Remote repository, which is nothing more than a copy of our Local Repository area, located in one or more servers, be it GitHub, GitLab or BitBucket among others...

It is possible to have more than one Remote Repository for a single Local Repository.

2. Initial configuration.

The first thing we have to do after installing Git is to configure our credentials, for this we are going to make use of these three commands.

$ git config --global user.name "your_username" 
$ git config --global user.email "your_email" 
$ git config --global core.editor "your_text_editor" 
Enter fullscreen mode Exit fullscreen mode

Vim is the default text editor.

3. Initialising/Downloading a repository.

To start a repository we just need to run the git init command inside our project directory.

To download (clone) an existing repository, we must execute the command git clone <url_repository>.

Common commands.

Most of the commands contain extra parameters that for simplicity will not be explained in this article. The most common ones for everyday use will be explained.

4.1. Files.

  • git add <file>: allows us to move files from Working directory to Staging Area.

  • git stash: allows us to reserve changes from our Staging Area for later use.

  • git stash list : allows us to list the changes we have previously stashed.

  • git stash pop : allows us to retrieve the last change we saved.

  • git stash pop <index_stash>: retrieve the change with a given index .

  • git stash drop : allows us to delete a change we have reserved.

  • git commit -m "message": allows us to move files from Staging Area to Local Repository by adding a message.

  • git diff <filename>:shows the difference in a file from its previous version.

  • git reset: allows us to move all files from Staging Area to Working Directory.

  • git reset <filename>: allows us to move a file from the Staging Area to the Working Directory.

  • git reset --soft HEAD~<commits_number>: allows us to undo changes to our Local Repository x commits backwards without losing those modifications, where x=<commits_number>.

  • git restore <filename>: allows us to undo changes that have been made to a file.

4.2. Branches.

  • git branch: lists all the branches in our Local Repository and which one we are currently on.

  • git branch -d: remove a branch that has already been merged into another branch.

  • git branch -D:forces the removal of a branch, whether it has already been merged or not.

  • git switch <destination_branch_name>: allows us to switch to the destination branch.

  • git switch -c <branch_name>: create a new branch from the branch we are currently on and move to it.

  • git checkout <id_commit>: allows us to move to an existing commit.

  • git merge <branch_to_merge>: merge a branch into our current branch.

4.3. Remote Repository.

  • git remote add <link_name> <url_repository>: links our Local Repository to a Remote Repository.

  • git remote remove <link_name>: delete a link to a Remote Repository.

  • git remote -v: lists which Remote Repositories our Local Repository is linked to.

  • git push <link_name> <remote_branch>: allows us to upload commits from our Local Repository to a Remote
    Repository
    .

  • git push -f <link_name> <remote_branch>: allows us to force the upload of commits from our Local Repository to a Remote Repository.

  • git fetch: download metadata from a Remote Repository to a Local Repository.

  • git pull: is a combination of git fetch and git merge, it pulls the changes and metadata from a Remote Repository and merges them with our current branch in Local Repository.

4.4. Generic.

  • git status: shows the current status of our repository.

  • git log: lists the changes.


If you know of a useful command that you think should be on this list, don't hesitate to comment!

Top comments (0)

Take Your Github Repository To The Next Level

Take Your Github Repository To The Next Level πŸš€οΈ: A step-by-step guide on creating the perfect Github repository.