Git Command Quick Search

This document provides detailed instructions on various Git operations, including password management, resetting commits, handling commits, and modifying remote repositories. It also covers advanced topics like auto-conversion of line endings and merging commits.

1. git 用户密码的保存和删除

1
2
3
4
5
# 保存密码
git config --global credential.helper store

# 清除密码
git config --system --unset credential.helper
1
git remote set-url remotename https://username:password@github.com/gitname/repo_name.git

2. git reset

2.1 撤销 commit (soft)

1
2
3
4
5
6
7
8
# 撤销上一条 commit
git reset --soft HEAD^

# 撤销上n条 commit
git reset --soft HEAD^n

# 撤销 commit 到某个 commit-version
git reset --soft <commit-version>

soft reset 仅仅撤销提交不回滚代码

2.2 撤销 commit 和 add (mixed)

1
git reset --mixed HEAD^

2.3 修改 commit 内容 (amend)1

1
git commit --amend

2.4 回滚代码 (hard)

1
git reset --hard <commit-version>

hard reset 会将代码强制回滚

如果需要强制提交到远端

1
git push -f origin master

强制 push 会导致当前 commit 之后的所有提交永久性丢失

3. 查看 commit-version

1
2
3
4
5
# 查看远端的 commit log
git log

# 查看本地的 commit log
git reflog

如果你的队友 push -f 了代码,而你又不幸 pull 了代码,可以通过 reflog 找回你之前的本地提交

4. 代码入栈2

pop 之后可能需要解决冲突

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
# 当前代码入栈,并恢复到线上的 commit
git stash

# 修改代码并提交
git add --all
git commit -m "fix"
git push

# 代码出栈
git stash pop

5. 代码入栈并拉取新分支3

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# 当前代码入栈,并恢复到线上的 commit
git stash

# 新建分支
git branch master-dev-fix
# 切换分支
git checkout master-dev-fix
# 也可以新建并切换分支
#git checkout -b master-dev-fix

# 修改代码并提交
git add --all
git commit -m "fix"

# 合并到 master
git checkout master
git merge master-dev-fix

# push 到线上
git push

# 恢复代码
git stash pop

6. 自动转换换行符

1
2
3
4
5
6
7
8
# 提交时转换为LF,检出时转换为CRLF
git config --global core.autocrlf true

# 提交时转换为LF,检出时不转换
git config --global core.autocrlf input

# 提交检出均不转换
git config --global core.autocrlf false

7. 使用字符串而不是 ascii 码输出

1
git config --global core.quotepath false

8. 修改远程仓库地址

1
git remote set-url origin https://giturl.git

9. 合并提交4

1
2
git rebase -i HEAD~3
# set merge commit to squash

10. 参考

使用 Hugo 构建
主题 StackJimmy 设计