Git command operation [full series]

Git common command operation

1 basic command

①git config --global user.name ['your username']: view/set

git config --global user.name ziyi: set the user name to ziyi
git config --global user.name: view the user name

②git config --global user.email ['your email']: view/set

set email

③git reset HEAD test.txt to cancel the cached content

Cancel the cached content of test.txt

④git rm --cached <file> deletes the file from the temporary storage area

⑤git branch related commands

git branch : view branch
git branch <branchName> : create a branch
git checkout <branchName>: switch branch
git merge <branchName>: Merge the content of branchName into the current branch
git branch -d <branchName>: delete branch

⑥git log related commands

git log -graph: View when branches and merges appear in the history
git log --reverse: reverse output log
git log --author=ziyi --online -5: Concise version View ziyi's latest five commits

⑦git tag related commands

git tag: view all tags
git tag -a -m "XX label": label

⑧git reset --soft <commit>: rollback version

  1. git reset --soft <commit>: Roll back to the specified commit, keep the modified content, and put these modifications in the temporary storage area.
  2. git reset --mixed <commit>: Roll back to the specified commit, keep the modified content, but put these modifications in the working directory.
  3. git reset --hard <commit>: Roll back to the specified commit and delete all modifications, including those in the temporary storage area and working directory.

⑨git merge --abort: abandon this merge

After local conflict resolution is complete

  • git status: view status
  • git commit -m "xxx": commit information
  • git push: push to remote

2 Advanced commands

2.1 Fork someone else's warehouse and keep it updated synchronously

  1. Clone the fork project into the local warehouse
  2. Configuring a remote for a fork
  3. Syncing a fork

For example: I forked a https://github.com/simplezhli/flutter_deer.git (hereinafter called A) to my own warehouse (hereinafter called B)

①git clone B (own warehouse)

git clone 我自己fork的大佬的项目的GitHub地址

②git remote add upstream A warehouse address

# 查看当前仓库的远程状态
git remote -v
# 被本地仓库添加一个将被同步给 fork 远程的上游仓库
git remote add upstream https://github.com/simplezhli/flutter_deer.git【A仓库地址】
# 这里后面的那个地址就是你fork的项目的源地址

There will be two more upstreams below, indicating that the second step has been successful. This is equivalent to having a pipeline established between the source project and your forked project. Let's see how to communicate updates.

③Synchronous fork (pull remote fetch, and then merge to local)

# 从upstream拉取信息
git fetch upstream [分支名]
# 切换到本地主分支
git checkout master
# 把 upstream/master 分支合并到本地 master 上,这样就完成了同步,并且不会丢掉本地修改的内容。
git merge upstream/master
# 这样,就把源项目同步更新到你的本地仓库中了。 如果再想更新到远程仓库fork(B),只需要:
git push origin master

Practical renderings:

insert image description here

2.2 Merge multiple commits into one

Generally speaking, open source communities submit PRs that need to be merged into one commit

  1. For example, I now have 4 commits:
85d5d8fa468b06bb9a62fafde01d80cbb7396682 # 我改的

621ca4121f971d9604e395556763551427d799d9 # 我改的

f744d2e91916ab7831f3a7695d1d1825916db164 # 我改的

5c135e49e683563fa470d7f5c281050ec1d73af9 # 我改的

295ac3b842b4ecb6eff1c9954a281a4606a8bc84 # 别人改的
  1. I now want to merge my commits into one
8403afe13664d6bb7f5a5557716a030e9389a944 # 我改的

295ac3b842b4ecb6eff1c9954a281a4606a8bc84 # 别人改的
  1. Specific operation method

2.2.1 Method 1: Merge commitID

First roll back the content from the version library to the temporary storage area, and then resubmit the content of the work area

  • Idea: Use git reset --soft to roll back the versions of the version library and the temporary storage area, while retaining the changes in the work area, and then resubmit the contents of the work area.
# 查看前10个commit【找到别人最后一次提交的位置】
git log -10
# 从版本库恢复文件到暂存区,不改动工作区的内容
git reset --soft 295ac3b842b4ecb6eff1c9954a281a4606a8bc84	# 别人改的commitID
# add已经跟踪的文件
git add -u
# 提交
git commit -m "修改信息"
# 强制push以替换远程仓的commitID
git push --force

If the push fails and Reject appears, you need to enable the option to force the branch to be merged and cancel the branch protection.

  • Settings -> Repository -> Protected Branches -> Protected branch (find the branch) -> Unprotect

2.2.2 Method 2: git rebase

# 查看前10个commit
git log -10
# 将4个commit压缩成一个commit
git rebase -i HEAD~4	
# add已经跟踪的文件
git add -u
# 提交
git commit -m "修改信息"
# 强制push以替换远程仓的commitID
git push --force

Note: A progress git rebasewill be 临时created 新分支. If you make a mistake, you can git checkout the original branch name to switch back to the original branch and then re-git rebase.

Specific demonstration of git rebase:

I want to merge the first four commits into the last one. As follows:

①git log View historical submission information
insert image description here
②git rebase -i HEAD~n

Use git rebase -i HEAD~5 to compress 5 commits into 1, or git rebase -i 51efaef517abdbf674478de6073c12239d78a56a (the id of the first commit)

Vim editor, press i to edit, change the pick of the last 4 commits to fixup, and keep the first pick. Press the esc key, enter: wq to save and exit.

  • pick: use commit.

  • reword: Use commit to modify the commit information.

  • squash: Use commit to merge the commit information into the previous commit.

  • fixup: use commit, discard commit information.
    insert image description here
    After the operation, it was found that the commits were merged into one.
    insert image description here
    ③git push --force submit
    insert image description here

3 Required commands

3.1 git remote related

git remote add [alias] [url]: Add remote warehouse
git remote rm [alias]: delete remote warehouse

3.2 git fetch related [Pull the latest code without merging]

git fetch test: fetch from the remote warehouse test [if there are multiple remote warehouses, specify the name]

If the local git adds multiple remote warehouses, specify the remote warehouse name

3.3 git pull【Pull the latest code and merge】

git pull = git fetch + git merge

3.4 git push

git push [alias] [branch] : Push to the specified branch of the specified warehouse

Actual combat: Submit pr

Reference: https://www.jianshu.com/p/021bb953ee8d

Guess you like

Origin blog.csdn.net/weixin_45565886/article/details/131560670