git speed pass

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

insert image description here

  • Create warehousegit init [项目名称]

  • View git statusgit status

  • Add files to staging areagit add 文件

  • Workspace files are added directly to the version areagit commit -am '消息'

  • Submit the files in the staging area to the historical versiongit commit -m '变更原因'

  • Rename files managed by gitgit mv oldName newName

  • View historical versiongit log [-n4] [--online] [--all] [--graph] --oneline 信息简短展示 -n4 最近4次提交 --all 所有分支 --graph 图形化展示版本关系

  • how many local branchesgit branch

  • Switch branch, create if it does not exist, you can specify the version to create a branchgit checkout [-b] 分支名称 [版本编号]

  • delete branchgit branch -d 分支名

  • Modify the latest commit messagegit commit --amend

  • Compare the difference between two specified commitsgit diff commitID1 commitID2

  • Compare the difference between the temporary storage area and the Headgit diff --cached

  • Compare the differences between the workspace and the staging areagit diff [-- 文件]

  • The temporary storage area is restored to the same as the headgit reset HEAD [file...]

  • The files in the workspace are restored to be the same as those in the temporary storage areagit checkout -- [file...]

  • Undo the last few commitsgit reset --hard

  • clear staging areagit reset --hard

  • Delete Filesgit rm 文件

  • stash

    • Temporarily hide workspace filesgit stash save '注释'
    • view hidden listgit stash list
    • recovergit stash apply stash@{$num}
    • Remove the specified stashgit stash drop stash@{$num}
    • remove all stashesgit stash clear
  • 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 localgit clone

  • Pull the remote warehousegit pull

  • Push local to remote warehousegit push [alias]

  • Pull remote code, but do not mergegit fetch [alias]

  • merge into current branchgit merge 分支

  • unmergegit 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
insert image description here

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.
insert image description here

branch

Create a new branch named newImage.
insert image description here

git branch newImage

insert image description here

The new branch does some operations

git commit

insert image description here

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.
insert image description here

The current branch is on main

git merge bugFix

insert image description here

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

insert image description here

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

insert image description here

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;

insert image description here

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.
insert image description here

git reset HEAD~1

insert image description here

revert Cancel modification and share this state with others

insert image description here

git revert HEAD

insert image description here

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).
insert image description here

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

insert image description here

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

insert image description here

变基当前分支
git rebase -i HEAD~4

insert image description here

git copied them according to the selected commit.

Use git commands flexibly

Modify the message before multiple commits

Method 1: git rebase -i
insert image description here

  • 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
    insert image description here

Method 2: cherry-pick
insert image description here

git checkout main;

git cherry-pick C2;

git commit --amend;

git cherry-pick C3

insert image description here

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.
insert image description here

Create a tag pointing to commit C1, indicating that this is our first version.

git tag v1 c1

insert image description here

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.
insert image description here

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
insert image description here

git describe main will output: v1_2_gC2

git describe side will output: v2_1_gC4

Guess you like

Origin blog.csdn.net/weixin_46488959/article/details/131148234