1、新建本地git仓库,并关联到远程仓库
$ mkdir dubbo-demo
$ cd dubbo-demo
$ git init
// 在Github上创建名为dubbo-demo的repository
$ git remote add origin https://github.com/pattywgm/dubbo-demo.git
$ git add .
$ git commit -m "something"
// 将本地库推送到远程, 有可能需要先pull, -u参数使得本地master分支与远程master关联,以后推送时不用再输入-u参数
$ git push -u origin master
2、新建分支并推送到远程
$ git checkout -b develop
$ git push origin develop
注:
git push origin local_branch:remote_branch,local_branch必须为你本地存在的分支,remote_branch为远程分支,如果remote_branch不存在则会自动创建分支。
git push origin :remote_branch,local_branch留空的话则是删除远程remote_branch分支
3、删除分支
$ git branch -D develop // 删除本地分支
$ git push --delete origin develop // 删除远程分支
4、克隆远程分支到本地
git clone https://github.com/pattywgm/dubbo-demo.git -b master // -b 参数指定克隆哪个分支
5、查看分支
1)查看远程分支
$ git branch -r
origin/develop
origin/master
2)查看所有分支,包括本地分支和远程分支
$ git branch -a
develop
* master
remotes/origin/develop
remotes/origin/master
3)查看本地分支
$ git branch
develop
* master
注: 星号表示本地当前所在分支
6、重命名分支
$ git branch -m old-branch new-branch
7、打标签
$ git tag -a v1.1 -m "version 1.1"
$ git push origin v1.1
8、回退
本地仓库回退: git reset --hard HEAD~1
远程仓库回退: git push origin HEAD --force
9、遇到过的异常及解决方法
1)错误: fatal: remote origin already exists.说明远程关联已存在,现在想关联远程分支,关联不了,只能先删除之前的远程关联
=> git remote rm origin
2)$ git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
=>
在github上个人主页查看SSH AND GPG KEYS 发现SSH那边没有任何key,本地命令行运行
$ ssh -T git@github.com
Permission denied (publickey).
确认被拒绝访问,重新生成public key,如下操作:
$ ssh-agent -s
$ ssh-add -K ~/.ssh/id_rsa
$ vim ~/.ssh/id_rsa.pub // 拷贝public key, 在github上ssh那边加上去
$ ssh -T git@github.com
Hi pattywgm! You've successfully authenticated, but GitHub does not provide shell access.
3)错误:fatal: refusing to merge unrelated histories
=> git pull origin master --allow-unrelated-histories //允许未关联的历史