Hello everyone, I am Wolf King, a programmer who loves playing ball

Git is a distributed version control system


Git operations are often used in everyday work. But for many people, just come up to Git is very strange, operation is also very confused. This article is intended for beginners to understand the basics of Git and to master some common Git commands.

About Version Control

What is version control? Do I really need it? Version control is a system for recording changes in the contents of several files for future reference to specific revisions.

What is a distributed version control system

Distributed Version Control System (DVCS).

On systems like Git, Mercurial, Bazaar, and Darcs, the client does not just take a snapshot of the latest version of the file, but mirrors the original repository in its entirety. This way, if any of the servers used by the collaboration fail, they can be recovered later using any of the mirrored local repositories. Because each extraction is actually a full backup of the repository.

Further, many of these systems can be specified to interact with several different remote repositories. This allows you to collaborate with different work groups on the same project. You can set up different collaborative processes as needed, such as hierarchical model workflows, which were not possible in previous centralized systems. \

Git workflow

These are some simple and common commands, but forget about them and get to know these four proper nouns. \

  1. -Serena: I don’t know.
  2. Index/Stage: staging area
  3. Your Repository is in a local warehouse.
  4. Remote: indicates the Remote warehouse

The workspace

This is a directory where a repository is created and visible on the PC. For example, my test folder is a workspace:

The staging area

Git directory index file, the staging area will record git add file information (file name, size, timestamp…) , does not save the file entities, and points to each file entity by id. You can use Git Status to check the state of the staging area. The staging area marks what is managed by Git in your current workspace.

When you have completed a requirement or feature and need to commit to a remote repository, the first step is to commit to the staging area through Git Add and be managed by Git.

Local repository

It holds all versions of the object that have been committed and is older than the contents of the workspace and staging area.

After git commit, you can synchronize the index directory tree to the local repository. In this way, you can use Git push to synchronize the local repository with the remote repository.

Remote warehouse

The content of the remote repository may be modified by collaborating local repositories in multiple locations, so it may or may not be in sync with the local repository, but its content is the oldest.

summary

  1. Any object is created and modified in the workspace;
  2. Any changes are versioned from the moment they enter the index section;
  3. Changes can only be recorded in the repository if they are committed to the local repository;
  4. Share local changes with collaborators and push them to a remote repository for sharing.

The following diagram is a more direct illustration of the relationship between the four areas. If some commands are not clear, it will be explained in more detail in the next section.

2. Git command

Found a map on the Internet, a map of other people’s arrangement, very good, borrowed. Some common commands are explained in detail below. \

HEAD

Before mastering the specific commands, understand HEAD. \

Git branches are essentially just mutable Pointers to commit objects. How does Git know which branch you are currently working on? The answer is simple. It holds a special pointer called HEAD. In Git, it is a pointer to the local branch you are working on. Think of HEAD as an alias for the current branch.

add

The add command is very simple, the main implementation of the workspace changed content to the staging area, by Git management. \

  1. Git add. adds all files from the current directory to the staging area
  2. Git add dir Adds a specified directory to the staging area, including subdirectories
  3. Git Add file Adds a specified file to the staging area

Git add -A: git add -u: git add -u

Git add -a: commit all changes to the repository, including all directories in the current Git repository. Git add -u: Commit modified and deleted files, excluding new files (new) git add. : This action is related to the version of git:1.X version: Submit new file (new) and modified files, excluding deleted files- 2.Version X: Commit all changes as with Git Add-aCopy the code

commit

Git commit is the process of committing changes from the staging area to the local repository. Every time we use git commit we generate a 40-bit hash in the local repository. This hash is also called commit-id. Commit-id is very useful when rolling back. It is the equivalent of a snapshot that can be returned to at any time in the future with the command git Reset.\

  1. Git commit -m message Commits the staging area to the local repository. Message stands for the description message
  2. Git commit file -m message Commits the specified file in the staging area to the local repository
  3. Git commit — amend-m message uses a new commit instead of the last one

branch

When it comes to collaboration, it naturally involves branching, and for branching, there are roughly four operations: show a branch, switch a branch, create a branch, and delete a branch. \

  1. Git Branch lists all local branches
  2. Git branch -r lists all remote branches
  3. Git branch -a Lists all local and remote branches
  4. Git branch branch-name Creates a new branch but stays in the current branch
  5. Git checkout -b branch-name creates a new branch and switches to that branch
  6. Git branch –track branch remote-branch Creates a branch and sets up the tracing relationship with the specified remote branch
  7. Git checkout branch-name Switches to the specified branch and updates the workspace
  8. Git branch -d branch-name Deletes a branch
  9. Git push origin –delete branch-name

