Lei Zhilong

The best way to input is to output

Jul 20, 2020 - 3 minute read - Comments - Cheat Sheets

Git

Disclaimer: This article will be updated from time to time to add new content or make any changes in exsisting sections without any notice. Using them under your own investigation in production is advised.

I. Git Submodule

1. Delete a submodule

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

git submodule deinit {MOD_PATH}

# delete mod info from .gitmodules
cat .gitmodules | gre -v {MOD_PATH} > .gitmodules

# use --cached to clear info in .git/modules
git rm --cached {MOD_PATH}

git commit -am "Remove a submodule."

2. Modify submodule URL

 1
 2
 3
 4
 5
 6
 7
 8
 9
10

# 1. modify submodule url in .gitmodules
vim .gitmodules

# 2. use `git submodule sync` to flush URL into  .git/config
git submodule sync
cat .git/config

# 3. commit change
git commit -am "Update submodule url."

II. Git ADD options(-A, ., -u)

  • git add -A - add all file changes, including updating, adding and deleting.
  • git add -u - add changes of indexed files, including updating and deleting.
  • git add . - add changes of files in current working directory, including adding, updating but excluding missing files.

Example:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47

git init
echo Change me > change-me
echo Delete me > delete-me
git add change-me delete-me
git commit -m initial

echo OK >> change-me
rm delete-me
echo Add me > add-me

git status
# Changed but not updated:
#   modified:   change-me
#   deleted:    delete-me
# Untracked files:
#   add-me

git add .
git status

# Changes to be committed:
#   new file:   add-me
#   modified:   change-me
# Changed but not updated:
#   deleted:    delete-me

git reset

git add -u
git status

# Changes to be committed:
#   modified:   change-me
#   deleted:    delete-me
# Untracked files:
#   add-me

git reset

git add -A
git status

# Changes to be committed:
#   new file:   add-me
#   modified:   change-me
#   deleted:    delete-me

III. Mirror

Thanks to some great firewalls, you may need to use a mirror site to access github.com.

1
git config --global url."https://github.com.cnpmjs.org/".insteadOf https://github.com/

IV. Revert

1. Revert multiple commits

Suppose you have a commit chain like:

1
A -> B -> C -> D

And you’d like to revert B to D to get a result like:

1
A-> B ->C -> D -> D'-> C' -> B'

This is the way:

1
git revert B^..D

And if you wanna combine the reverts in one commit:

1
2
git revert -n B^..D
git commit -m "revert from B to D"

V. Push

1. Push to multiple remote repositories simultaneously

First, add a URL to you existing remote. Yes, a repository can have multiple remote URL!

1
2
3
4
5
6
7
8

git remote set-url --add origin git@gitlab.com:fakeuser/my-repo.git

git remote -v

origin	git@github.com:fakeuser/my-repo.git (fetch)
origin	git@github.com:fakeuser/my-repo.git (push)
origin	git@gitlab.com:fakeuser/my-repo.git (push)

Then when you push with git push origin master, you’ll get two results:

1
2
Everything up-to-date
Everything up-to-date