Git 常用命令

jc39
jc39
发布于 2026-02-07 / 3 阅读
0
0

Git 常用命令

基本命令

# 克隆
git clone <url>
# 拉取
git pull
# 新建分支
git checkout -b <branch>
# 合并
git merge <target-branch>
# 上传
git add .
git commit -m "<commit>"
git push

全局信任目录

# 添加信任全部目录
git config --global --add safe.directory '*'
# 查询所有配置
git config --global --get-all safe.directory
# 取消所有配置
git config --global --unset-all safe.directory

全局记住账密

git config --global credential.helper store

注意:很不安全,确保机器只有自己使用时可设置。

设置仓库用户名

git config user.name "<user-name>"
git config user.email "<user-email>"

修改上次提交用户名

git commit --amend --author="<user-name> <<user-email>>"
git push --force

注意:邮箱用 <> 包裹。

删除本地与远程分支

# 切换到其他分支
git checkout <other-branch>
# 删除本地分支
git branch -d <branch>
# 删除本地分支 (强制)
git branch -D <branch>
# 删除远程分支
git push origin --delete <branch>
# 清理已删除的远程分支
git fetch -p

修改远程仓库链接

主要想达到“将外部仓库的内容迁移到本地的新空仓库中”的效果。

# 查看当前仓库的设置
git config --list
# 查看当前仓库关联的远程仓库地址
git remote -v
# 修改远程仓库链接
git remote set-url origin <local-url>
# 合并, 没共同祖先也合并
git pull --no-rebase --allow-unrelated-histories

评论