git常用命令

1.基本知识点

Git本地有四个工作区域:工作目录(Working Directory)、暂存区(Stage/Index)、资源库(Repository或Git Directory)、git仓库(Remote Directory)。文件在这四个区域之间的转换关系如下:在这里插入图片描述

2.常用命令:

第一步:git pull ssh/网址  --把代码拉到本地
    一顿操作(修改代码)............
第二步:创建自己的分支并同步到远程
git checkout -b 分支名
git branch --set-upstream-to=origin/master --同步到远程仓库
第三步:
git add .
git commit -m  "提交说明"
git push origin HEAD



其他一些常用的操作:
git status  --查看状态:在那个分支上面
git checkout master/分支  --切换到master/其他分支
git diff:查看branch和master的差异,修改了那些内容
git log :查看操作日志
git fetch <remote>:访问远程仓库,从中拉取所有你还没有的数据


3.工作遇到的问题

Case1: 当前分支落后于master分支

Before preceding, you have to commit or stash all the changes you made on the branch behind commits.

Solution:

  1. Checkout your local Master branch
git checkout master
  1. Pull from remote Master branch
git pull origin master

Now your local Master is in sync with the remote branch. As a result of the above command, other local branches branched from the previous local Master branch are not in sync. To fix that:

Checkout the branch that is behind your local Master branch

git checkout BranchNameBehindCommit

Merge with the local Master branch

git merge master  // Now your branch is in sync with the local Master branch

If this branch is on the remote repository, you have to push your changes.

git push origin branchBehindCommit
Case2:合并多个commits

rebase合并远程多个commits

Case3:当前分支落后于master分支,n个 commits ahead

结合case1+case2

4.撤销已经push的文件

第一步: git log --pretty=oneline   查看当前提交的日志
第二步: git reset --soft XXX   XXX是commitID(d6cdbba417…) 回退当前工作空间的上一个版本,并且保留代码更改
第三步: git log --pretty=oneline   再次查看当前提交的日志,确认是否成功撤销,当然,你也可以不看,基本上都会成功,保险一下,看看呗
第四步: git push origin 远程分支 --force   强制提交当前版本号,以达到撤销版本号的目的.必须添加参数force进行强制提交,否则会提交失败,报错原因:本地项目版本号低于远端仓库版本号。(master 代表分支名称,默认是 master,或者也可以直接用git push --force)

注意:这种强制提交的方法只有owner的时候可以用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值