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.
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.
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"
Vim is the default text editor.
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>.
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.
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
commitsbackwards without losing those modifications, where x=<commits_number>.
git restore <filename>: allows us to undo changes that have been made to a file.
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.
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
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 merge, it pulls the changes and metadata from a Remote Repository and merges them with our current branch in Local Repository.
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!