Make full use of Git to cancel and improve project quality (recruitment at the end of the article)

Posted by kkessler on Sat, 13 Jun 2020 11:39:17 +0200

0x001 general

An excellent front-end engineer must be an excellent engineer first; an excellent engineer must be responsible for every line of code he writes and every commit he submits. That said, I don't want to write a very large and comprehensive article, but I want to write some small things.

0x002 undo git add

We know that to submit a commit, you must add first. What should you do if you add something you don't want to add? Here are a few scenarios and solutions:

Scenario 1: add a mistakenly created file, which is not desired

  • Environment: mistakenly created a new.txt , and use add
$ echo new > new.txt
$ git add .
$ git status

On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   new.txt # 👈
  • Solution: just delete it directly
$ rm new.txt
$ git add .
$ git status

On branch master
nothing to commit, working tree clean # 👈

Scenario 2: create a new component, but do not want to submit it together in this commit

  • Environment: a Product component has been created, but it is not currently used. This modification should only contain one index.js
$ git status

 On branch master
 Changes to be committed:
     (use "git restore --staged <file>..." to unstage)
    new file:   Product/index.css # 👈  surplus
    new file:   Product/index.js # 👈  surplus
    new file:   index.js
  • Solution 1: remove using git rm, - r means folder recursion, - cache means remove from index, working tree still keeps
$ git rm -r --cache Product/
rm 'Product/index.css'
rm 'Product/index.js'

$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   index.js  # 👈  index has only one file left

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    Product/ # 👈 But working tree still has
$ git commit -m 'feat: index'
\[master 3bf1398\] feat: index
 1 file changed, 1 insertion(+)
 create mode 100644 index.js
  • Solution 2: use git stash to stage and submit the Product components first index.js , then release the Product
  1. Temporary Product
$ git stash -- Product
Saved working directory and index state WIP on master: b426e7e feat: initial
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   index.js  # 👈  There's only one file left
  1. Submit index.js
$ git commit -m 'feat: index'
\[master 3bf1398\] feat: index
 1 file changed, 1 insertion(+)
 create mode 100644 index.js
  1. Remove the Product from the stash
$ git stash pop
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   Product/index.css  # 👈  It's out again
    new file:   Product/index.js  # 👈  It's out again

Dropped refs/stash@{0} (28675d4accc329da3e54b0a839967471f21c7102)
  1. Submit as independent commit
$ git commit -m "feat: product"
\[master de42d60\] feat: product
 2 files changed, 2 insertions(+)
 create mode 100644 Product/index.css
 create mode 100644 Product/index.js

0x003 undo unwanted changes

Scenario 1: modify a file, but do not want some or all of them

  • Environmental Science
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
  modified:   Product/index.js  # 👈  No
    modified:   index.js  # 👈  No
  • Solution 1: git stash, and then it doesn't need to worry about it. It is applicable to all discards or parts
$ git stash -- Product/index.js
Saved working directory and index state WIP on master: de42d60 feat: product
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   index.js   # 👈  This is the only one left
no changes added to commit (use "git add" and/or "git commit -a")
  • Solution 2: git reset, which is applicable to all current modifications
$ git reset --hard HEAD
HEAD is now at de42d60 feat: product

$ git status
On branch master
nothing to commit, working tree clean   # 👈  It's all gone
  • Solution 3:git checkout, suitable for partial discard
$ git checkout  -- Product/index.js
$ git status
 On branch master
 Changes not staged for commit:
   (use "git add <file>..." to update what will be committed)
   (use "git restore <file>..." to discard changes in working directory)
    modified:   index.js   # 👈  This is the only one left
 
 no changes added to commit (use "git add" and/or "git commit -a")

0x004 undo / modify git commit

Scenario 1: commit has been committed, but commit message has errors

  • Scene display
$ git add .
$ git commit -m 'feat: FALSE message'
\[master ed31f58\] feat: FALSE message # 👈 
 2 files changed, 2 insertions(+), 2 deletions(-)
  • Solution: git commit --amend
$ git commit --amend -m 'feat: correct message'
\[master 325e290\] feat: correct message # 👈 
 Date: Tue May 26 12:39:07 2020 +0800
 2 files changed, 2 insertions(+), 2 deletions(-)

Scenario 2: commit has been submitted, but some files are missing

Scene display:

  1. View the status. Currently, two files have been modified
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   Product/index.js  # 👈  modify
    modified:   index.js  # 👈  modify
  1. Submit only one file
$ git add Product/index.js  # 👈  Submit a
$ git commit -m 'feat: Complete product functions'
\[master 8f3f605\] feat: Complete product functions
 1 file changed, 1 insertion(+), 1 deletion(-)
  1. Check the status. There is another file that hasn't been submitted
