美文网首页
Git 实用技巧

Git 实用技巧

作者: Guang777 | 来源:发表于2023-06-29 08:08 被阅读0次

打开配置文件

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;

相关文章

  • git/svn 技巧以及常见问题整理

    Git 实用技巧 Git乱码问题解决方案汇总 常用快捷键 常见问题 遇到多个commit回滚的情况,采用方案如下 ...

  • 6 条 Git 实用技巧

    本文首次发表在 6 条 Git 实用技巧 -- 泰晓科技 本文汇总最近一段时间用到的几则 Git 实用小技巧,欢迎...

  • git实用技巧

    作为一名有多年开发经验的老兵,版本控制从最开始的SVN到Git,用着还算顺手,今天总结下整理成文章,以便用时查阅,...

  • Git 实用技巧

    1. 撤销操作技巧 任何时候,你都有可能需要撤消刚才所做的某些操作。本段总结git中常用的撤销操作: 修改最后一次...

  • git实用技巧

    git切换/重新关联分支 重新跟踪远程文件 如下的命令,使用场景是,本地有原代码,但是没有与远程git做关联,或关...

  • 一些git实用技巧

    原创 @shhp 转载请注明作者和出处 这篇文章将通过场景分析的方式讲解一些git的实用技巧。这些技巧的使用频率或...

  • 第一课 补益中药实用技巧

    补益中药实用技巧;课程表 补益中药实用技巧 第一讲

  • 补益中药实用技巧9

    补益中药实用技巧9

  • 补益中药实用技巧8

    补益中药实用技巧8

  • 浙江省老年电大2020年秋季课程

    补益中药实用技巧 第二课 补益中药实用技巧2 https://v.youku.com/v_show/id_XND...

网友评论

      本文标题:Git 实用技巧

      本文链接:https://www.haomeiwen.com/subject/qiyyydtx.html