304 字
2 分钟
Git 常用指令
2025-02-16

Git 常用指令#

  • 克隆最新的远程仓库:git clone url
  • 拉取最新的远程仓库:git pull
  • 添加文件到暂存区:git add
    • 添加所有文件:git add .
    • 添加特定文件:git add main.c
  • 提交代码:git commit -m "message"
  • 修改提交信息:git commit --amend -m "new message"
  • 设置远程仓库:git remote set-url origin url
  • 取消上次提交,但保留修改内容:git reset --soft HEAD~1

Git Branch 分支操作#

显示所有分支

Terminal window
git branch -a

创建并切换到分支

Terminal window
git branch branch1
git checkout branch1
# 或合并成下面:
git checkout -b branch1

将 branch2 合并到 branch1 上

Terminal window
git checkout branch1 # 切换到需要合并的分支
git merge branch2 # 将 branch_name2 合并到 branch1 上

将 branch2 合并到 branch1 上,但需要忽略 branch2 的所有更改

Terminal window
git checkout branch1
git merge -s ours branch2
# -s ours 表明在合并分支时,完全忽略对方分支(branch2)的更改

Git Rmote 远程仓库#

git push 每次都需要密码,可以通过下面两种方式解决

git 远程仓库的地址为 ssh 格式,而不是 http格式

Terminal window
git remote set-url origin git@gitlab.example.com:user/remote.git

http 格式可以通过凭证助手缓存账号和密码

Terminal window
git config --global credential.helper wincred
# 或者
git config --global credential.helper store
Git 常用指令
https://fuwari.vercel.app/posts/工具/git-常用指令/
作者
Asuwee
发布于
2025-02-16
许可协议
CC BY-NC-SA 4.0