Git 命令整理

git.png

基础

  • git init :初始化一个 Git 版本库
  • git add <file> :把文件添加到仓库
    • git add -f <file> :强制添加文件
  • git commit :把文件提交到仓库;-m "xxx" :提交说明
  • git status :显示工作区状态
  • git diff :查看修改内容
  • git log :显示提交日志
    • --pretty=oneline :更简洁地显示日志
  • git reset --hard commit_id :回退到历史版本
  • git reflog : 查看命令历史
  • git diff HEAD -- <file> :查看工作区和版本库里面最新版文件的区别
  • git checkout -- <file> :丢弃工作区的修改
  • git reset HEAD <file> :把暂存区的修改撤销掉,重新放回工作区
  • rm <file>git rm <file> :删除文件
  • git check-ignore -v <file> :检查 .gitignore 规则哪里写错

远程仓库

  • ssh-keygen -t rsa -C "youreamil@example.com" 创建 SSH Key
    用户主目录下会创建 .ssh 目录,里面有 id_rsaid_rsa.pub 两个文件。id_rsa 是私钥,id_rsa.pub 是公钥。
  • git remote add origin git@github.com:xxx/xxx.git
    链接远程仓库,远程库的名字是 origin
  • git push -u origin master
    -u 会把本地的 master 分支内容推送到远程新的 master 分支,还会把本地的 master 分支和远程的 master 分支关联起来,在以后的推送或者拉取时就可以简化命令。
  • git push origin master
  • git clone git@github.com:xxx/xxx.git
    克隆一个本地库。
  • git remote :查看远程库的信息,-v 显示更详细的信息
  • git push origin <name> :推送分支
  • git pull :抓取远程的新提交
  • git checkout -b <branch_name> origin/<branch_name> :创建远程 origin<branch_name>分支到本地
  • git branch --set-upstream-to=origin/<branch_name> <branch_name> :创建本地 <branch_name> 分支和远程 <branch_name> 分支的链接关系
  • git push <remotename> <commit SHA>:<remotebranchname> :推送特定提交

分支管理

  • git checkout -b <name> :创建 name 分支,相当于 git branch <name>git checkout <name> 两条命令
  • git branch :查看当前分支
  • git checkout <name> :切换分支,或使用 git switch <name>
  • git merge <name> :合并指定 xxx 分支到当前分支。通常 Git 会用 Fast Forward 模式,但这种模式下删除分支后会丢掉分支信息。
  • git branch -d <name> :删除分支
  • git branch -D <name> :强行删除,如果要丢弃一个没有被合并过的分支
  • git branch -m <old> <new> : 分支重命名
  • git log --graph --pretty=oneline --abbrev-commit :查看分支合并图
  • git merge --no-ff -m "<info>" <name> :禁用 Fast forward 模式,合并分支时生成一个新的 commit,可以从分支历史上看出分支信息。
  • git stash :保存当前工作状态
  • git stash list :查看工作现场存储
  • git stash apply :恢复工作状态,stash 内容不删除
  • git stash apply stash@{?} :恢复指定的 stash
  • git stash clear :清除所有内容
  • git stash drop :删除 stash 内容
  • git stash pop :恢复工作状态并删除 stash 内容
  • git rebase :把本地未 push 的分叉提交历史整理成直线
  • git push --all origin :推送所有分支

标签管理

  • git tag <tag-name> :打标签,-a 指定标签名,-m 指定说明文字,-s 用私钥签名一个标签。默认标签是打在最新提交的 commit 上的。
  • git tag <tag-name> <commit id> :给历史提交打标签
  • git tag :查看所有标签
  • git show <tag-name> :查看标签信息
  • 标签总是和某个 commit 挂钩。如果这个 commit 既出现在 master 分支,又出现在 dev 分支,那么这两个分支上都可以看到这个标签。
  • git tag -d <tag-name> :删除本地标签
  • git push origin <tagname> :推送标签到远程
  • git push origin --tags :推送全部尚未推送到远程的本地标签
  • git push origin :refs/tags/<tag-name> :删除远程标签

自定义 Git

  • 每个仓库的 Git 配置文件存放在 .git/config 文件中,当前用户的 Git 配置文件放在用户主目录下的 .gitconfig
  • git config --global user.name "your name" :设定名字
  • git config --global user.email "email@example.com" :设定邮箱
  • git config --global core.editor vim : 设置默认编辑器
  • git config --global color.ui true :让 Git 显示颜色
  • git config --global alias.xx xxxxx :配置别名
  • git config --global init.defaultBranch <name> :自定义初始分支名称
  • git config --list :查看已经有的配置信息

Git Large File Storage

  1. 安装

    1
    sudo apt install git-lfs
  2. 验证安装成功

    1
    2
    $ git lfs install
    > Git LFS initialized.
  3. 将仓库中的文件类型与 Git LFS 相关联

    1
    2
    $ git lfs track "*.psd"
    > Adding path *.psd
  4. 将本地 .gitattributes 文件提交到仓库中

  5. 将文件添加到与关联的扩展名相匹配的仓库

    1
    git add path/to/file.psd

其它

