What you need to know about Git and the basic Git commands

Tram Ho

1. What is Git?

Git is the name of a Distributed Version Control System (DVCS) that is one of the most popular distributed version management systems today. DVCS means a system that makes it possible for each computer to store different versions of a source code cloned from a repository, each change to the source code on the computer will be able to commit (commit) and then upload to the server where the main repository is located. And another computer (if they have access) can also clone the source code from the repository or clone a set of the latest changes on the other computer. In Git, the working directory on your computer is called a Working Tree.

In addition, there is another way to understand Git more simply that it will help you save versions of changes to the source code and can be easily restored without having to copy the source code. put it somewhere. And someone else can see your changes for each version, they can also collate your changes and include your version in their version. In the end it is all possible to publish changes to their source code in a source code repository.

2. What is Github?

When hearing about Git many people will immediately think of Github and maybe there will be some misunderstandings with them. Git is the name of a system model. As I explained above, computers can clone source code from a repository and Github is a public repository server service, each of which can create accounts on it to create their own repositories. I can work.

Link: https://github.com/join

Why use Git?

  • Git is easy to use, safe and fast.
  • You can make the team code workflow much simpler by combining branches.
  • You can work anywhere by simply cloning the source code from the repository or cloning a modified version from the repository, or a branch from the repository.
  • Ease of product deployment.

3. The basic commands often used

1. git config

Utility: To set your user name and email in the main configuration file. To check the email name and style in the configuration use:

$ git config -- global user.name

$ git config -- global user.email .

To set a new email or name:

$ git config -- global user.name = “Anh Nguyen”

$ git config -- global user.email = “ [email protected]

2. git init

$ git init

If you want to track an old project in Git, you need to stay in that project’s directory. This command will create a new directory called .git, which contains all the files needed for the repository.

3. git clone

Copy 1 git repository from remote source.

$ git clone https://github.com/user/repository.git

The above command will create a new directory with the same name as the one above for the repo.

4. git status

$ git status To check the status of the files you have changed in your working directory. Example: All last changes since the last commit.

Usage: git status in your working directory.

5. git add.

After you change the source code: add, edit, delete files, … You need to update to the Staging Area. To update all files:

$ git add .

6.git commit After the add command, you need to use Commit command to change the information to Local Respository:

$ git commit -m ”Đây là message, bạn dùng để note những thay đổi để sau này dễ dò lại”

Commit is an action that tells Git to save a snapshot of the changes made to the working directory. And the changed files and folders must be in the Staging Area. Each commit it will save the edit history of the code, along with the name and email address of the commit. In Git you can also restore a file in its commit history to divide it by another branch, so it’s easy to undo previous changes.

7. git push

After Commit command, new information is only updated to the Local Repository. If you want to update to the server, you must use the push command:

$ git push origin <name_branch>

Also, if a remote does not exist on the server, you need to add a new one first and then push:

$ git remote add origin <remote_url>

$ git push origin <name_branch>

8. git pull

Pull from the remote repository

$ git pull origin master

The above command will merge the new changes brought in from the remote host with the current branch on the local machine.

9. git branch

  • Using Git, you can create many different branches. This Git command is used to check the current branch:

$ git branch

-To create a new branch:

$ git branch <name_branch>

  • To transfer and create new:

$ git branch -b <name_branch>

10.git checkout Switch to another branch:

$ git checkout <name_branch>

11. git merge

After a while updating files and pushing to git on the new branch, now I need to merge the code back into the root branch (master). First, you need to checkout out of the current branch you want to merge to join the master branch, then use the merge command to merge the new branch into master:

$ git checkout master

$ git merge <new_branch>

12. git log

$ git log

The git log command will show you the commit history: know who committed, date, time, message of those commits.

13. git diff Viewing changes before push $ git diff This command helps you see what was changed between the current branch and the one before it.

14. git rebase

$ git rebase -i HEAD~

This instruction helps you merge commits. After the ~ sign is the number of commits you want to merge. After typing this command an editor window will appear. Change the pick character of the line after the first line to s and then save / end. Then, the editor to edit the commit explanation set for the commit after merging will be displayed, so edit save / end.

Share the news now

Source : Viblo