Git general instruction

Time:2020-11-6

Git basic research and summary

The previous words: about the construction of GIT environment, there have been a lot of articles on it. This paper only summarizes some of my own learning experience of GIT, as well as the description of basic git instructions, so as to help you understand git foundation faster and adapt to git needs in daily work faster.

Now, assuming the environment is set up, create a new repository on GitHub and clone it locally.

Git basic process:

Now, let’s try a set of basic git commands
1. Store the GIT address on git clone GitHub in the local folder of the warehouse, and clone the remote branch to the local;

2. Touch. Gitignore file (can be used to ignore the file type of non GIT);

3. Create the first file, GIT add firstgitfile.txt Add the file to the temporary storage area;
Git general instruction
4. Git commit to submit, input I to modify in the pop-up panel, and then ESC ZZ exits;
Git general instruction
5. Git log view log information and push git to the remote end;
Git general instruction
So far, GIT basic operations have been completed. These operations are easy to understand, but in the daily development, only these knowledge is far from enough. We will introduce some commonly used git instructions and principles in detail.

Git instruction introduction

Branch related instruction

Instruction: git checkout – B branch name
Introduction: create and cut out to the created branch
Instruction: git branch < branchname > command
Description: creates a new branch, but does not cut out to it
Instruction: git checkout branch name
Introduction: switch to the specified branch. The specified branch must already exist, otherwise an error will be reported
Instruction: git branch – D branch name
Introduction: delete the specified branch
Instruction: git branch
Description: view all local branches
Instruction: git branch – R
Description: view all remote branches
Instruction: git branch – A
Description: view all branches locally and remotely
Instruction: git push origin — delete [branchname]
Description: delete the remote branch and push it to the server after deletion
Instruction: git branch – M < oldbranch > < newbranch >
Description: renames the local branch

Upload local submit:

Instruction: git push
Introduction: submit the commit pointed by the current branch to the remote end, and at the same time, submit a series of commit on the branch to the remote end (only for the branch that has been committed to the remote end)
Instruction: git push warehouse name branch name
Introduction: submit the branch to the specified branch of the specified warehouse, which can be directly submitted to the remote end (origin is the default name of the current warehouse)

Merge other branches:

Instruction: git merge branch name
Introduction: starting from the branch bifurcation position, merge all the commits of the target branch into the current branch to form a commit to the current branch

Pull remote submission content:

Instruction: git pull
Introduction: pull all the commits from the remote branch. The implementation principle is equivalent to fetching the remote commit to the local, and then merge these commit to the current branch

Add command:

Instruction: git add file name
Introduction: add files to the temporary storage area
Instruction: git add
Introduction: save new changes and create new ones to the staging area
Instruction: git add – U
Introduction: save new changes or delete them to the staging area
Instruction: git add – A
Introduction: save all changes to the staging area
Note that the file changes are added here, not the file name

View log:

Instruction: git log
Introduction: view all submission records
Instruction: git log – P
Introduction: View submission record details
Instruction: git log – oneline – n
Introduction: display the latest log submitted by yourself

Variable base:

Instruction: git rebase branch name
Introduction: set the benchmark of the submitted record to the specified branch
Details: the commit of merge often forms a fork in the log tree, which may be a bit confusing. Therefore, we can use rebase to form a single chain structure, which makes the log record clearer. Its principle is to take the last commit of the specified branch as the base position of our branch, and then make our branch the same as the fork position of the specified branch Re submit from the base location

Amendment submission:

Instruction: git commit — amend
Directive: revise the content just submitted
The above instruction can only modify the latest submission. If you want to modify the latest submission, you can use the following instruction.
Instruction: git rebase – I submit location [^]
Introduction: interactive rebase correction, where ^ represents the previous submission of the submitted position, ^ ^ represents the first two analogies, or ~ num represents the first num to start interactive base changing
Note: for the submission records that need to be adjusted during the rebase process, the corresponding pick should be changed to edit. After exiting, rebase will start, and then use commit – modify to adjust the record information, and rebase – continue to rebase until it is completed
Git general instruction

Withdrawal of submission:

Instruction: git reset – – hard head^
Introduction: undo the last commit and empty the workspace and staging area
Instruction: git rebase – I
Introduction: rebase – I can also be undone and the corresponding pick can be deleted
Instruction: git rebase – – onto branch submission start and end point
Introduction: submit the submission record rebase from the starting point to the end point of the current branch to the target branch
Note: the starting position does not include the starting point
Instruction: git revert submit record
Introduction: to withdraw the submission of a specified submission record, the principle is that git performs an operation opposite to the specified submission probability