git push -u origin master-u 的含义

  • 来自文档中的解释:

    1
    2
    3
    4
    -u, --set-upstream
    For every branch that is up to date or successfully pushed,
    add upstream (tracking) reference, used by argument-less git-pull(1)
    and other commands.
  • 当在一个分支中使用 git pull 时,如果没有指定一个远程分支,git 会寻找 branch.<name>.merge 设置来了解从何处 pull。

  • 所以 -u 用来设置本地与远程分支之间的联系。

    1
    git push -u origin master

    应该与下面的命令相同:

    1
    2
    git push origin master
    git branch --set-upstream master origin/master

    设置完成后就可以直接使用 git pull 来代替 git pull origin master

合并多个 Commit

在使用 Git 作为版本控制的时候,我们可能会由于各种各样的原因提交了许多临时的 commit,而这些 commit 拼接起来才是完整的任务。那么我们为了避免太多的 commit 而造成版本控制的混乱,通常我们推荐将这些 commit 合并成一个。

查看提交历史

首先你要知道自己想合并的是哪几个提交,可以使用 git log 命令来查看提交历史,假如最近 4 条历史如下:

1
2
3
4
5
6
7
commit 3ca6ec340edc66df13423f36f52919dfa3......

commit 1b4056686d1b494a5c86757f9eaed844......

commit 53f244ac8730d33b353bee3b24210b07......

commit 3a4226b4a0b6fa68783b07f1cee7b688.......

历史记录是按照时间排序的,时间近的排在前面。

git rebase

想要合并 1 - 3 条,有两个方法

  1. 从 HEAD 版本开始往过去数 3 个版本

    1
    git rebase -i HEAD~3
  2. 指名要合并的版本之前的版本号

    1
    git rebase -i 3a4226b

    请注意 3a4226b 这个版本是不参与合并的,可以把它当做一个坐标。

选取要合并的提交

  1. 执行了 rebase 命令之后,会弹出一个窗口,头几行如下:

    1
    2
    3
    4
    5
    pick 3ca6ec3   '注释**********'

    pick 1b40566 '注释*********'

    pick 53f244a '注释**********'
  2. pick 改为 squash 或者 s ,之后保存并关闭文本编辑窗口即可。改完之后文本内容如下:

    1
    2
    3
    4
    5
    pick 3ca6ec3   '注释**********'

    s 1b40566 '注释*********'

    s 53f244a '注释**********'
  3. 然后保存退出,Git 会压缩提交历史,如果有冲突,需要修改,修改的时候要注意,保留最新的历史,不然我们的修改就丢弃了。修改以后要记得敲下面的命令:

    1
    2
    git add .
    git rebase --continue

    如果你想放弃这次压缩的话,执行以下命令:

    1
    git rebase --abort
  4. 如果没有冲突,或者冲突已经解决,则会出现如下的编辑窗口:

    1
    2
    3
    4
    5
    6
    7
    8
    # This is a combination of 4 commits.
    #The first commit’s message is:
    注释......
    # The 2nd commit’s message is:
    注释......
    # The 3rd commit’s message is:
    注释......
    # Please enter the commit message for your changes. Lines starting # with ‘#’ will be ignored, and an empty message aborts the commit.
  5. 输入 wq 保存并退出,再次输入 git log 查看 commit 历史信息,你会发现这两个 commit 已经合并了。

将一个分支推送到新的仓库

如果想将一个仓库中原有的分支转换为新的远程仓库,可以执行如下操作:

1
git push url://to/new/repository.git branch-to-move:new-branch-name

重命名 master 分支为 main

Github 上重命名 master 分支为 main 分支,本地仓库设置:

1
2
3
4
git branch -m master main
git fetch origin
git branch -u origin/main main
git remote set-head origin -a

删除远程仓库中已存在的文件或文件夹

如果需要在远程仓库中忽略已经纳入版本管理的文件或文件夹,可以这样操作:

  1. 删除本地 git 缓存

    1
    git rm -r --cached <filename>
  2. .gitignore 中加入需要忽略的文件或文件夹

  3. 重新追踪文件

    1
    2
    git add .
    git commit -m "remove files"
  4. 推送到远程仓库

Convert a git repository to a shallow repository

1
2
git pull --depth 1
git gc --prune=all

批量修改提交中的邮箱信息

需要用到 git-filter-repo 的 CALLBACKS 功能

1
2
3
4
5
6
7
8
9
10
11
# 安装
sudo apt install git-filter-repo
#将所有用户名中包含的 foo 替换成 ttys3 (注意,不支持中文)
git-filter-repo --name-callback 'return name.replace(b"foo", b"ttys3")'

#将所有 commit 信息的 email 中包含的 foo@example.com 替换成 my-email@example.com
git-filter-repo --email-callback 'return email.replace(b"foo@example.com", b"my-email@example.com")'
# 重新链接仓库地址
git remote add origin <git_address>
# 强制提交
git push origin main --force

修改历史提交信息

git commit --amend 用于修改上次提交信息。

git 使用 amend 选项提供了最后一次 commit 的反悔。但是对于历史提交呢,就必须使用 rebase 了。

  1. git rebase -i HEAD~3:如果你要修改哪个,就把那行的 pick 改成 edit,然后保存退出。
  2. 这时通过 git log 你可以发现,git 的最后一次提交已经变成你选的那个了,这时再使用:git commit --amend
  3. 修改完了之后,要回来使用 git rebase --continue

仓库迁移

如果将项目从 Gitlab 迁移到 Github 上,可以进行如下操作:

  1. 进入 .git 文件夹下,打开 config 文件
  2. 修改 remote "origin" 分类下的 url 字段,更改为 Github 仓库地址
  3. 重新推送,即 git push origin master

参考