git远程仓库-github

提交本地库到github

第1步:创建SSH Key。在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。如果没有,打开Shell(Windows下打开Git Bash),创建SSH Key:

$ ssh-keygen -t rsa -C “[email protected]

2步:登陆GitHub,打开“Account settings”,“SSH Keys”页面:

然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容:

首先,登陆GitHub,然后,在右上角找到“Create a new repo”按钮,创建一个新的仓库:

    $ git remote add origin [email protected]:michaelliao/learngit.git

    $ git push -u origin master
Counting objects: 19, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (19/19), done.
Writing objects: 100% (19/19), 13.73 KiB, done.
Total 23 (delta 6), reused 0 (delta 0)
To [email protected]:michaelliao/learngit.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

从现在起,只要本地作了提交,就可以通过命令:

$ git push origin master

从远程克隆

现在git新建respository,复制
git链接,
在本地执行
$ git clone [email protected]:michaelliao/gitskills.git

在某些只开放http端口的地方,可以使用https://的地址
不过要每次输入口令

分支

  • 查看分支 git branch
  • 创建分支 git branch
  • 切换分支 git checkout
  • 创建+切换分支 git checkout -b
  • 合并某分支到当前分支 git merge
  • 删除分支 git checkout -d
  • git checkout 汇总显示工作区、暂存区与HEAD的差异。

解决冲突

冲突情况

$ git merge feature1
Auto-merging readme.txt
CONFLICT (content): Merge conflict in readme.txt
Automatic merge failed; fix conflicts and then commit the result.

使用 ++git status++ 可以查看冲突文件
可以直接cat命令看冲突显示
有<<<<<<========>>>>>>>>>
使用git log可以查看分支的合并情况

    xn@xn-pc:~/learngit$ git log --graph --pretty=oneline --abbrev-commit 
*   99561a9 conflict fixed
|\  
| * 3126eb3 AND simple
* | 4ef1670 & simple
|/  
* a9d63b6 branch test
* 1795bee understand how stage works
* fcfed65 append GPL
* fd6f9a8 add distributed
* 6892b28 wrote a readme file

重新add commit 可以提交成功。
最后删除冲突的分支

合并其他分支时禁用fast forward

本次合并要创建一个新的commit,所以加上-m参数,把commit描述写进去。

准备合并dev分支,请注意–no-ff参数,表示禁用Fast forward:

$ git merge –no-ff -m “merge with no-ff” dev
Merge made by the ‘recursive’ strategy.
readme.txt | 1 +
1 file changed, 1 insertion(+)

合并后,我们用git log看看分支历史:

$ git log –graph –pretty=oneline –abbrev-commit
* 7825a50 merge with no-ff
|\
| * 6224937 add merge
|/
* 59bc1cb conflict fixed

保存当前工作现场

修复bug时,我们会通过创建新的bug分支进行修复,然后合并,最后删除;

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

$ git stash list
stash@{0}: WIP on dev: 6224937 add merge
一是用git stash apply恢复,但是恢复后,stash内容并不删除,你需要用git stash drop来删除;

另一种方式是用git stash pop,恢复的同时把stash内容也删了。
你可以多次stash,恢复的时候,先用git stash list查看,然后恢复指定的stash,用命令:
$ git stash apply stash@{0}

强制删除分支

如果要丢弃一个没有被合并过的分支,可以通过git branch -D 强行删除。

多人协作

多人协作的工作模式通常是这样:

首先,可以试图用git push origin branch-name推送自己的修改;

如果推送失败,则因为远程分支比你的本地更新,需要先用git pull试图合并;

如果合并有冲突,则解决冲突,并在本地提交;

没有冲突或者解决掉冲突后,再用git push origin branch-name推送就能成!
如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,用命令git branch --set-upstream branch-name origin/branch-name。

操作远程库

查看远程库信息,使用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,如果有冲突,要先处理冲突。

标签

命令git tag <name>用于新建一个标签,默认为HEAD,也可以指定一个commit id;
git tag v0.9 xxxidx