$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   index.js  # 👈  One remaining

Solution 1: git commit --amend

$ git add .
$ git commit --amend -m 'feat: Complete product functions'   # 👈  Submit
\[master 1ae5752\] feat: Complete product functions
 Date: Tue May 26 12:49:00 2020 +0800
 2 files changed, 2 insertions(+), 2 deletions(-)
 
$ git status
On branch master
nothing to commit, working tree clean  # 👈  All submitted

Solution 2: git reset --soft HEAD^

$ git reset head^
Unstaged changes after reset:
M   Product/index.js  # 👈  The last submission is back

$ git add .
$ git commit -m 'feat: Complete product functions'  # 👈  Resubmit
\[master 1ae5752\] feat: Complete product functions
 Date: Tue May 26 12:49:00 2020 +0800
 2 files changed, 2 insertions(+), 2 deletions(-)

Scenario 3: commit has been submitted, but there are more files

  • Scene display:
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    new file:   Product/index.css
    new file:   Product/index.js
    modified:   index.js

# Originally, I only wanted to submit Product related documents, but I mistakenly index.js  Submitted
$ git add .
$ git commit -m 'feat: complete Product assembly'
\[master 6c0f3c8\] feat: complete Product assembly
 3 files changed, 3 insertions(+), 1 deletion(-)
 create mode 100644 Product/index.css
 create mode 100644 Product/index.js

Solution 1: resubmit after git reset head ^

$ git reset head^
Unstaged changes after reset:
M   index.js

$ git status
On branch master
Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git restore <file>..." to discard changes in working directory)
    modified:   index.js  # 👈  The submission is back

Untracked files:
  (use "git add <file>..." to include in what will be committed)
    Product/  # 👈  The submission is back

no changes added to commit (use "git add" and/or "git commit -a")

$ git add Product/\*   # 👈  Submit Product only
$ git commit -m 'feat: complete Product assembly'
\[master 9b9d085\] feat: complete Product assembly
 2 files changed, 2 insertions(+)
 create mode 100644 Product/index.css
 create mode 100644 Product/index.js

0x005 delete a commit (latest one)

Sometimes we don't want a commit. Here are several solutions

  • Scene display:
$ git status
On branch master
Changes to be committed:
  (use "git restore --staged <file>..." to unstage)
    modified:   index.js

$ git commit -m 'feat: Unwanted submissions'
\[master aa47ff2\] feat: Unwanted submissions
 1 file changed, 1 insertion(+)
 
$ git log --pretty=oneline
aa47ff2541fa64e6d44735e90dedcee60d5fb0a3 (HEAD -> master) feat: Unwanted submissions   # 👈  This submission doesn't want to
0dc03f1970ed670b97bccffc92ab5598b5049bf8 feat: Complete the home page
9b9d085b932043a2d9250b864a1cbb1535f82184 feat: complete Product assembly
...
  • Solution 1: reset
$ git reset --hard HEAD^
HEAD is now at 0dc03f1 feat: Complete the home page

$ git log --pretty=oneline
0dc03f1970ed670b97bccffc92ab5598b5049bf8 (HEAD -> master) feat: Complete the home page # 👈  be without
9b9d085b932043a2d9250b864a1cbb1535f82184 feat: complete Product assembly
  • Execute the following command
$ git rebase 7064a1652cda00d4a57d24d3661b018771a49387 # Two down
  • vim will be entered
pick 5acd6e2 feat(project): add to version script

# Rebase 7064a16..5acd6e2 onto 7064a16 (1 command)
  • Change to the following and save
drop 5acd6e2 feat(project): add version script
  • View results
Successfully rebased and updated refs/heads/feat/A.

$  git log --pretty=oneline
7064a1652cda00d4a57d24d3661b018771a49387 (HEAD -> feat/A) feat(changeLost): add to changeLog  # The first one is gone
c45cbb73e79d460473dc941ab062ff7e11d099fa (tag: v1.0.3) 1.0.3
671757d69b98e3d6cae6e4dce5717fac4f77a3a7 feat(project): add to ChangeLog
fd369e89072279a7057e236ba1707f2a19709b03 (tag: v1.0.2) 1.0.2
4e9f32cc30525a70c07f3c544594f105c48d8d0f (tag: v1.0.1) 1.0.1
  • Solution 2: rebase