Push submit record to remote end:

Instruction: git push
Introduction: push submission records to the remote end
Instruction: git push origin branch – F
Introduction: forced push. If the branch has been pushed to the far end and the commit commit record has been changed locally, you can use this command to forcibly commit to the remote branch without affecting others.
Note: this method can’t be used for the master, it is limited to its own branch, and it should be used with great care because it will cover the submitted records of others

Reset command details:

The essence of the reset command is to reset the head and branch positions
Instruction: git reset – – hard submit record
Introduction: reset the head to the specified location. At this time, the submission records of the staging area and workspace will be directly deleted
Instruction: git reset submit record
Introduction: resetting the head to the specified location will put the contents of the staging area and the workspace into the workspace
Instruction: git reset – soft submit record
Introduction: resetting the head to the specified location will add the changes caused by the reset head position to the temporary storage area

Check out command details:

Instruction: git checkout branch | commit | file name
Introduction: the essence of the checkout command is to check out the specified commit, which can check out branches, submit records and even files
Instruction: git checkout – detach
Introduction: head and branch pointer separation, rarely used

Local cache

Instruction: git stash
Introduction: cache uncommitted files
Instruction: git stash pop
Introduction: recover cache files
Instruction: git stash – U
Introduction: cache files including untrack

Reflog instruction

Instruction: git reflog branch
Introduction: it is useful to view all operation records of all branches. Sometimes it can be used to retrieve the deleted branches

Clean instruction

git clean -[dfxn]
Introduction: delete the untrack file under git directory
Parameter introduction:
D — delete files not added to git’s path
F —– forced operation
X —– only delete the marked files in. Gitignore. Those files that are not controlled by git version and are not in. Gitignore will be preserved.
-N displays the files and directories to be deleted

Diff instruction

Instruction: git diff
Description: compares changes to files in the current workspace
Instruction: git diff – stage or git diff – cache
Description: compares the differences between the files in the current staging and the last commit
Instruction: git diff – stat
Description: View simple diff results
git diff sha1 sha2 [path]
Description: compare the differences between the two submissions, and specify the file path for comparison

Restore instruction

Instruction: git restore < File >
Description: undo the changes of the specified file. It is only valid for the files in the version library. The changes in the workspace are undone by default
–Staged to restore the specified file in the staging area to the workspace
Instruction: git restore. Do the same for all files

Fetch instruction

Introduction: pull the branch of the original break and do not merge. The difference between pull and pull is that git pull = git fetch + git merge
Instruction: git fetch < remote host name >
Description: retrieve all updates from a remote host to the local
Instruction: git fetch < remote host name > < branch name >
Description: retrieve updates only for specific branches

Reverse instruction

Instruction: git revert < commit > command
Introduction: revoking the designated submission is essentially a one-time submission of the specified content
Instruction: git revert < commit1 >.. < commit2 > command
Introduction: cancels the commit within the specified commit1 to commit2 interval, excluding commit
Instruction: git revert — no commit commit1
Introduction: after the revert session, the default commit will be – no – commit will cancel this commit, and the changes will only be put in the temporary storage area

Remote instruction

Instruction: git branch
Description: view all local branches
Instruction: git remote
Description: lists existing remote branches
Instruction: git remote – V | — verb
Description: lists the details and lists the remote URL after each name
Instruction: git remote Add URL
Description: add a remote warehouse

RM instruction

Instruction: git RM
Description: delete the workspace file and place the deletion in the staging area
Instruction: git RM – F
Description: forces the deletion of a workspace file and places the deletion in the staging area
Instruction: git RM — cached
Description: removes a file from the index. But the local file still exists, just don’t want it to be version controlled.

cherry-pick

Instruction: git cherry pick < commit > command
Introduction: submit the specified submission record to the current branch
Instruction: git cherry pick < commit1 > < commit2 >
Introduction: submit the specified multiple submission records to the current branch, and each submission record is separated by a space

Grep instruction

It is very convenient to retrieve the text content in the file by git grep command
Grep is powerful and has many parameters. You can get a detailed introduction through git grep – help

epilogue

After that, you can pay more attention to git if you want to know more about GIT. Welcome to learn more about GIT!

Recommended Today

DK7 switch’s support for string

Before JDK7, switch can only support byte, short, char, int or their corresponding encapsulation classes and enum types. After JDK7, switch supports string type. In the switch statement, the value of the expression cannot be null, otherwise NullPointerException will be thrown at runtime. Null cannot be used in the case clause, otherwise compilation errors will […]