This is the 13th day of my participation in the First Challenge 2022
The last article covered git installation, commit, and synchronization. For more details, see the previous article, “How to Use GIT with Your Hands”.
Today we’ll take a look at other ways to use Git, such as viewing commit history, rolling back versions, comparing three regions, and using git branches that are common at work
View git commit records
Git log or git log –oneline
The difference between the two lines above:
git log
Is to display detailed Git recordsgit log --oneline
Is to display each record in a single line
For example, I committed to the repository twice, so let’s see what the two statements look like. Git log
git log --oneline
Note: The long string of garbled characters we see is the ID of every commit record git generates
Compare the differences between regions
Git is divided into three areas: workspace, cache, and repository. The workflow is described in detail above
Compare the difference between workspace and cache
Example: There is no code for 02.html in the project. Now I write code to 02.html.
change
rungit diff
Command to view the difference between workspace and cache
Run Git diff –chached to see the difference between caches and repositories
After committing the records from the above workspace to the cache, execute the Git diff –cached command
Rollback an earlier version
Go back to workspace content Git Checkout
Git checkout is equivalent to CTRL + Z, but a little more advanced than CTRL + Z.
HTML modified
rungit checkout
after
Git reset rollback cache contents directive
Rollback the warehouse to the specified history
Git reset --hard History ID
The history record ID, git log, is shown in the following figure
Git branch
View branches (Before creating branches, let’s view all branches of the current repository)
git branch
There is only one branch, and the branch name is master
Create a branch
Creating a branch is like making a copy of the project folder
The command
Git branch Specifies the branch name
A branch named A is created (this is equivalent to renaming the copied project file to A)
Switch branches (after creating divide-and-conquer we need to switch to the created branch)
Git Checkout branch name
This is like opening the copied project folder with a new vscode window, and everything that follows is done in this new vscode, so that everything I change in this new vscode doesn’t affect my original project folder, which is the main reason why we use branches, Avoid handling files on the main branch of the repository as much as possible.
Merging branches
Git merge branch name Example:
- Switch to the branch to be merged (the target branch of the merge) the main branch
- run
Git merge subbranch name
This step is equivalent to merging the copied project folder with the source folder
Delete the branch
Git branch -d git branch -d git branch -d
Git branch Branch name -d
We can only delete branches that have already been merged, which is equivalent to deleting files and we don’t have administrator rightsGit branch Branch name -d
Can branches be deleted under any circumstances (forced deletion) equivalent to deleting files with administrator privileges
Example: Delete the newly created branch A step:
- Switch to another branch in the main branch
- run
Git branch Branch name -d
Reason for removing branches: This step is usually performed because the code in this version is not OK
Extension:
Modify the synchronization remote warehouse address and application scenario
1. Change the name of the online warehouse. 2
Git remote set-url origin git remote set-url origin
Download online warehouse
Git clone the address of the remote repository
Synchronize offline warehouse with online warehouse
Run Git pull locally
After the
That’s all I have to share with you about Git