After reading this article, the use level of git can reach the junior and intermediate level, and you can compete with the big guys
Remember a few basic concepts first:
warehouse:
local warehouse
local branch, workspace, temporary storage area remote
warehouse
remote branch
Mastering this command first will help you understand git design ideas
git status
Check the status of the file. If the file is displayed in red, it means that the modification of the workspace has not been saved to the temporary storage area. If it is green, it means that it has been saved to the temporary storage area but has not yet been put into the warehouse. If there is no prompt, it means that the workspace, staging area and warehouse code are consistent.
Next, we will explain the basic commands.
The basic commands of git include
git add
to add local modifications to the temporary storage area. There are two steps to save the local modifications to the local warehouse. After executing add, execute commit. After adding, the status of the file turns green, indicating that it has entered the temporary storage area and is waiting to be submitted
git commit
submits the temporary storage area to the local warehouse, that is, submits the green state submission to the warehouse. After storage, the work area is consistent with the code in the temporary storage area and the warehouse, and git status does not see the prompt. Each submission will generate a submission record, generally represented by a hash value
git push
pushes the commit of the local warehouse into the remote warehouse. If the remote warehouse is also modified, the code needs to be updated before submission. If the address of the remote warehouse is different from the address of the remote warehouse mapped to the local warehouse, it cannot be pushed and updated synchronously. of.
git pull
pulls the latest submitted code from the remote warehouse and merges it with the local warehouse code. If the local warehouse has uncommitted code, you need to submit the local warehouse code first. If there is a conflict, the conflict needs to be resolved
git clone
copies code from a remote warehouse
git fetch
pulls the latest submission from the remote warehouse to the local warehouse, the difference from pull is that it does not do merge processing
Advanced commands
These commands often have complex functions, and different parameters can produce unexpected effects, which often make you dizzy.
The original meaning of git reset
reset is to roll back and restore the code. It pulls a submission from the local warehouse, overwrites the content of the work area and the temporary storage area, and the subsequent status is red. As for how much content is rolled back, it depends on the parameters. The commonly used one is
git
reset file HEAD, (use it casually, it will restore the latest submission to the workspace, and lose the state of the temporary storage area)
git reset --soft HEAD^ (use it casually, it will restore the latest submission to the state before submission)
git reset - -hard HEAD^(use with caution, you will lose code)
git diff
to view the difference between the workspace and the temporary storage area
git diff --cached
to view the difference between the temporary storage area and the local warehouse
git diff HEAD
to view the difference between the workspace and the local warehouse
git diff SHA1 SHA2 to view the difference between the two submissions (SHA1 SHA2 is The hash value generated by each commit)
There are many sayings about git rebase
on the Internet, and many of them are translated as rebase, which is actually not easy to understand. To put it bluntly, it means appending one submission to another. Often used to merge code across branches.
A simple analogy is like taking a team of classmates to line up to buy things, only to find out that there are multiple lines when they arrive.
As the captain, you have the right to change which team the entire team is in. If you find that the other team is faster in the middle, if you decide to change teams, the people behind will follow you and change teams.
When rebasing, you are the captain of the current branch, and the team members are the submission queue formed by each submission. Every time you do a rebase, you append all the commits to the other branch.
For advanced usage, git rebase -i HEAD~N
can open the interactive interface and merge multiple submissions into one. Similarly, one time can also be split into multiple times.
When rebasing, you may encounter a conflict. After you need to resolve it, continue to rebase
git rebase --continue
continue to rebase
git rebase --abort
discard this rebase
git rebase --skip
skip this rebase
Git revert
is understood as resubmitting a certain submission. It doesn't mean to roll back, undo, roll back, or discard. It is more appropriate to translate it as recovery.
git merge
branch merging, multiple warehouse merging, etc.
git checkout
restores the contents of the temporary storage area to the work area, can restore files, and roll back submissions, and the local warehouse and temporary storage area will not be affected.
git checkout .
Revert changes to the workspace
git checkout file
restores the modification of a file
git checkou SHA1 -b new_branch
restores a commit and switches to a new branch
git checkou -b new_branch switches to the new branch directly under the current latest commit
Before switching branches, git stash
can be used to temporarily store changes in the temporary storage area. Generally, commit can be used instead, but commit will generate a commit record, stash will not
git log
view commit log
git blame
Check who wrote a piece of shit-like code
git branch
browse branch
git branch -a
view branch of remote warehouse
git remote
can be used to purify remote branches, operate remote warehouses, view remote warehouse addresses, etc.
git remote prune origin
git submodule
submodule operations, such as looping to pull submodule content
git submodule --update recursive
git gc
compressed warehouse and commit records
The magic trick of combining commands and advanced commands