整理自 Acer Predator 开发实战,涵盖从基础到深度填坑的所有指令。
git init
git clone git@github.com:用户名/仓库名.git
git clone https://github.com/用户名/仓库名.git
git clone -b 分支名 git@github.com:用户名/仓库名.git
git clone -b 分支名 https://github.com/用户名/仓库名.git
git status
git add .
git add 文件名
git commit -m "feat: 描述你的改动"
git push -u origin main
main 分支会与远程 origin/main 建立关联。git push 和 git pull,无需再指定远程和分支名。
git push origin main
origin 和分支名 main。-u 设置过上游分支,可以简化为 git push。
| 指令 | 使用场景 | 效果 |
|---|---|---|
git push -u origin main |
首次推送 / 新建分支首次推送 | 推送 + 设置上游跟踪,之后可简写为 git push |
git push origin main |
日常推送(未设置上游时) | 仅推送,每次都需写完整命令 |
git push |
日常推送(已设置上游后) | 自动推送到已关联的上游分支 |
git pull origin main
git pull。
git branch
git branch -a
git branch 分支名
git checkout 分支名
git checkout -b 分支名
git merge 分支名
git branch -d 分支名
git push origin --delete 分支名
git push -u origin 分支名
git remote -v
git remote add origin git@github.com:用户名/仓库名.git
git remote add origin https://github.com/用户名/仓库名.git
git remote set-url origin git@github.com:用户名/新仓库名.git
git remote set-url origin https://github.com/用户名/新仓库名.git
git fetch origin
git remote remove origin
git branch -vv
-u 是否设置成功。
git log
git log --oneline
git log --oneline --graph --all
git diff
git diff --staged
git checkout -- 文件名
git reset HEAD 文件名
git reset --soft HEAD^
git reset --hard 提交ID
--amend 参数,允许你修正最后一次提交的内容或说明。如果你刚刚执行完 git commit,还没执行 git push,这是最简单的修正方式。
git commit --amend -m "这是修正后正确的描述内容"
如果你已经执行了 git push,GitHub 网页上已经显示了错误的描述,修正起来会稍微复杂一些,因为这涉及到"修改历史"。
git commit --amend -m "修正后的正确描述"
git push 会报错,必须使用"强力推送"
git push --force origin main
--force 会覆盖远程仓库的历史。如果你在公司或团队合作开发,千万不要对公共分支使用强制推送,否则会把别人的代码弄丢!| 情况 | 操作指令 | 风险等级 |
|---|---|---|
| 未推送到远程 | git commit --amend -m "新描述" |
安全 |
| 已推送到远程 | git commit --amend + git push --force |
危险 会覆盖远程历史 |
git stash
git stash list
git stash pop
git stash apply stash@{0}
git branch -M main
git config --global core.autocrlf true
git push -f origin main
-f 是 --force 的简写。仅在个人项目或确认安全时使用!
git config --global user.name "你的名字"
git config --global user.email "你的邮箱"
git config --list
git config core.fileMode false