学无先后,达者为师

网站首页 编程语言 正文

Git推拉存删push、pull、commit、reset的常用命令总结

作者:weixin_44953227 更新时间: 2022-04-09 编程语言

Git 常用命令汇总

更多命令:git --help

对某个命令文档(比如 push):git push --help

############### Git使用分支:###############
############### 首先添加远程仓库: git remote add origin URL ###############
查看分支:git branch

创建分支:git branch <分支名>

切换分支:git checkout <分支名> 或者 git switch <分支名>

创建+切换分支:git checkout -b <分支名> 或者 git switch -c <分支名>

合并某分支到当前分支:git merge <分支名>

删除分支:git branch -d <分支名>

克隆指定分支: git clone -b <分支名> origin

(pull/拉取)指定分支: git pull origin <分支名>

拉取远程分支到本地分支: git fetch origin <远程分支名>:<本地分支名>

拉取远程分支到本地分支同时切换分支: git checkout -b <本地分支名> origin/<远程分支名>

修改本地分支名: git branch -m <旧分支名> <新分支名>

删除远程分支: git push --delete origin <分支名>

将新分支名推送到远程仓库: git push origin <分支名>

将新分支设为上游分支:  git push --set-upstream origin <分支名>

将当前分支重置为dev分支: git reset --hard dev

将分支强制推送到远程分支: git push origin <分支名> --force

设置默认提交仓库和分支:  git push -u origin <分支名>

设置分支为默认分支:git branch --set-upstream-to=origin/<分支名> master



############### Git撤销上次commit提交:###############
1. 撤销上一次commit, 不会撤销<git add>:git reset --soft head~1

2. 撤销上一次commit, 同时撤销<git add>:git reset head~

3. 回退到上个版本:git reset --hard head~1



############### Git版本回退:###############
1. 回退到上个版本:git reset --hard HEAD^

2. 回退到3个版本:git reset --hard HEAD~3

3. 回退到指定版本:git reset --hard commit_id



############### Git tag打标签 (标签id通过 git log 查看):###############

补打标签:git tag -a v1.0 标签id

查看标签列表: git tag

标签重命名: git tag <新标签名> <旧标签名>

正常打标签:git tag -a <标签名> -m "说明"

删除本地标签:git tag -d <标签名>

推送标签到远程仓库:git push origin <标签名>

将本地所有标签推送到远程仓库:git push --tags

删除远程仓库标签: git push origin -d <标签名>



############### Git合并历史 -- (refusing to merge unrelated histories):###############
git pull origin master --allow-unrelated-histories



############### Git remote(修改远程仓库):###############
1.查看远程仓库: git remote -v

2.删除远程仓库: git remote rm origin(仓库名)

3.添加远程仓库: git remote add origin(仓库名) URL

4.修改远程仓库地址: git remote set-url origin(仓库名) URL



############### Git 账户信息操作:###############
Git生成SSH Key:ssh-keygen -t rsa -C "email@email.com"

查看当前账户信息:git config --list

设置当前账户:git config --global user.name "bryan sun"

设置当前邮箱:git config --global user.email "hitsjt@gmail.com"

移除当前账户: git config --global --unset user.name



############### Git 修改最后一次commit 的注释:###############
1. git commit --amend

2. 之后会进入图形编辑界面, 按 i 切换为编辑模式

3. 按 Esc 然后 wq 加回车保存



############### Git 修改最后某次提交的 commit 注释:###############
1. git rebase -i head~2

2. 想修改哪个备注, 就把那个备注的 pick 修改成 edit, 然后保存

3. 然后先 git commit --amend 修改备注, 最后一步 git rebase --continue

原文链接:https://blog.csdn.net/weixin_44953227/article/details/119480091

栏目分类
最近更新