git 常用指令

306 阅读4分钟

合并多个 commit

git rebase -i HEAD~2 # 合并最近两个 commit

git 切换账号

osxkeychainmacos 默认存储

security delete-internet-password -s <github.com>

更新本地代码

git pull --rebase origin origin_branch

新建分支

git checkout -b new_branch  # 新建并切换分支

切换分支

git checkout branch   # 切换分支
git checkout -        # 切换到上一次分支

查看本地文件状态

git status

暂存代码

git add .

提交代码

git commit -m 'message'

查看提交信息

git log             # 当前分支
git log --all       # 所有分支
git log --oneline

代码推到远端

git push origin origin_branch

合并分支

git merge --no-ff branch

清除本地文件修改

git checkout -- xxx   # 某个文件
git checkout -f       # 所有

临时保存最近修改

git add .     # 没有被 git 管理的文件,需要使用
git stash

查看 stash

git stash list

恢复临时保存

git stash pop stash@{stash_id}

git stash 冲突

git stash pop 冲突,不会自动删除git stash中的记录需手动清理

git stash drop stash@{stash_id}

删除本地分支

git branch -d local_branch    # 普通删除
git branch -D local_branch    # 强制删除

删除远端分支

git push --delete origin origin_branch

Tip:无法重命名远端分支,需要现删除远端分支,再将本地分支推到远端。

将本地分支推送到远端

git push origin local_branch:origin_branch

拉远端分支

git fetch
git checkout origin_branch

重命名本地分支

git branch -m old_branch new_branch   # 不在 old_branch
git branch -m new_branch      # 在 old_branch

修改最近一次提交信息

git commit --amend -m 'message'

修改 git 仓库开发者信息

git config --global user.name
git config --global user.email

撤销上一次提交

git reset HEAD^1

回滚

git reset --hard commit_id

取消合并

git merge --abort

查看所有操作记录

git reflog

Tip:搭配回滚可御剑飞行

打标签

git tag v1.2.3    # 当前版本打标签
git push origin --tags    # 标签推到远端

查看 git 远程关联

git remote -v

关联远程仓库

git remote set-url --add origin origin_url

添加远程仓库

git remote add origin_name origin_url

从 git 中删除某个文件的记录

先在 .gitignore 中添加需要忽略的文件或文件夹,但 git 仍然跟踪着这些文件的记录

可以使用下面的命令将其从 git 的记录中删除

git rm --cached -r <directory_path> # 从 git 中删除文件夹记录
git rm --cached <file_path> # git 中删除文件记录

注意项目的操作危险

git rm -r <directory_path> # 同时删除文件夹和文件夹记录
git rm <file_path> # 同时删除文件和文件记录

git clone 使用 --branch 和 --single-branch 参数

git clone <url> --depth 1 --branch master --single-branch --filter=blob:none

参数说明:

  • --depth 1:指定 clone 的深度为 1,意味着只获取仓库的最新一次提交记录,而不是整个项目完整历史记录
  • --branch master:指定要 clone 的分支为 master
  • --single-branch:该表示只克隆指定的分支(这里是 master 分支)
  • --filter=blob:none:用于过滤掉仓库中的二进制文件(blobs),只克隆文件的元数据和提交历史

使用了 --single-branch 之后,如何拉取其他分支

直接更新特定分支

使用这种方法拉取分支比较纯粹,只会关联你想要的分支,其他不需要的分支不会被拉取下来

git fetch origin <branch-name>:<branch-name>
git checkout <branch-name>

验证

git branch -a # 查看所有分支,只能看到本地分支和远程通过 --single-branch 拉取下来的分支

用这种方法拉取的分支是没有和远程关联的,如果需要关联远程分支,可以使用下面的命令

git push --set-upstream origin dev

更新远程分支信息

用这个方法可以拉取到所有远程分支的信息,和正常 clone 下来的仓库一样

git remote set-branches origin '*'

git worktree 工作流程

假设目录结构

/project/
  ├── main/          # 主工作目录
  └── main-feature   # 新功能

main 目录下创建一个新的工作空间

git worktree add ../main-feature -b feature   # 新建一个分支
git worktree add ../main-feature feature      # 使用现有分支,要确保该分支当前没有被其他 worktree 使用

开发完之后在当前分支下正常提交代码

回到主分支,执行合并分支的操作

cd ../main
git checkout main
git merge/git rebase  # 有冲突需要解决一下
git push

删除 worktree

git worktree remove main-feature  # 只会删除工作目录,不会删除分支
git branch -d feature             # 删除分支

要注意的是,使用 git worktree,未被 git 托管的代码或者文件不会跟随过来,所以需要注意一下

查看工作树列表

git worktree list