下面是一个「常用命令速查表」,作为git使用的备忘录
设置用户名
git config --global user.name "jameskerry651"
设置邮箱
git config --global user.email "[email protected]"
查看当前配置
git config --list
git init
在当前文件夹创建一个新的 Git 仓库(生成 .git 目录)。
git clone <repo-url>
例如: git clone https://github.com/user/project.git
从远程拉一份完整的仓库到本地。
git status
查看哪些文件被修改、哪些已暂存、当前所在分支等。
git diff # 查看工作区相对暂存区的改动 git diff --staged # 查看暂存区相对最近一次提交的改动
git add <file> # 添加指定文件 git add . # 添加当前目录下所有改动
git commit -m "描述本次修改"
写清楚提交说明,有助于以后回看历史。
git log git log --oneline # 一行一个提交,简洁模式 git log --graph --oneline --all # 图形化查看分支
git branch # 列出本地分支 git branch <name> # 创建分支 git checkout <name> # 切换分支(老用法) git switch <name> # 切换分支(推荐,较新) git switch -c <name> # 创建并切换到新分支
git merge feature
如果有冲突,需要手动解决后再:
git add <conflict-files> git commit
git branch -d <name> # 删除本地分支(已合并) git branch -D <name> # 强制删除本地分支
git remote -v # 查看远程仓库列表 git remote add origin <url> # 添加名为 origin 的远程
git fetch # 只拉取远程更新,不合并 git pull # 等于 fetch + merge(把远程分支合并到当前分支)
指定远程和分支:
git pull origin main
git push # 已设置 upstream 时可直接用 git push origin main # 将本地 main 推到远程 origin/main
首次推送新分支:
git push -u origin <branch>
-u 会设置默认上游分支,后面就可以直接 git push。
git stash # 把当前未提交的修改暂存起来 git stash list # 查看所有 stash git stash pop # 取出最近一次 stash 并应用 git stash drop # 删除最近一次 stash
撤销工作区的改动(回到暂存区或最近提交):
git restore <file> # 恢复文件(新命令) git checkout -- <file> # 老命令,作用类似
撤销已经暂存的改动:
git restore --staged <file>
移动当前 HEAD 到某个提交(谨慎):
git reset --soft <commit> # 回退到某提交,但保留改动在暂存区 git reset --mixed <commit> # 默认,改动回到工作区 git reset --hard <commit> # 全都丢弃到该提交状态(慎用!)
git tag v1.0.0 # 在当前提交打轻量标签 git tag -a v1.0.0 -m "说明" # 带注释标签 git push origin v1.0.0 # 推送指定标签 git push origin --tags # 推送所有标签
git fetch --all
注意:将 <branch_name> 替换为你的分支名,如 master 或 main
git reset --hard origin/main
本文作者:James
本文链接:
版权声明:本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!