常用命令

git init
git add //添加到git(暂存区)准备提交
git add -A
git add -all //把工作区所有修改添加到暂存区
git commit -m “描述内容” //默认提交到主分支
git pull //从远程抓取分支

远程仓库

git remote //查看远程库信息
git remote -v //查看更详细的远程库信息
git remote add origin
//关联一个远程仓库
git push -u origin master //第一次提交带上-u 用于把本地以前的commit推送到关联的仓库
git push origin master
git remote rm origin //移除关联
git clone //克隆仓库

分支管理

git branch //查看分支
git branch //创建分支
git branch -D //强行删除
git checkout //切换分支
git checkout -b //创建+切换分支
git checkout -b origin/ //在本地创建和远程分支对应的分支
git branch --set-upstream origin/
git merge //合并分支到当前分支
git branch -d //删除分支
git log --graph //查看分支合并图
git stash //暂存工作区
git stash apply //恢复工作区(不删除暂存)
git stash drop //删除工作区暂存
git stash pop //恢复工作区(删除暂存)
git stash list //查看工作区暂存
origin是自己命名的远程仓库源 名字

If you added this path by mistake, you can remove it from the
hint: index with:
hint:
hint: git rm --cached sa18
hint:

通常clone一个仓库会生成 两级 .git

此时第一级是clone 时 git bash的位置 下一次git pull 拉取更新或者push等操作 都要直接在第二级 .git中操作 千万不要在第一级 否则 pull时都是在第一级位置开始生成文件

git pull 冲突

error: Your local changes to the following files would be overwritten by merge:
xxx/xxx/xxx.java
Please, commit your changes or stash them before you can merge.
Aborting

  1. 可以commit时commit
  2. 不可以commit时需要stash
git stash: 备份当前的工作区的内容,从最近的一次提交中读取相关内容,让工作区保证和上次提交的内容一致。同时,将当前的工作区内容保存到Git栈中。
git pull
git stash pop: 从Git栈中读取最近一次保存的内容,恢复工作区的相关内容。由于可能存在多个Stash的内容,所以用栈来管理,pop会从最近的一个stash中读取内容并恢复。
git stash list: 显示Git栈内的所有备份,可以利用这个列表来决定从那个地方恢复。
git stash clear: 清空Git栈。此时使用gitg等图形化工具会发现,原来stash的哪些节点都消失了。

总的来说,git stash命令的作用就是将目前还不想提交的但是已经修改的内容进行保存至堆栈中,后续可以在某个分支上恢复出堆栈中的内容。

  • 这也就是说,stash中的内容不仅仅可以恢复到原先开发的分支,也可以恢复到其他任意指定的分支上。
  • git stash作用的范围包括工作区和暂存区中的内容,也就是说没有提交的内容都会保存至堆栈中。

所以一般通过 git stash将本地本分支的内容暂时保存在栈中,然后 Git pull拉取远端仓库中的内容,此时就不会有冲突,然后通过diff比对stash中的内容与最近拉取的内容进行更改。

方便的配套工具:vscode