git学习总结
很早之前在廖雪峰老师的网站上学习的git,参见链接Git教程。总结一下常见的git操作。
git的四个分区
- workspace(工作区):我们平时写代码,对代码的直接修改就是在这个工作区中。
- index/stage(暂存区):修改代码之后,需要对代码进行保存,就保存在这个暂存区。
- respository(本地仓库,版本库):代码保存在暂存区之后,可以再commit到本地仓库。
- remote(远程仓库):本地仓库可以和远程仓库同步。
这四个分区之间的关系如下:
一.新建代码
git init
git init [project_name]
git clone [url]
二.配置
git config --list //显示当前配置
git config --global user.name ["name]"
git config --global user.email ["email_address"] //配置用户名和邮箱
三.添加删除文件
git add [file1 file2...] //添加file1,file2等到暂存区
git add [dir] //添加dir文件夹到暂存区
git add . //添加所有文件,等价于git add --all
git rm [file1 file2...] //删除file1,file2等,并添加到暂存区
rm [file1 file2...] //删除file1,file2等,仅仅是工作区的操作
git mv [file-original] [file-renamed] //文件改名放入暂存区
四.代码拉取
git commit -m ["message"] //提交暂存区到本地仓库,message是提示信息
git commit [file1 file2...] -m ["message"]
git commit --amend //追加一次提交,commit_id不变
git commit --amend -m ["message"] //使用一次新的提交代替最近一次
五.分支
git branch //列出所有本地分支
git branch -r //列出所有远程分支
git branch -a //列出所有本地和远程分支
git branch [branch_name] //新建一个分支
git checkout -b [branch_name] //新建一个分支并切换到该分支
git checkout [branch_name] //切换到指定分支
git merge [branch_name] //合并分支branch_name到当前分支
git branch -d [branch_name] //删除分支branch_name
git push [remote] --delete [branch-name] //删除远程分支
六.标签
git tag //列出所有标签
git tag [tag_name] //新建tag到当前commit
git tag [tag_name] [commit_id]
git tag -d [tag_name] //删除本地tag
git push [remote] --tags
git checkout -b [branch_name] [tag_name]
七.查看信息
git status //显示变更文件
git log //显示当前分支版本历史
git log -[count] --pretty --oneline //过去count次提交
git diff //暂存区和工作区的差别
git reflog //显示当前分支最近几次提交
git show [commit_id] //
八.远程同步
git fetch [remote] //下载远程仓库的所有变动,[remote]常是origin
git remote -v //显示所有远程仓库
git remote show [remote] //显示远程库信息
git pull [remote] [branch] //取回远程仓库变化,合并到本地分支
git push [remote] [branch] //推送本地分支到远程仓库例如git push origin master
git push [remote] [branch] --force
九.撤销
git checkout [file] //恢复暂存区文件到工作区(撤销工作区)
git checkout .
git reset [file] //重置暂存区指定文件和上一次commit一致(撤销暂存区)
git reset --hard [commit_id] //撤销本地仓库