Learn git online:https://learngitbranching.js.org/?locale=zh_CN
git configuration
- minimum configuration
git config --global user.name '用户'
git config --global user.email '用户email'
git config --local //local 只对指定仓库有效
git config --global //global 对当前用用户所有的仓库有效
git config --system //system 对系统所有登录的用户有效
Display the configuration of config, add --list
git config --list --local
git config --list --global
git config --list --system
–local To take effect in a warehouse, local priority is higher than global
Order
-
Create warehouse
git init [项目名称]
-
View git status
git status
-
Add files to staging area
git add 文件
-
Workspace files are added directly to the version area
git commit -am '消息'
-
Submit the files in the staging area to the historical version
git commit -m '变更原因'
-
Rename files managed by git
git mv oldName newName
-
View historical version
git log [-n4] [--online] [--all] [--graph]
--oneline 信息简短展示
-n4 最近4次提交
--all 所有分支
--graph 图形化展示版本关系
-
how many local branches
git branch
-
Switch branch, create if it does not exist, you can specify the version to create a branch
git checkout [-b] 分支名称 [版本编号]
-
delete branch
git branch -d 分支名
-
Modify the latest commit message
git commit --amend
-
Compare the difference between two specified commits
git diff commitID1 commitID2
-
Compare the difference between the temporary storage area and the Head
git diff --cached
-
Compare the differences between the workspace and the staging area
git diff [-- 文件]
-
The temporary storage area is restored to the same as the head
git reset HEAD [file...]
-
The files in the workspace are restored to be the same as those in the temporary storage area
git checkout -- [file...]
-
Undo the last few commits
git reset --hard
-
clear staging area
git reset --hard
-
Delete Files
git rm 文件
-
stash
- Temporarily hide workspace files
git stash save '注释'
- view hidden list
git stash list
- recover
git stash apply stash@{$num}
- Remove the specified stash
git stash drop stash@{$num}
- remove all stashes
git stash clear
- Temporarily hide workspace files
-
rebase
git rebase -i commitID
-i interactive rebase- Modify the commit message r
- Merge multiple consecutive commits
- Merge multiple discontinuous commits and put the discontinuous ones together, s
-
Ignore unneeded files.gitignore
- / folder, * wildcard
-
Backup warehouse to local
git clone
-
Pull the remote warehouse
git pull
-
Push local to remote warehouse
git push [alias]
-
Pull remote code, but do not merge
git fetch [alias]
-
merge into current branch
git merge 分支
-
unmerge
git merge --abort
-
Prohibit the push -f operation to the integration branch [-f force update]
-
Disable change history rebase operations to integration branches
common operations
Basic
commit
Save the
commit record before the branch status
git commit
Modify the file and express these modifications as a commit. The newly generated c2 has a parent commit C1, which means that the modification came from there.
branch
Create a new branch named newImage.
git branch newImage
The new branch does some operations
git commit
The main branch moved forward, but the newImage branch did not! This is because we are not "on" this new branch, which is why the asterisk (*) is on main.
merge
merge produces a special commit that contains two unique parent commits. If a commit has two parent commits, it means: "I want to include these two parent commits and all their parent commits.
The current branch is on main
git merge bugFix
main now points to a commit that has two parent commits. If you start from main and go up the arrow, you will pass through all the commits on the way to the starting point. This means that main now records all changes to the file.
rebase The current branch is rebased to the specified branch
Want to move the changes made in the bugfix directly to the main branch. With rebase, the two branches appear to be modified sequentially, but in fact their modifications are performed in parallel.
git rebase main
Work on the bugFix branch is now at the forefront of the main branch, and we've got a more linear order of commits.
Note that the original commit C3 does not disappear (shaded on the graph), but we "copy" C3 and connect its copy C3' behind the main branch.
The only problem now is that the main branch hasn't been updated yet; switch to the main branch. Then rebase it to bugFix
git checkout main; gti rebase bugFix;
Because main branch is the parent of bugFix, git just moves main branch forward to bugFix.
Advanced
detach HEAD
HEAD is a reference, it points to the current checkout commit, which is basically the commit you are currently in;
in the commit tree, HEAD always points to the latest commit, most commands will modify the status of the commit tree First change the commit pointed to by HEAD.
HEAD usually points to the name of a branch. When you commit, it is time to check the status of the branch. This change can be seen from the change of HEAD.
To separate HEAD is to make it point to a commit instead of a branch name, git checkout hash; move HEAD to a commit
relative reference^
If you want to move in git, it will become more troublesome by specifying the hash value of the commit. You have to use git log to look up the hash value.
Using relative references, you can start working from an easy-to-remember place (such as the branch name bugFix or HEAD).
- Use ^ to move up a commit
- Use ~ to move multiple commits up
Insert (^) this one symbol. Putting this symbol after a reference means that you tell git to find the parent commit of the commit pointed to by the reference.
main^ is equivalent to "main's parent commit".
main^^ is the grandparent commit of main (two generations forward)
You can also specify HEAD as a relative reference
git branch -f 分支 引用
Let the branch point to another commit
cancel modification
reset has no effect on remote branches
git reset returns the reference point of the branch to the previous commit to cancel the modification. You can think of this as "rewriting history". git reset moves the branch back, and the commit pointed to by the original branch seems to have never existed.
git reset HEAD~1
revert Cancel modification and share this state with others
git revert HEAD
There is actually a new commit behind the commit to be canceled! This is because the new commit C2' introduces a modification - it is used to indicate that we cancel the modification of the commit C2. Now you can share your edits with others.
Adjust the order of commits
Super easy to use cherry-pick
git cherry-pick <Commit1> <Commit2> <...>
This is a very straightforward way to do it when you want to copy a few commits under your current location (HEAD).
In the side branch we have some commits that we want to copy to the main branch, this can be done with a rebase; see how git cherry-pick does it.
git cherry-pick c2 c4
We copied C2 and C4 and put them after main
rebase -i interactive rebase
When the interactive rebase dialog window is open, you can do three things:
- You can reorder the commits by changing their position in the UI.
- You can choose to ignore certain commits completely
- Commits can be merged together
变基当前分支
git rebase -i HEAD~4
git copied them according to the selected commit.
Use git commands flexibly
Modify the message before multiple commits
Method 1: git rebase -i
- First use git rebase -i to reorder the commits, and then move the commits we want to modify to the front
- Then use git commit --amend to make some changes
- Then use git rebase -i to rearrange them in the original order
Method 2: cherry-pick
git checkout main;
git cherry-pick C2;
git commit --amend;
git cherry-pick C3
tag
Branch is easy to be moved, and when there is a new commit, it will be moved again. Branch often points to a different commit. Branch is easy to change.
Is there any way to always have a mark pointing to the commit, for example, to indicate a major software release, or to fix a big bug, is there any other better method than branch, which can always point to these commits?
Git tags can solve this problem, they can always point to a specific commit, just like representing a "milestone". More importantly, when there are new commits, they will not move. You cannot "checkout" to commit on the tag. The existence of the tag is like an anchor representing a specific message on the commit tree.
Create a tag pointing to commit C1, indicating that this is our first version.
git tag v1 c1
git describe
Because tag represents an anchor point on the commit tree, git has a command that can be used to display the anchor point closest to you (that is, tag), and this command is called git describe
git describe <ref>
It is any location that can be interpreted as a commit by git. If you do not specify it, git will take your current location as the standard (HEAD)
The output of the command is like this:
__g
means the nearest tag, numCommits means how many commits there are from this tag, and means the first seven ids of the commit you have given.
git describe main will output: v1_2_gC2
git describe side will output: v2_1_gC4
ag>__g
means the nearest tag, numCommits means how many commits there are from this tag, and means the first seven ids of the commit you have given
git describe main will output: v1_2_gC2
git describe side will output: v2_1_gC4