$ git log --pretty=oneline
5acd6e27a85b7e728454374b9fe516387e2e7450 (HEAD -> feat/A) feat(project): add to version script
7064a1652cda00d4a57d24d3661b018771a49387 feat(changeLost): add to changeLog
c45cbb73e79d460473dc941ab062ff7e11d099fa (tag: v1.0.3) 1.0.3
671757d69b98e3d6cae6e4dce5717fac4f77a3a7 feat(project): add to ChangeLog
fd369e89072279a7057e236ba1707f2a19709b03 (tag: v1.0.2) 1.0.2
4e9f32cc30525a70c07f3c544594f105c48d8d0f (tag: v1.0.1) 1.0.1
  • Solution 3: revert
$ git revert aa47ff2541fa64e6d44735e90dedcee60d5fb0a3 --no-edit
\[master ae7eae8\] Revert "feat: Unwanted submissions"
 Date: Tue May 26 13:45:07 2020 +0800
 1 file changed, 1 deletion(-)

$ git log --pretty=oneline
ae7eae86691a90e81b68b85e0f3cb7ebaee4312c (HEAD -> master) Revert "feat: Unwanted submissions"   # 👈  be without
aa47ff2541fa64e6d44735e90dedcee60d5fb0a3 feat: Unwanted submissions
0dc03f1970ed670b97bccffc92ab5598b5049bf8 feat: Complete the home page
9b9d085b932043a2d9250b864a1cbb1535f82184 feat: complete Product assembly
  • Solution 4: reflog + reset
$ git reflog
6fa3df5 (HEAD -> master) HEAD@{0}: commit: feat: Unwanted submissions
0dc03f1 HEAD@{1}: reset: moving to HEAD^   # 👈  this
aa47ff2 HEAD@{2}: reset: moving to HEAD
aa47ff2 HEAD@{3}: reset: moving to HEAD@{6}

$ git reset --hard HEAD@{1}
HEAD is now at 0dc03f1 feat: Complete the home page

0x006 cancel a merge

  • Scene display
The feat/A and feat/B branches are merged into the master, as shown in the figure below. Now you want to undo the merge of feat/B and master