git tag -a <tagname> -m "blablabla..."可以指定标签信息;

git tag -s <tagname> -m "blablabla..."可以用PGP签名标签;
用命令git show <tagname>可以看到PGP签名信息
创建使用指定的key签名的标签,需要使用 -u 参数来指定key:git tag -u "rangu.dl" -s v0.2 -m "signed version 0.2 released" de4a01d
命令git tag可以查看所有标签。

*删除和向github推送标签*

命令git push origin <tagname>可以推送一个本地标签;

命令git push origin --tags可以推送全部未推送过的本地标签;

命令git tag -d <tagname>可以删除一个本地标签;

命令git push origin :refs/tags/<tagname>可以删除一个远程标签。

如何参与一个开源项目呢?比如人气极高的bootstrap项目,这是一个非常强大的CSS框架,你可以访问它的项目主页https://github.com/twbs/bootstrap,点“Fork”就在自己的账号下克隆了一个bootstrap仓库,然后,从自己的账号下clone:

git clone [email protected]:michaelliao/bootstrap.git

一定要从自己的账号下clone仓库,这样你才能推送修改。如果从bootstrap的作者的仓库地址[email protected]:twbs/bootstrap.git克隆,因为没有权限,你将不能推送修改。
如果你想修复bootstrap的一个bug,或者新增一个功能,立刻就可以开始干活,干完后,往自己的仓库推送。

如果你希望bootstrap的官方库能接受你的修改,你就可以在GitHub上发起一个pull request。当然,对方是否接受你的pull request就不一定了。

git remote -v 查看远程库信息
git remote rm origin 删除远程库
git remote add github [email protected]:michaelliao/learngit.git
git remote add gitee [email protected]:liaoxuefeng/learngit.git

查询
git remote -v
gitee [email protected]:liaoxuefeng/learngit.git (fetch)
gitee [email protected]:liaoxuefeng/learngit.git (push)
github [email protected]:michaelliao/learngit.git (fetch)
github [email protected]:michaelliao/learngit.git (push)
如果要推送到GitHub,使用命令:

git push github master

如果要推送到码云,使用命令:

git push gitee master

忽略某些文件

所有配置文件可以直接在线浏览:https://github.com/github/gitignore

忽略文件的原则是:

忽略操作系统自动生成的文件,比如缩略图等;
忽略编译生成的中间文件、可执行文件等,也就是如果一个文件是通过另一个文件自动生成的,那自动生成的文件就没必要放进版本库,比如Java编译产生的.class文件;
忽略你自己的带有敏感信息的配置文件,比如存放口令的配置文件。

忽略某些文件时,需要编写.gitignore;
.gitignore文件本身要放到版本库里,并且可以对.gitignore做版本管理!
- 强制提交
gitaddfApp.class git check-ignore -v App.class

配置别名

$ git config –global alias.st status
–global参数是全局参数,也就是这些命令在这台电脑的所有Git仓库下都有用。

很多人都用co表示checkout,ci表示commit,br表示branch:

$ git config --global alias.co checkout
$ git config --global alias.ci commit
$ git config --global alias.br branch
命令git reset HEAD file可以把暂存区的修改撤销掉(unstage),重新放回工作区。既然是一个unstage操作,就可以配置一个unstage别名:

$ git config --global alias.unstage 'reset HEAD'
配置一个git last,让其显示最后一次提交信息:

$ git config --global alias.last 'log -1'
甚至还有人丧心病狂地把lg配置成了:

git config --global alias.lg "log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit"

配置Git的时候,加上–global是针对当前用户起作用的,如果不加,那只针对当前的仓库起作用。

配置文件放哪了?每个仓库的Git配置文件都放在.git/config文件中
而当前用户的Git配置文件放在用户主目录下的一个隐藏文件.gitconfig中

猜你喜欢

转载自blog.csdn.net/xxxxxx______xxxxxx/article/details/78162136