Hello everyone, I am Wolf King, a programmer who loves playing ball
In this article, let’s learn about Git, a distributed version control system
Git operations are often used in daily work. But for many people, Git is new and confusing to use. This article is mainly aimed at the new contact Git, understand the basic principle of Git, master some common commands.
About Version Control
What is version control? Do I really need it? Version control is a system that records changes to the contents of several files so that future revisions to a particular version can be reviewed.
What is a distributed version control system
Distributed Version Control System (DVCS).
In such systems, such as Git, Mercurial, Bazaar, and Darcs, the client doesn’t just take a snapshot of the latest version of the file, but a full mirror of the original repository. That way, the failure of any of the co-operating servers can be later recovered using a local repository image from any of the mirrors. Because each extraction operation is actually a complete backup of the code repository.
Furthermore, many of these systems can specify interactions with several different remote code repositories. This allows you to collaborate with people from different work groups on the same project. You can set up different collaboration processes as needed, such as hierarchical model workflows, which were previously not possible in centralized systems.
Git workflow
These include some simple and common commands, but let’s forget about these four proper nouns.
- -Jenny: Well, I’m working in a Workspace.
- Index/Stage: temporary storage area
- A Repository is a Repository.
- Remote: Remote repository
The workspace
My test folder is a workspace:
The staging area
Git add contains information about the index file added to git (filename, size, timestamp…). , does not save file entities and points to each file entity by ID. You can use git status to check the status of the staging area. The staging area marks what is managed by Git in your current workspace.
When you complete a requirement or feature and need to commit it to a remote repository, the first step is to use Git add to commit it to the staging area for Git management.
Local repository
Holds versions of the submitted object, which is older than the contents of the workspace and staging area.
After the git commit, synchronize the index directory tree to the local repository. In the next step, synchronize the local repository with the remote repository through Git push.
Remote warehouse
The contents of a remote repository may be modified by collaborating local repositories distributed across multiple locations, so it may or may not be synchronized with the local repository, but its contents are the oldest.
summary
- Any object is created and modified in the workspace;
- Any changes are versioned from the time they enter the index section;
- Changes can leave traces in the repository only if they are committed to the local repository.
- Share local changes with collaborators, and you can push them to a remote repository for sharing.
The following diagram illustrates the relationship between the four regions more directly. If some commands are not clear, it doesn’t matter.
Git command
Online to find a map, others organized a map, very all very good, borrowed. The following sections explain some common commands in detail.
HEAD
Before you get to the specific command, understand the HEAD.
This starts with git branches, which 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’s a pointer to the local branch you’re working on. Think of HEAD as an alias for the current branch.
add
The add command is very simple, mainly to achieve the workspace modification content to the staging area, for Git management.
- Git add. Add all files from the current directory to the staging area
- Git add dir Adds specified directories to the staging area, including subdirectories
- Git add file Adds the specified file to the staging area
Git add-t git add-t git add-t git add-u
Git add-u: commits all changes to the git repository, including all the directories in the current git repository. This operation depends on the git version: -1.x: new and modified files are submitted, excluding deleted files. -2.x: as with git add-a, all changes are submittedCopy the code
commit
Git commit involves committing changes in the staging area to a local repository. Every time you use git commit, you generate a 40-bit hash in your local repository. This hash is also called commit-id. Commit-id is very useful during version rollback. It acts as a snapshot that can be returned at any time in the future using a combined command with Git reset.
- Git commit -m message Commit the staging area to the local repository
- Git commit file -m message Commit the file specified in the staging area to the local repository
- Git commit — amend-m message uses a new commit instead of the last one
branch
When it comes to collaboration, it naturally involves branches, and about branches, there are roughly four operations: show branches, switch branches, create branches, and delete branches.
- Git branch lists all local branches
- Git branch -r lists all remote branches
- Git branch -a lists all local and remote branches
- Git branch branch-name Creates a branch but remains in the current branch
- Git checkout -b branch-name Creates a branch and switches to it
- Git branch — Track branch remote-branch Creates a branch and establishes a tracing relationship with the specified remote branch
- Git checkout branch-name switches to the specified branch and updates the workspace
- Git branch -d branch-name Deletes a branch
- Git push origin –delete branch-name Deletes a remote branch
There are a lot of branches, but they’re easy to remember.
merge
Git merge Git merge Git merge Git merge Git merge Git merge Git merge Git merge
The merge command merges different branches. As shown in the figure above, in a real open scenario, we might cut a branch from the master branch, and then develop to complete the requirements, through R3,R4,R5 commit records, and finally to complete the development needs to merge into the master. This uses merge.
- Git Fetch Merge Git Fetch Merge
- Git merge branch Merges a specified branch to the current branch
Generally, conflict occurs after merge, and you need to resolve the conflict manually. This is mainly because two users have modified the same area of the same file.
rebase
Rebase, also known as derivatives, is an alternative to consolidation.
In the initial phase, we are on the new branch, execute git rebase dev, and all new commits on the new branch are repeated on the master branch. Finally, checkout switches back to the new branch. This is the same with a merge; the branches before and after the merge do not change. Git rebase dev is a new branch that wants to stand on the shoulders of dev. Rebase also requires manual conflict resolution.
Rebase vs. Merge
Now we have two branches like this,test and master, 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 at master
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 displayed separately. The rebase operation does not generate a new node, but fuses the two branches into a linear commit.
Git Rebase git rebase git Rebase Git Rebase Git Rebase Git Rebase Git Rebase Git Rebase Git Rebase Git Rebase Git Merge
reset
When you commit your code with Git, you may find that there is an error in the content of the commit.
- Modify the error content and commit again
- Use git reset to undo the commit
The first method is more straightforward, but commits the record multiple times. Personally, I prefer the second method, the wrong commit does not need to be preserved.
The reset command points the current branch to another location and the corresponding change workspace and staging area.
- Git reset — soft commit changes only the commit point, and does not change the contents of the staging area or working directory
- Git reset — Mixed commit Changes the commit point and changes the contents of the staging area
- Git reset — Hard commit The contents of the staging area and workspace are changed to the exact same state as the commit point
- Git reset — Hard HEAD returns the workspace to where it was when it was last committed
revert
Git Revert uses a new commit to undo any changes made to a historical commit.
The difference between revert and reset
11
- Git revert allows you to use a new COMMIT to rollback the previous commit. Git reset deletes the specified commit.
- In the case of rollback, the effect is similar. However, there are differences when continuing to merge older versions before. Because Git Revert “neutralizes” previous commits with a reverse commit, this part of the change does not reappear when merging older branches later, reducing conflicts. However, git reset deletes some commit on a branch, so when merging with the old branch again, these rollback commit should be introduced again, causing many conflicts. You can read this article if you don’t understand.
- Git reset moves the HEAD back, while Git Revert moves the HEAD forward, but the new commit is the opposite of what you want to revert, and canceles what you want to revert.
push
Git push < remote host name > < local branch name > < remote branch name > Refs /for/master: pushes the local master branch to the corresponding master branch on the remote host Origin. Origin is the name of the remote host
- Git push remote branch uploads a locally specified branch to a remote repository
- Git push remote –force Pushes the current branch to the remote repository forcibly, even if there is a conflict
- Git push remote –all Pushes all branches to the remote repository
Other commands
- Git status displays the changed files
- Git log displays the version history of the current branch
- Git diff shows the difference between staging and workspace
- Git diff HEAD displays the difference between the workspace and the latest commit of the current branch
- Git cherry-pick commit Select a commit and merge it into the current branch
Above is about Git some common commands and detailed elaboration, I believe that can have a preliminary understanding of Git.