git 基本命令

基本命令

1.全局设置用户名
$ git config --global user.name "YourName"               
2.全局设置邮箱    
$ git config --global user.email "[email protected]"     
3.初始化仓库  
$ git init                                         
4.把文件添加到仓库
$ git add
5.添加所有文件到仓库
$ git add .
6.把所有文件提交到仓库
$ git commit -m "comment message"
7.推送分支
$ git push origin tagname
8.查看状态
$ git status
9.查看文件修改的内容
$ git diff filename
10.拉取更新
$ git pull
11.撤消指定的提交
$ git revert<commit>
12.查看远程仓库
$ git remote -v
13.查看远程服务器地址和仓库名称
$ git remote -v;
14.查看更新的内容
$ git diff
15.查看当前目录
$pwd
16.创建文件夹
$ mkdir name

版本控制

1.查看历史记录
$ git log
$ git reflog
2.回退版本
$ git reset --hard HEAD^
$ git reset --hard 3628164
3.丢弃工作区修改
$ git checkout -- filename
4.从版本库删除文件
$ git rm test.txt

远程仓库控制

1.关联远程苍库
$ git remote add origin git@server-name:path/repo-name.git
2.关联后第一次推送
$ git push -u origin master
3.克隆一个本地库
$ git clone git@server-name:path/repo-name.git

分支管理

1.查看分支
$ git branch
2.查看所有分支(包括远程分支)
$ git branch -a
3.切换分支
$ git checout branchname
4.创建分支
$ git branch name
5.创建+切换分支
$ git checkout -b name
6.合并到某分支
$ git merge name
7.删除分支
$ git branch -d name
8.删除远程分支
$ git push origin :delbranchname #origin后面一定要空一格
9.查看远程分支
$ git branch -r
10.获取所有远程分支 
$ git fetch
11.合并分支但不提交
$ git merge  –no-commit <some branch>

TAG标签

1.添加标签
$ git tag name
2.查看标签
$ git tag
3.推送标签
$ git push origin tagname
4.删除本地标签
$ git tag -d tagname

技巧

1.当手头工作没有完成时,先把工作现场git stash一下,然后去修复bug,修复后,再git stash pop,回到工作现场.
$ git stash
$ git stash pop

多人协作工作

多人协作的工作模式,一旦熟悉了,就非常简单。
查看远程库信息,使用git remote -v;
本地新建的分支如果不推送到远程,对其他人就是不可见的;
从本地推送分支,使用git push origin branch-name,如果推送失败,先用git pull抓取远程的新提交;
在本地创建和远程分支对应的分支,使用git checkout -b branch-name origin/branch-name,本地和远程分支的名称最好一致;
建立本地分支和远程分支的关联,使用git branch --set-upstream branch-name origin/branch-name;
从远程抓取分支,使用git pull,如果有冲突,要先处理冲突。

猜你喜欢

转载自482739566.iteye.com/blog/2259024