记录一些git常用操作,会保持更新。如有错误请指出。
分支操作
新建本地和远程分支并上传
- 新建本地分支"new_local_branch"
git checkout -b new_local_branch
- 更新分支程序,并将更新部分加入缓存区,并写commit
git add
git commit -m "XXXX"
- 新建远程分支“new_remote_branch",并将本地分支提交到远程分支。
git push origin new_local_branch:new_remote_branch
origin是远程主机名,后面先是之前新建的本地分支名,后跟一个:,最后是新的远程分支名。
这一步有简略写法。
git push -u origin new_branch
-u 是–set-upstream的缩写形式,origin是远程主机名, new_branch为新建的远程分支,这里新建的远程分支名需要与本地分支名相同。
下载远程分支到新的本地分支
- 下载远程分支remote_branch到新建的本地分支new_local_branch下
git checkout -b new_local_branch origin/remote_branch
重命名本地分支与远程分支
- 重命名本地分支
git branch -m new_branch_name
- 重命名远程分支。
先删除用旧名的远程分支old_remote_branch
git push origin -d old_remote_branch
再将新的本地分支提交到新的远程分支
git push origin -u new_branch_name
- 查询本地分支指向的远程分支
git branch -vv
删除本地分支和远程分支
- 删除本地分支local_branch
git branch -d local_branch
- 删除远程分支remote_branch
git push origin -d remote_branch
缓存区操作
清空add的缓存区
- 清空所有缓存区内容
git reset --mixed
–mixed 指清空stage/cache/index区域
- 删除add进缓存区的文件new_file
git reset new_file
- 可以使用git rm 删除文件,但要注意
git rm --cached new_file
可以去除add进缓存区的文件new_file,并且不删除物理文件.
而
git rm -f new_file
则是既删除缓存区new_file,也会将new_file的物理文件删除