There are many operations on branches, but they are simple and easy to remember.

merge

Git Git merge is a command frequently used in Git. Many people think Git merge is a very troublesome thing. If you are not careful, you will encounter the problem of lost code, so they are afraid of Git.

The merge command merges different branches. As shown in the figure above, in an actual opening, we might cut a branch from the master branch, and then complete the development requirements. In the process, we go through the COMMIT records of R3,R4, and R5, and finally merge the development requirements into the master.

  1. Before git fetch remote merge, pull the latest code of the remote repository
  2. Git merge Branch Merge a branch into the current branch. A conflict occurs after a merge. You need to manually remove the conflict. This is mainly because two users have modified the same area of the same file.

rebase

Rebases, also known as derivatives, are an alternative to merging. \

In the beginning, we do git rebase dev on the new branch, so all new commits on the new branch are repeated on the master branch, and finally checkout switches back to the new branch. This is the same as with a merge, where the branch does not change before and after the merge. Git rebase dev, colloquially, means that the new branch wants to continue on dev’s shoulder. Rebase also requires manual conflict resolution.

Differences between rebase and merge

Now we have two branches,test and Master, and commit as follows:

      D---E test
     /
A---B---C---F master
Copy the code

Git merge test (git merge test)

      D--------E
     /          \
A---B---C---F----G   test, master
Copy the code

Git rebase test (git rebase test)

A---B---D---E---C'---F'   test, master
Copy the code

As you can see, the merge operation generates a new node, and the previous commits are shown separately. Instead of generating new nodes, the rebase operation fuses the two branches into one linear commit.

If you want a clean, linear history tree without merge commit, you should choose Git Rebase. If you want to keep a complete history and avoid the risk of rewriting commit History, you should choose Git Merge

reset

There are two ways to do this: \

  1. Modify the error and commit again
  2. Use the git reset command to undo the commit error

The first approach is more straightforward, but involves multiple commit records. Personally, I prefer the second approach. The wrong commit is not necessary to keep.

The reset command points the current branch to another location and changes the workspace and staging area accordingly.

  1. Git reset – soft COMMIT only changes the commit point. The contents of the staging area and working directory are not changed
  2. Git reset — Mixed COMMIT changes both the commit point and the contents of the staging area
  3. Git reset – Hard COMMIT The contents of the staging area and workspace will be changed to exactly the same state as the commit point
  4. Git reset — Hard HEAD returns the workspace to the state it was in the last commit

revert

Git Revert uses a new commit to erase any changes made to a historical commit. \

The difference between Revert and reset

  1. Git Revert rolls back the previous commit with a new commit, and Git Reset deletes the specified commit directly.
  2. In the case of the rollback, the effect is similar. However, there is a difference when you continue to merge older versions of the previous version in the future. Because Git Revert “neutralizes” the previous commit with a backward commit, this change will not occur again in the later merge of the old branch, reducing conflicts. However, git reset deletes a commit from a branch, so when merge with an older branch, the rollback commit should be introduced again, causing a lot of conflicts. For those who don’t understand this, read this article.
  3. Git Reset moves the HEAD back a bit, while Git Revert moves the HEAD forward, but the new commit is the reverse of what is being reverted.

push

Git push origin master: < git push origin master > Refs /for/master pushes the local master branch to the corresponding master branch on the remote host origin, where origin is the remote host name

  1. Git push Remote branch Upload the specified local branch to the remote repository
  2. Git push remote –force Push the current branch to the remote repository, even if there is a conflict
  3. Git push remote –all Push all branches to the remote repository

Other commands

  1. Git status displays files that have changed
  2. Git log displays the version history of the current branch
  3. Git diff shows the difference between the staging area and the workspace
  4. Git diff HEAD displays the difference between the workspace and the latest commit for the current branch
  5. Git Cherry -pick COMMIT Select a commit and merge it into the current branch

The above is about Git some common commands and detailed elaboration, I believe can have a preliminary understanding of Git.

Happy to output dry goods Java technology public number: Wolf King programming. There are a lot of technical articles, massive video resources, beautiful brain maps in the public account, might as well pay attention to it! Get lots of learning resources and free books!

Forwarding circle of friends is the biggest support for me!

\

Click “like and watch” if you feel something! Thanks for your support!