1. There are multiple Git accounts on the same computer
Suppose we have multiple Git accounts on the same computer. For example, the company uses Gitlab internally and personally uses Github or Gitee. Then there will be a situation where I want to submit code to a personal open source project when I go to work, but Git is bound to the company's account.
In this case, we can let Git bind multiple different ssh keys, and each ssh key corresponds to a different Git server.
Generate the first ssh key:
ssh-keygen -t rsa -C "xxx@xxx.xx"
Generate the second ssh key:
ssh-keygen -t rsa -f path/to/file -C "xxx@xxx.xx"
The parameter -f
indicates the file name to be generated, and path/to/file
is the file name path, for example, ~/.ssh/id_rsa_github
.
After executing the above two commands, you will get two pairs of ssh keys.
At this time, you also need to create a config
file in this directory. Write the following:
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa_github # 私钥文件路径
Host gitlab.com
HostName gitlab.com
User git
IdentityFile ~/.ssh/id_rsa
Host gitee.com
HostName gitee.com
User git
IdentityFile ~/.ssh/id_rsa_github
The purpose of this configuration file is to specify the location of the private key file.
Then we can configure the first key to the company's Gitlab server, and set the corresponding Git account and mailbox to global.
git config --global user.name "xxx"
git config --global user.email "xxx@xxx.xx"
Then configure another pair of ssh keys on Github, and configure the Git user name and email address separately in the Github project on the computer.
git config user.name "xxx"
git config user.email "xxx@xxx.xx"
At this point, we are done. You can submit code on different Gitlab and Github projects at the same time.
2. Modify the user name and email address recorded by git commit
Assume that there are both Gitlab and Github projects on the computer, and the Gitlab user information has been globally configured. Now a new Github project has been pulled, a commit has been submitted and it has been pushed to the remote warehouse. At this time, it was found that the project was not configured with Github user information, and the user information of the global account Gitlab was used by default.
We can modify the last submitted user information through the following command:
git commit --amend --author="username <yyy@ccc.com>" --no-edit
username
is the user name, the <>
beside the user mailbox cannot be removed. After modification, execute git push -f
push to the remote warehouse.
What if you want to modify the user information of multiple commits? It can be modified by the following code:
git filter-branch --commit-filter '
if [ "$GIT_AUTHOR_EMAIL" = "tanguangzhi@shiqiao.com" ];
then
GIT_AUTHOR_NAME="woai3c";
GIT_AUTHOR_EMAIL="411020382@qq.com";
git commit-tree "$@";
else
git commit-tree "$@";
fi' HEAD
After modifying the user name and mailbox in the above code, save it as .sh
format, such as edit.sh
. sh edit.sh
in the project root directory (right-click -> Git Bash Here
-> sh edit.sh
under windows), and then execute git push -f
force push.
3. Modify a message in a historical record
Assume that the current branch has four commit records of a b c d
a
b
c
d
If you want to c
the message recorded by 06189dd3adde95. You can use git rebase
change the c
record to the top, and then use git commit --amend
to modify its message.
Specific steps
Execute the following command to edit the three commits in front of d
git rebase -i d
After the vim editing interface, move the cursor to the c record, press dd
cut the record, then move the cursor to the first line, press p
paste, and then type :wq
save.
Execute git commit --amend
to modify c
record after the switching sequence. After entering the vim editing interface, press i
to modify, then press ESC
, and then enter :wq
save.
Finally, use the git rebase
operation c
restore the 06189dd3addf59 record to its original position.
The execution result of this process is the same as the above figure. This is the comparison between the current branch and the remote branch after modification. The recorded message pointed to by the arrow is the modified message.
If you want to synchronize the modified records to the remote warehouse, you only need to execute git push -f
this time.
The second way
- Use
git checkout -b <branchName> c
cut a branch from the specified record git commit --amend
modify the commit message in the new branch- Use
git cherry-pick
tob a
record to the new branch ( pay attention to , where theb a
commit record refers to the commit on the original branch, that is, select theb a
record on the original branch and add it to the new branch, so that the record on the new branch is Becomesa b c
, and the commit message recorded by c has been modified in the second step) - Use
git checout original branch name to switch back to the original branch, then execute
git rebase <branchName>
merge the new branch, and finally push to the remote branch
4. Select the specified commit to merge
Suppose you cut a bugFix branch to fix an online bug, and after a period of hard work, the bug is finally fixed. But for debugging (adding a lot of debug code) or other reasons, there are a lot of useless log messages on bugFix.
commit3: 修复登录 bug
commit2: 添加 debug 语句
commit1: 添加 console 语句
For example, in the three records above, a lot of debugging code was added to the first two records, and the bug was finally fixed in the last record, and the debugging code was deleted. At this time, if you directly merge the bugFix branch to the master branch, the debugging records will also be merged.
At this time, you can use git cherry-pick
only the last record into the master branch. Or use git rebase
to discard the other two records of the bugFix branch, and then merge them.
5. The difference between ^
and ~
Operator ^
and ~
character, as may be later followed by a number. ~
means to go back several generations of records.
However, the number after the operator is ~
after 06189dd3ade1fc. It is not used to specify the number of generations to return, but to specify the parent record of the merge submission record.
Git selects the "first" parent record of the merge commit by default, and ^
can change this default behavior.
It may not be easy to understand just looking at the text. Let’s look at a few examples.
Execute the command git checkout main^
to return to the first parent record (the original HEAD pointed to c3, now it points to c1).
Execute the command git checkout main^2
to return to the second parent record (the original HEAD pointed to c3, now it points to c2).
Finally, here is a more complex example:
G H I J
\ / \ /
D E F
\ | / \
\ | / |
|/ |
B C
\ /
\ /
A
A = = A^0
B = A^ = A^1 = A~1
C = A^2 = A^2
D = A^^ = A^1^1 = A~2
E = B^2 = A^^2
F = B^3 = A^^3
G = A^^^ = A^1^1^1 = A~3
H = D^2 = B^^2 = A^^^2 = A~2^2
I = F^ = B^3^ = A^^3^
J = F^2 = B^3^2 = A^^3^2
Through these examples, we can also find that ~n
is equal to n consecutive ^
.
6. The difference between git revert
and git reset
git reset
can go back to the history of Git. For example, the current branch has three records, and HEAD points to the c record:
c <- HEAD
b
a
git reset b
back to the b record, just execute 06189dd3ade386:
b <- HEAD
a
Then use git push -f
to force the branch after the rollback version to be pushed to the remote warehouse, so that the local branch and the remote branch are synchronized.
git push -f
git revert
can also revoke records, but the revoked records will not disappear, which is different from git reset
. git reset
revoked record is the same as disappeared.
Now we use git revert
to re-demonstrate the operation just now:
c
b
a
If we execute git revert b
, will be re-created on the current branch commit a new record, become a b c b'
, this b'
state and record b
is the same.
In other words, git reset b
, the current branch record will become a b
. After executing git revert b
, the current branch record will become a b c b'
.
If you want others to know that you have revoked the record, use git revert
, because it will leave the revoked record, otherwise use git reset
.
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。