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:
- Checkout your local Master branch
git checkout master
- 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
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的时候可以用