Skip to content

git常用命令清单

jiaxw32 edited this page Dec 7, 2021 · 5 revisions

仓库配置

  • 查看远程仓库地址
git remote -v
  • 查看 git 配置信息
git config --list
  • 查看全局配置信息
git config --global --list
  • 查看本地配置信息
git config --local --list
  • 编辑配置信息
git config -e
  • 修改当前仓库用户名
git config user.name "jiaxw32"
  • 修改当前仓库邮箱
git config user.email "jiaxw32@gmail.com"

克隆

  • 基于特定 tag 克隆
# swift-5.5.1-RELEASE 为 tag 名
git clone -b swift-5.5.1-RELEASE git@github.com:apple/llvm-project.git

分支管理

  • 查看本地分支
git branch
  • 查看远程分支
git branch -r
  • 删除分支
git branch -d <branch_name>

日志查看

  • 显示所有提交
git log
  • 显示合并提交
git log --merges
  • 显示当前分支的最近几次提交
git reflog
  • 显示指定作者、指定日期提交
# 显示指定作者提交
git log --author=jiaxw

# 显示指定日期提交
git log --author=jiaxw --since=2020-04-01

# 导出提交日志
git log --author=jiaxw --since=2020-04-01 --reverse > ~/log.txt

代码储存(stash)

当您想记录工作目录和索引的当前状态,但又想回到一个干净的工作目录时,请使用 git stash。该命令会将你的本地修改保存起来,并将工作目录恢复到与 HEAD 提交一致的状态。

Use git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. The command saves your local modifications away and reverts the working directory to match the HEAD commit.

# 存储当前目录状态
git stash
# 查看存储列表
git stash list
# 从暂存列表中删除一个单一的暂存状态,并将其应用到当前工作树状态之上。
git stash pop
# 查看 git stash 帮助文档
git stash --help

代码合并

使用 rebase 来合并,提交纪录不会显示自动生成的 commit merge,分支更简洁。执行 git pull --rebase 时,本地目录不能存在修改状态的文件。

# 通过 `rebase` 而不是 `merge` 来合并更改。
git pull --rebase

提交回退

  • 本地回退
# 使用 git reflog 查看 commit
git reset --hard <commit>
  • 远程回退
1. 查看提交记录: git reflog
2. 重置本地提交:git reset --hard <commit>
3. 远程推送:git push -f

github 同步 fork 分支

# 1. Check out the branch you wish to merge to. Usually, you will merge into the default branch.
$ git checkout master

# 2. Pull the desired branch from the upstream repository. This method will retain the commit history without modification.
$ git pull https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git BRANCH_NAME

# 3. If there are conflicts, resolve them.

# 4. Commit the merge.

# 5. Push the merge to your GitHub repository.
$ git push origin master

其他

  • 统计仓库作者提交纪录
git shortlog -sn

# 排除 merge 提交
git shortlog -sn --no-merges

参考资料

Clone this wiki locally