Extended application of git (improve work efficiency)

Extended application of git (improve work efficiency)

In git version management, the more commonly used commands are pull and push . However, there are still some important instructions that can provide us with convenience in actual use. The main instructions mentioned in this article are stash , reset --soft , cherry-pick , revert and reflog .

stash

Introduction

​ Use when you want to record the current state of your working directory and index, but want to return a clean working directory git stash. This command will save local changes and restore the working directory to match the head commit. stashThe command can commitsave unfinished code and make your working directory clean.

Application Scenario

​ When you develop new requirements on an independent branch, you need to temporarily modify the code on the main branch of the master (such as changing the bug of an online project). At this time, your function is only half developed and you need to temporarily store the code Instructions are available stash.

​Instructionsstash are different from direct commitcode, and will not submit records in the branch, but only provide a temporary storage function for the code.

Related commands

# 保存当前未commit的代码  
git stash  
  
# 保存当前未commit的代码并添加备注  
git stash save "备注的内容"  
  
# 列出stash的所有记录  
git stash list  
  
# 删除stash的所有记录  
git stash clear  
  
# 应用最近一次的stash  
git stash apply  
  
# 应用最近一次的stash,随后删除该记录  
git stash pop  
  
# 删除最近的一次stash  
git stash drop  

​ When there are multiple records stash, we can specify to operate a certain one stash. First, we need to stash listlist all the records, and then add the command we want to operate directly after the operation command.

# 列出所有记录
$ git stash list  
stash@{0}: WIP on ...  
stash@{1}: WIP on ...  
stash@{2}: On ...

# 应用第二条记录
$ git stash stash apply stash@{1}

# pop, drop 指令同理

reset --soft

Introduction

​ Mainly used for version rollback, only committhe code of the operation is rolled back, and the files in the workspace (locally saved) will not be affected. When rolling back to the specified version, the content modified after this version will be returned to the temporary storage area and will not be deleted.

Application Scenario

  • If you are not careful commitabout the content that should not be submitted, you don’t want to use it committo overwrite and leave a record. At this time, you can use reset --softthe command to trace back the code
  • The code management is standardized. When the submission needs to be distinguished according to the function point, the code of different functions is modified once commit, and the code needs to be rolled back

Related commands

# 恢复最近一次 commit  
git reset --soft HEAD^  

# 恢复上上次 commit  
git reset --soft HEAD^^  

# 恢复上上上次 commit  
git reset --soft HEAD^^^ 

The above command is aimed at the direct rollback to the recent commit version. If there are many rollback versions, you can also directly specify the commit version number for backtracking.

