Git 实用技巧

打开配置文件

git config --global --edit

改变默认编辑器

git config --global core.editor vim

删除远程git仓库里面的文件

# 取消对某个文件/文件夹的追踪
git rm -r --cached file_path/fold_path
git commit -m "删除远程文件"
git push

将远程分支(还没有merge到master的分支)拉到本地

# 先进入 master 分支, 保证新建的分支是以 master 为基准
git checkout master
# 与远程仓库进行同步
git pull
# 在本地创建dev 分支, 并且关联到dev分支
git checkout -b dev origin/dev

操作分支

# 删除本地分支-
git branch -d dev
git branch -D dev # 强行删除分支

# 删除远程分支
git push --delete origin branch

# 重命名分支
git branch -m [old_branch_name] new_branch_name # 重命名本地分支
git push origin :old_branch_name  # 删除远程分支
git push origin branch_name   # 将本地新分支推送到远程

# 设置对应的远程(上游)分支
git push --set-upstream <remote> <branch>

# 给文件重命名
git mv old_name new_name
git mv old_folder new_folder

查看某次 commit 涉及的文件

git show commit_id --stat

git stash

  • git stash apply: 继续编辑最新的草稿;
  • git stash list: 列出所做存储的草稿;
  • git stash == git stash push:
  • drop:
  • pop:
  • clear:
  • create:
  • store:

换行符

Win 下换行符是 \n\r (CRLF);
Linux/Mac 下换行符是 \n (LF);

VSCode 下建议将默认换行符都设置为 \n (LF). setting: FIle: Eol

git config --global core.autocrlf input

If you’re on a Linux or macOS system that uses LF line endings, then you don’t want Git to automatically convert them when you check out files; however, if a file with CRLF endings accidentally gets introduced, then you may want Git to fix it. You can tell Git to convert CRLF to LF on commit but not the other way around by setting core.autocrlf to input:

离线更新

  1. 将最近代码打包成 bundle 文件: git bundle create bundle_tag.bundle master
  2. 将远程分支打包成 bundle 文件: git bundle create bundle_tag.bundle origin master(推荐使用)
  3. 使用 bundle 文件离线更新仓库: git pull bundle_tag.bundle master
  4. 将其他分支打包成 bundle 文件: git bundle create bundle_tag.bundle branch_name
  5. 使用 bundle 文件离线更细仓库: git pull bundle_tag.bundle branch_name

合并多个 commit 有以下几种方式

  • git commit --amend: 在上一个 commit 的基础上添加本次改动;

  • git log: 首先调出提交的 commit 历史, 然后找到一个基准点的commit id;

    git rebase -i commit_id: 可对该 commit_id 之后提交的所有 commit 进行整理

    • pick: 使用该次commit;
    • squash: 将本次commit合并到上个commit, 并且之后编辑 commit 备注;
    • fixup: 将该条 commit 合并到上个 commit, 合并之后使用上次的备注.

git diff 查看 2个 commit 之间的变动

git diff commit_id1 commit_id2: 可查看这2个commit之间每个文件的改动

-- name-only: 只查看发生改变的文件名: 如 git diff --name-only commit_id1 commit_id2

恢复部分文件

在某次改动中经常会改动多个文件. 一般来说, 一次 commit 只包含一个文件.

如果需要将其中部分文件恢复至之前的某个状态, 可以采取一下措施:

  1. git log 确定需要回滚的 commit 编号.
  2. 恢复文件, 有以下2种方法:
    1. git checkout commit_id -- 需要恢复的文件
    2. git restore --source=commit_id 需要恢复的文件
  3. git commit -m "msg" 恢复的文件: 将恢复的文件提交

.gitignore

  • ! 表示否, 如 !mining_log.py: 不排除 mining_log.py 文件
  • 子文件夹中也可以有 .gitignore .gitkeep 文件.

git reset

git reset commit_id: 将 index and working tree 恢复至指定状态, 文件内容不变;
--hard: 将文件内容也恢复至 指定状态;

恢复文件

如果改动了某个/写文件, 但是不需要提交这些改动, 而是需要将其恢复至改动之前:

  • 老版 git: git checkout -- path/to/file;
  • 新版 git: git restore path/to/file;
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 本文首次发表在 6 条 Git 实用技巧 -- 泰晓科技 本文汇总最近一段时间用到的几则 Git 实用小技巧,欢迎...
    吴章金阅读 356评论 2 1
  • 作为一名有多年开发经验的老兵,版本控制从最开始的SVN到Git,用着还算顺手,今天总结下整理成文章,以便用时查阅,...
    jiantaocd阅读 183评论 0 0
  • 1、新建 创建一个新的 git 版本库。这个版本库的配置、存储等信息会被保存到.git 文件夹中 2、配置 更改设...
    WYL_99阅读 242评论 0 0
  • 1. 撤销操作技巧 任何时候,你都有可能需要撤消刚才所做的某些操作。本段总结git中常用的撤销操作: 修改最后一次...
    OldChicken_阅读 227评论 0 2
  • Git命令: --基础使用-- 用户名:git config --global user.name "<Your ...
    Dollkey阅读 1,789评论 0 86