![image.png](https://intranetproxy.alipay.com/skylark/lark/0/2020/png/281707/1590472368645-536b45c9-ebc2-4f6e-858a-f906614ba5cb.png)  
  • Solution 1: reset
$ git log --pretty=oneline
aeca903ffad7140b2d1c854c936290c006542f05 (HEAD -> master) Merge branch 'feat/B'
7c8f46806ffb2ee5961e24b4a435d68f893c78c4 (feat/A) feat: Add features a   # 👈  Go back to this
1b2dd75b7193a9b37e20a269f5490971adfe4b50 (feat/B) feat: Add features b
0dc03f1970ed670b97bccffc92ab5598b5049bf8 feat: Complete the home page
9b9d085b932043a2d9250b864a1cbb1535f82184 feat: complete Product assembly
$ git reset --hard 7c8f46806ffb2ee5961e24b4a435d68f893c78c4
HEAD is now at 7c8f468 feat: Add features a

Look at the results

![image.png](https://intranetproxy.alipay.com/skylark/lark/0/2020/png/281707/1590472911298-e89e3453-14ff-4ad6-bf32-3c28e4e26699.png?x-oss-process=image%2Fresize%2Cw_1500 "image.png")
  • Solution 2: revert
$ git log --pretty=oneline
aeca903ffad7140b2d1c854c936290c006542f05 (HEAD -> master) Merge branch 'feat/B'
7c8f46806ffb2ee5961e24b4a435d68f893c78c4 (feat/A) feat: Add features a  # 👈  Just go back to this
1b2dd75b7193a9b37e20a269f5490971adfe4b50 (feat/B) feat: Add features b
0dc03f1970ed670b97bccffc92ab5598b5049bf8 feat: Complete the home page
9b9d085b932043a2d9250b864a1cbb1535f82184 feat: complete Product assembly

$ git revert aeca903ffad7140b2d1c854c936290c006542f05 -m 1   # 👈  1 is branch number only
\[master a26fa95\] Revert "Merge branch 'feat/B'"
 1 file changed, 1 deletion(-)
 delete mode 100644 b.txt
  • Solution 3: reflog + reset
$ git reflog
aeca903 (HEAD -> master) HEAD@{0}: merge feat/B: Merge made by the 'recursive' strategy.
7c8f468 (feat/A) HEAD@{1}: merge feat/A: Fast-forward   # 👈  Just go back to this
0dc03f1 HEAD@{2}: checkout: moving from feat/A to master
7c8f468 (feat/A) HEAD@{3}: commit: feat: Add features a
0dc03f1 HEAD@{4}: checkout: moving from feat/B to feat/A
1b2dd75 (feat/B) HEAD@{5}: commit: feat: Add features b
0dc03f1 HEAD@{6}: checkout: moving from feat/A to feat/B
0dc03f1 HEAD@{7}: checkout: moving from master to feat/A

$ git reset --hard HEAD@{1}
HEAD is now at 7c8f468 feat: Add features a

result:

0x007 delete a large / sensitive file in the submission history

In my opinion, this function has two uses:

  1. Sensitive files, such as keys, have been submitted in the past and are expected to be removed from the submission history
  2. A large file was submitted, which made the whole project larger. Although it was deleted later, it still exists in the history
  • Scene display
\# Accidentally submitted an installation package to
$ git add .
$ git commit -m 'feat: Complete the home page'
\[master aa23c6a\] feat: Complete the home page
 2 files changed, 1 insertion(+), 1 deletion(-)
 create mode 100644 XMind-for-macOS-10.1.0-202003221812.dmg    # 👈 Large files added accidentally
 
 # Try to remove the installation package
 $ rm XMind-for-macOS-10.1.0-202003221812.dmg
 $ git add .
 $ git commit -m 'feat: Remove installation package'
\[master 68a3be8\] feat: Remove installation package
 1 file changed, 0 insertions(+), 0 deletions(-)
 delete mode 100644 XMind-for-macOS-10.1.0-202003221812.dmg  # 👈 delete

On the face of it, it's deleted, but look at the submission history. This file still exists. If it's a key file, there's a risk of disclosure.

And the files are very large. Let's see how big our project is at this time:

Solution 1: filter branch

$ git filter-branch --force --index-filter 'git rm --cached --ignore-unmatch XMind-for-macOS-10.1.0-202003221812.dmg' --prune-empty --tag-name-filter cat -- --all
Rewrite b426e7e5fe1633d4321403e36be5656a99ba4ad8 (1/5) (0 seconds passed, remainRewrite 3bf1398f9010f2358ddb9bb6c29669c11a2fea01 (2/5) (0 seconds passed, remainRewrite 9b9d085b932043a2d9250b864a1cbb1535f82184 (3/5) (0 seconds passed, remainRewrite aa23c6ad093baa8a90264e5d8c761d8cc7b062f8 (4/5) (0 seconds passed, remaining 0 predicted)    rm 'XMind-for-macOS-10.1.0-202003221812.dmg'
Rewrite 68a3be8e878eaba59af800c63856d488de3f3df3 (5/5) (0 seconds passed, remaining 0 predicted)
Ref 'refs/heads/master' was rewritten

Take a look at history:

Look at the size. It's still the same. What's the matter

Continue:

$ git for-each-ref --format='delete %(refname)' refs/original | git update-ref --stdin
$ git reflog expire --expire=now --all
$ git gc --prune=now
Enumerating objects: 16, done.
Counting objects: 100% (16/16), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (16/16), done.
Total 16 (delta 3), reused 14 (delta 3)

Note: git filter branch has many pitfalls and is no longer recommended for rewriting history. Consider using git filter repo, a Python script that does better than most of the applications that use filter branch. Its documentation and source code are accessible https://github.com/newren/git-filter-repo obtain.

0x008 recruitment

New retail technology business group - the front-end team of CCO technology department. We are committed to the research of Web front-end technology, and also attach importance to the ability of the whole stack and the promotion of products. Glue is the glue. We glue the relationship between design and technology, users and products, and use the coolest technology to improve the customer service experience and the happiness of the sophomore's work. FGT can also be regarded as the abbreviation of Fighting. We are Fighting in the place where we can hear the gunfire. We need to take care of the last step of Alibaba's user experience!

We are responsible for a wide range of product lines, such as PC, wireless, internal CRM, Native, etc. no matter what your technical characteristics, you can always find the right application scenarios.

Our business is also very challenging. The whole BU is committed to driving service change through product technology innovation and changing the traditional human intensive service mode. By building a one-stop service platform, we can provide enterprises with a full range of customer service solutions, improve service experience and greatly reduce service costs. By exporting Alibaba platform precipitation, build a service ecosystem and contribute hydropower coal to the e-commerce service system.

If you come to our team, you will be responsible for:

  • Business front-end development - platform ease of use and continuous improvement of user experience
  • Research on Web frontier technology and new technology

Job requirements:

  • Proficient in various front-end technologies, with PC / wireless terminal development capabilities, with complex product development experience.   
  • Master the common methods of performance optimization and the methods of measuring product performance.   
  • Have a certain understanding of MVC/MVVM and other modes, be familiar with React/Vue and other popular frameworks, and have practical project experience.   
  • Have a certain understanding of front-end engineering, and master the use and configuration of Webpack/Grunt/Gulp and other construction tools. '
  • Familiar with ES6/Node.js And has certain development ability.

Resume address:

  • cangxiu.lyx@alibaba-inc.com

Wechat (you can make friends without sending your resume):

Topics: Programming git vim Python github