# 先使用 log 命令显示我们的 commit 记录,commit 字符后的便是相应的版本号
$ git log
commit 1669bcf55e1e177284d297bb41d1f7a3411dd333 (HEAD -> ***, origin/***)
Author: ***
Date:   Tue Mar 29 14:42:58 2022 +0800
    commit222 日志

commit a7820456a42cad712c4d01df842a5f83d683333d
Author: ***
Date:   Tue Mar 29 14:37:15 2022 +0800
	commit111 日志
	
# 指定版本号进行代码版本回退到commit111版本,commit222版本的代码也会回到暂存区
$ git reset --soft a7820456a42cad712c4d01df842a5f83d683333d

**Remarks:** The above is not yet pushcompleted commit. For existing pushones commit, you can also use this command, but again push, because there are differences between the remote branch and the local branch, you need to force push git push -fto overwrite the old resetone commit.

cherry-pick

Introduction

Given one or more existing commits, apply the changes introduced by each commit, recording a new commit for each commit. Simply put, it is to copy what has been submitted in branch a commitand commitapply it to branch b.

Application Scenario

  • In order to copy the code in branch a to branch b, for example, in requirements development, some functions do not need to be submitted to the formal environment for the time being, you can copy the specified version in your own branch to the main branch.
  • ​ The code of a certain branch is polluted, and the structure is relatively chaotic. After opening a new branch, copy some useful code from the original branch to the new branch.

Related commands

# a分支
$ git log
commit 1669bcf55e1e177284d297bb41d1f7a3411aa333 (HEAD -> aaa, origin/aaa)
Author: ***
Date:   Tue Mar 29 14:42:58 2022 +0800
    a3

commit a7820456a42cad712c4d01df842a5f83d68aa222
Author: ***
Date:   Tue Mar 29 14:37:15 2022 +0800
	a2
	
commit a7820456a42cad712c4d01df842a5f83d68aa111
Author: ***
Date:   Tue Mar 29 14:37:00 2022 +0800
	a1
# b分支
$ git log
commit 1669bcf55e1e177284d297bb41d1f7a3411bb222 (HEAD -> bbb, origin/bbb)
Author: ***
Date:   Tue Mar 29 14:42:58 2022 +0800
    b2
	
commit a7820456a42cad712c4d01df842a5f83d68bb111
Author: ***
Date:   Tue Mar 29 14:37:00 2022 +0800
	b1

Copy single : copy version a1 to branch b

# 切换到 b分支,执行以下命令
$ git cherry-pick a1版本号

Copy multiple :

# 一次转移多个提交,将a1、a2复制到 b分支
# 切换到 b分支,执行以下命令
$ git cherry-pick a1版本号 a2版本号

# 区间复制,将a1至a3全部复制到 b分支
# 切换到 b分支,执行以下命令
$ git cherry-pick a1版本号^..a3版本号

Remark:

​ When using cherry-pickcode duplication, code conflicts sometimes occur. At this point the conflicting code needs to be resolved before cherry-pick --continuethe replication can continue. If you want to stop copying, you can use git cherry-pick --abortthe command. If you want to return to the state before the operation, you can exit directly cherry-pickand use git cherry-pick --quitthe command

revert

Introduction

Given one or more existing commits, revert the changes introduced by the related commits, and record those changes in new commits. Reverts an existing commit, reverts the commit, and generates a revert record.

​ Different from resetdirectives, revertdirectives will not affect other commits, but will only affect the restored commit and leave a change record.

Application Scenario

​ On a certain branch, there are three submission records a, b, and c in sequence. At this time, if you want to change the relevant code in the submission record of a without affecting the codes submitted by b and c twice, you can directly use the command to operate reverta Record.

Related commands

revertnormal submission

# b分支,现有提交记录
$ git log
commit 1669bcf55e1e177284d297bb41d1f7a3411bb222 (HEAD -> bbb, origin/bbb)
Author: ***
Date:   Tue Mar 29 14:42:58 2022 +0800
    b2
	
commit a7820456a42cad712c4d01df842a5f83d68bb111
Author: ***
Date:   Tue Mar 29 14:37:00 2022 +0800
	b1
	
# 修改b1
$ git revert commit b1版本号

​ After using revertthe command, you will enter the edit page, edit the log information and use wqthe save command. Check the latest log at this time, you can see that a new revertrecord has been generated, the previously submitted record is still retained, but the modified code has been withdrawn.

reflog

Introduction

This command is used to manage the information recorded in the re-recording.

If it reset --softis a regret medicine, it reflogis a powerful regret medicine. It records all commitoperation records, which is convenient for retrieving records after wrong operations.

Application Scenario

When using resetthe command to go back too far, we can use the previous record of reflogthe query , and go back again to restore the previous misoperation.resetcommitreset

Related commands

# a分支
$ git log
commit 1669bcf55e1e177284d297bb41d1f7a3411aa333 (HEAD -> aaa, origin/aaa)
Author: ***
Date:   Tue Mar 29 14:42:58 2022 +0800
    a3

commit a7820456a42cad712c4d01df842a5f83d68aa222
Author: ***
Date:   Tue Mar 29 14:37:15 2022 +0800
	a2
	
commit a7820456a42cad712c4d01df842a5f83d68aa111
Author: ***
Date:   Tue Mar 29 14:37:00 2022 +0800
	a1

The above is the submission status of branch a. Because there is a problem with the a3 code, I want to delete the a3 record directly. I accidentally deleted the wrong record and deleted a2.

# a分支,此时仅剩下a1记录
$ git log	
commit a7820456a42cad712c4d01df842a5f83d68aa111 (HEAD -> aaa, origin/aaa)
Author: ***
Date:   Tue Mar 29 14:37:00 2022 +0800
	a1

At this time, you can use reflogthe query to the previous commitrecords

# 查询之前的 commit 记录
$ git reflog

# 由 reflog 指令能得到a分支的所有commit记录,得到a3的版本号
# 此时再使用reset指令便可覆盖之前的误操作,仅删除a3记录
$ git reset --hard a3版本号

# 使用上述指令后,能查询到a2版本回来了

Summarize

This article only needs to introduce five instructions, and its instructions and main functions are as follows:

  • stash: store temporary code.
  • reset --soft: Soft backtracking, roll back the commit while retaining the modified content.
  • cherry-pick: Copy commit.
  • revert: Undo the modified content of the commit.
  • reflog: Records the historical operations of commit.

Guess you like

Origin blog.csdn.net/xiri_/article/details/123897098