打开配置文件
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:
离线更新
- 将最近代码打包成 bundle 文件:
git bundle create bundle_tag.bundle master
- 将远程分支打包成 bundle 文件: git bundle create bundle_tag.bundle origin master(推荐使用)
- 使用 bundle 文件离线更新仓库:
git pull bundle_tag.bundle master
- 将其他分支打包成 bundle 文件: git bundle create bundle_tag.bundle branch_name
- 使用 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 只包含一个文件.
如果需要将其中部分文件恢复至之前的某个状态, 可以采取一下措施:
- git log 确定需要回滚的 commit 编号.
- 恢复文件, 有以下2种方法:
- git checkout commit_id -- 需要恢复的文件
- git restore --source=commit_id 需要恢复的文件
- 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;
网友评论