git 常用命令

config

csharp 复制代码
git config --global user.name "name"
git config --global user.email "email"
# 查看
git config --global --list

init

csharp 复制代码
git init

clone

bash 复制代码
git clone <repository-url>

add

csharp 复制代码
git add <file-name>
# 添加所有更改文件
git add .

commit

sql 复制代码
git commit -m "message"
# 合并
git rebase -i
git merge --squash

stash

perl 复制代码
git stash -m "message"
# 查看
git stash list
# 应用
git stash apply
# 应用并删除最新一个
git stash pop
# 删除最新一个
git stash drop
# 删除指定暂存的记录  
$ git stash drop <stash@{0}>
# 清空
git stash clear

查看

bash 复制代码
# 状态
git status
# 日志
git log
git show

branch

perl 复制代码
# 创建
git branch <branch-name>
# 切换
git checkout <branch-name>
# 创建并切换
git checkout -b <branch-name>
# 删除,-D 强制删除
git branch -d <branch-name>
# 删除远端
git push origin --delete <branch-name>

合并分支

命令 合并后的历史记录分支 解决冲突 是否产生新 commit
git merge 保留两条并行的线 会自动解决一些冲突
git rebase 变基 合并为一条线,更整洁 需要自己处理所有冲突

rebase 建议用于对自己 commit 的操作,避免修改别人的记录

相关命令

sql 复制代码
# merge 合并到当前分支
git merge <source-branch>
# 合并冲突时,可以执行这个命令放弃本次 merge
git merge --abort
# 合并 commit,后续还有复杂的操作
git merge --squash

# rebase 将当前分支的所有提交移动到另一个分支的顶部
git rebase <target-branch>
# 解决冲突并继续变基
git add <resolved-file>
git rebase --continue
# 跳过当前冲突的提交并继续变基
git rebase --skip
# 中断或取消变基
git rebase --abort
# 合并 commit,后续还有复杂的操作
git rebase -i

将特定的提交应用到当前分支

python 复制代码
git cherry-pick <commit-hash>

添加远程仓库

csharp 复制代码
git remote add origin <repository-url>

拉代码

bash 复制代码
# 不自动合并
git fetch
# 自动合并
git pull

push

perl 复制代码
git push
# 强制推送到远端,可能会覆盖或删除一些 commit
git push -f
# 推送本地分支到远程仓库并绑定
git push --set-upstream origin <your-local-branch>
git push -u origin <your-local-branch>

回滚

python 复制代码
# 删除已有的 commit
git reset --soft <commit-hash>
git reset --hard <commit-hash>
# 保留已有的 commit
git revert <commit-hash>

从本地仓库中删除所有不再被任何远程跟踪分支引用的远程跟踪分支

sql 复制代码
git fetch --prune origin
相关推荐
承渊政道2 小时前
Linux系统学习【Linux系统的进度条实现、版本控制器git和调试器gdb介绍】
linux·开发语言·笔记·git·学习·gitee
Doro再努力2 小时前
【Linux操作系统12】Git版本控制与GDB调试:从入门到实践
linux·运维·服务器·git·vim
摇滚侠4 小时前
MAC IDEA GIT 提交区显示了几个不存在的目录
git·idea
城东5 小时前
Git使用[远程仓库远端的head比本地和提交的head旧,其他人拉不到最新代码]
git·head·远程仓库远端·比本地和提交的head旧·其他人拉不到最新代码
何中应14 小时前
使用SSH地址拉取远程仓库代码报下面的错误
git
何中应14 小时前
Git本地仓库命令补充
git
sun00770016 小时前
执行repo sync -c -d -j4以后,提交未git push的代码看不到了。要怎么恢复?
git
胖虎118 小时前
Git 一个本地仓库同时推送到两个远程仓库(详细教程)
git·多远程仓库·双远程仓库·git双远程·git备份
春日见1 天前
如何创建一个PR
运维·开发语言·windows·git·docker·容器
stevenzqzq2 天前
git 常用操作
大数据·git