My git notes

Time:2021-5-11

Please indicate the source of reprinthttps://www.cnblogs.com/sogeisetsu/p/11410219.html

There are many mistakes in the update about version rollback. Please refer toVersion rollback


‘–‘ – marks the end of the command, and you can write the file name freely after it, without causing errors due to the file name and command name

There are two steps to add files to git warehouse

Using commandsgit addNote that it can be used repeatedly to add multiple files;
Using commandsgit commit -m, complete the upload to the library
git commitIt could be uploading logs
Add is the file to the staging area
Commit is file to libraryAdd many times and commit once

Compare workspace with staging area

    git diff No parameters are added to compare the workspace and the temporary storage area by default
Compare the staging area with the latest local version Library (the content of the latest commit in the local library)
    git diff --cached [...]
Compare workspace with latest local version Library
    git diff HEAD [...]If the head points to the master branch, the head can also be changed to master

Compare the differences between different branch files
git diff branch1 branch2 –stat    // A list of all the different files is displayed
Git diff branch1 Branch2 specific file path    // Displays the detailed differences for the specified file
git diff branch1 branch2                    // Show the detailed differences of all files with differences
This is an article by Gu Feilong, a CSDN blogger

View status

git statusIt’s state
git reset --hard HEAD^It’s back to a certain version, back to the first 100 versionsgit resert --hard HEAD~100And so on.
git resert --hard HEAD^It’s back to the previous version, and so on

Git encourages extensive use of branches:

View branches:git branch

To create a branch:git branch

Switch branches:git checkout

Create + switch branches:git checkout -b

Merge a branch to the current branch:git merge

Delete branch:git branch -d

If you want to discard a branch that has not been merged, you can use thegit branch -DForce deletion.

Delete remote branch:

git branch -d -r //After deleting the remote branch, it needs to be pushed to the server,git branch -aThe deleted remote branch will not be displayed, but it can still be seen on GitHub, so it can be recovered at this step
git push origin -d//After deleting, push it to the server. If you execute this command directly, it can be deleted directly and cannot be recovered

Merge branches

git mergeIt’s a merger

When Git is unable to automatically merge branches, the conflict must be resolved first. After the conflict is resolved, submit again, and the merge is completed.

To solve the conflict is to manually edit the failed git merge file to what we want, and then submit it.

usegit log --graphCommand to see the branch merge diagram.

When merging branches, add--no-ffParameters can be merged in normal mode. After merging, there are branches in the history, so it can be seen that merging has been done, while fast forward merging can’t see that merging has been done.

When fixing a bug, we will fix it by creating a new bug branch, then merge it, and finally delete it;

Stash temporary storage

When the work at hand is not finished, put it on the spot firstgit stashThen fix the bug, fix it, and try againgit stash popReturn to the work site;
git stash listCommand to see the current stash list

There are two ways to restore the state before stashgit stash applyRestore, but after restoring, stash content is not deleted, you need to usegit stash dropTo delete;

Another way is to usegit stash popAt the same time, the stash content was deleted:

If you want to merge the bug fixed on the master branch into the current dev branch, you can use thegit cherry-pickCommand to “copy” the changes submitted by the bug to the current branch to avoid duplication of effort.Is the hash value.

Recovery (there are many mistakes about version rollback in this article. Please refer toVersion rollback)

git checkout -- fileIs to restore a file in the workspace and the temporary storage area, butgit checkout -- fileOffileCan not be omitted, that is to say, only one file can be recovered, not all files. To recover all the files, usegit reset --hard HEAD

git reset HEAD fileIt is to restore the files in the temporary storage area to the same state as the library.

There are three scenarios
Scenario 1: when you change the contents of a file in the workspace and want to discard the modification directly, use the command git checkout — file.
Scenario 2: when you not only change the contents of a file in the workspace, but also add it to the temporary storage area, you want to discard the modification in two steps. The first step is to use the command git reset headThen we return to Scene 1. In the second step, we operate according to Scene 1.
Scenario 3: if you want to cancel the submission after you have submitted an inappropriate modification to the version library, refer to the version rollback section, but only if it is not pushed to the remote library.

git reset --hard HEAD^Workspace and temporary storage are used to return to a certain version and the first 100 versionsgit resert --hard HEAD~100And so on.

git resert --hard HEAD^It’s back to the previous version, and so on.

Revoking the amendment

(there are many mistakes in the text about version rollback. Please refer toVersion rollback)
In general, if you modify a file and want to undo the modification, the file has the following states

  • It’s just that a file in the workspace has been modifiedgit checkout -- file
  • The modification of the workspace has already mentioned the temporary storage areagit reset HEADCancel the modification of the staging area, and then use thegit checkout -- fileCancels the modification of the workspace.git checkout -- fileIn fact, it is the same as the temporary storage to recover a file
  • If the library has been mentioned, usegit reset --hard HEAD^To go back to the state of the previous library.

Git reset has an impact, where — mixed is the default state. An index can be understood as a staging area.

在这里插入图片描述
Read this article for a further explanation of GIT resethttps://www.jianshu.com/p/cbd5cd504f14

From GitHub

git remote add oraign urlTo associate remote libraries.

git push -u oraign masterTo push to the remote library

git push oraignTo push other branches

git pull To get branch content

git checkout -b dev origin/devCreate the dev branch of remote origin to local

git pullCapture the latest Submission from origin / dev

Git fetch and pull

It can be summarized as follows
Git fetch is to pull the latest content of the remote host to the local. After checking, the user decides whether to merge it into the working local branch.
Git pull is to pull down the latest content of the remote host and merge it directly, that is: git pull = git fetch + git merge, which may cause conflicts and need to be solved manually.

label

Tags make it easy to contact commit

Command git tagIt is used to create a new tag. The default value is head. You can also specify a commit ID;
Command git tag – A-M “BLA…” can specify label information;
Command git tag to view all tags.
Command git push originA local tag can be pushed;
The command git push origin — tags can push all the local tags that have not been pushed;
Command git tag – DYou can delete a local label;
Command git push origin: refs / Tags/You can delete a remote tag.