把本地仓库数据push到github远程仓库
1、⾸先为了防⽌每次都需要输⼊GitHub账号密码进⾏数据push,我们采⽤配置SSH的⽅式
先在本地初始化⼀个仓库
由于你的本地 Git 仓库和 GitHub 仓库之间的传输是通过SSH加密的,所以我们需要配置验证信息:
使⽤以下命令⽣成 SSH Key:(后⾯的 your_email@youremail 改为你在 Github 上注册的邮箱,之后会要求确认路径和输⼊密码,我们这使⽤默认的⼀路回车就⾏。)
ssh-keygen -t rsa -C "youremail@example"
成功的话会在C:\Users\(你登陆的⽤户名)\.ssh下会有⼀个id_rsa.pub的⽂件
.pub结尾那个为公钥,以⽂本⽅式打开它,复制全部内容,粘贴到你的GitHub上⾯
左边选择 SSH and GPG keys,然后点击 New SSH key 按钮,title 设置标题,可以随便填,粘贴在你电脑上⽣成的 key。添加成功之后显⽰:
验证是否连接成功
$ ssh -T git@github
The authenticity of host 'github (52.74.223.119)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no/[fingerprint])? yes                  # 输⼊ yes
Warning: Permanently added 'github,52.74.223.119' (RSA) to the list of known hosts.
Hi tianqixin! You've successfully authenticated, but GitHub does not provide shell access. # 成功信息
3、在github上创建⼀个仓库
4、创建成功后,显⽰如下信息
上⾯这张图要注意,SSH后⾯对应⼀个链接,HTTPS后⾯也对应⼀个链接。我们要使⽤SSH后⾯对应那个链接(也就是⽤git@github:tianqixin/runoob-git-test.git)
使⽤HTTPS那个你在使⽤github远程仓库的时候会弹出来⼀个框,让你登录GitHub
SSH也可以在这
5、把⼯作区的资源⽂件提交到暂存区
这⾥我们的⼯作区就是刚才你使⽤git init命令初始化之后那个⽬录
你先弄⼏个⽂件放在这个⽬录下
然后我们使⽤
git add ./*  #把当前⽬录下全部⽂件都放到暂存区
git add (具体⽂件名) #把某⽂件存放到暂存区中
之后需要提交commit,把暂存区⽂件放到版本库中
git commit -m "添加 README.md ⽂件"        # 提交并备注信息
远程连接到GitHub中的某库
git remote add origin git@github:tianqixin/runoob-git-test.git
这个origin是我们给这个远程连接的库起的⼀个名字,你使⽤git remote命令可以查看远程连接的所有库的在本地的名字,加上-v选项会显⽰更多信息
$ git remote
origin
$ git remote -v
origin    git@github:tianqixin/runoob-git-test.git (fetch)
origin    git@github:tianqixin/runoob-git-test.git (push)
然后把本地仓库的master提交到远程仓库(本地化名为origin)
git push -u origin master
6、git push出现的问题
有时候你⽤完git push之后会出现下⾯情况
$ git push -u origin master
Everything up-to-date
Branch 'master'set up to track remote branch 'master'from'origin'.
刚开始我以为没出现红字就不算错误,但是我push之后就和没有操作⼀样,远程仓库没变化,我就⼀直不到作哪⾥出错,把输出信息⼀搜才知道这就是报错了。。。
解决办法:
1、移除
git remote rm origin
2、再次连接
git remote add origin ‘仓库地址’
7、出现错误 failed to push some refs to ‘’
$ git push -u origin master
To github:qweqwe/Test.git
! [rejected]          master -> master (fetch first)
error: failed to push some refs to 'github:qweqwe/Test.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards'in'git push --help'for details.
解决:
本质原因:因为远程库与本地库不⼀致造成的,那么我们把远程库同步到本地库就可以了
⼀般问题:发⽣在本地库提交到远程库的时候。
⼀般原因:当我们在github版本库中发现⼀个问题后,你在github上对它进⾏了在线的修改;或者你直接在github上的某个库中添加readme⽂件或者其他什么⽂件,但是没有对本地库进⾏同步。这个时候当你再次有commit想要从本地库提交到远程的github库中时就会出现push失败的问题。
我的问题:本地新建⼀个⽂件夹,打开git bash,输出git init初始化⼀个本地库,然后git pull了远程库,修改后提交。
我的原因:远程库pull下来后,就是⼀个完整的库了,⽽我当初新建了个库,⽽且⽤这个本地库(与puul下来的不是⼀个),和远程库同步。
我的解决⽅法:进⼊pull下来的库的⽂件夹,打开git bash,并push,就可以了。
解决⽅案:这个问题是因为远程库与本地库不⼀致造成的,那么我们把远程库同步到本地库就可以了。
使⽤指令:git pull --rebase origin master
这条指令的意思是把远程库中的更新合并到本地库中,-rebase的作⽤是取消掉本地库中刚刚的commit,并把他们接到更新后的版本库之中。
git pull –rebase origin master意为先取消commit记录,并且把它们临时保存为补丁(patch)(这些补丁放到”.git/rebase”⽬录中),之后同步远程库到本地,最后合并补丁到本地库之中。
接下来就可以把本地库push到远程库当中了。
8、git push --rebase作⽤
git pull = git fetch + git merge FETCH_HEAD
git pull --rebase =  git fetch + git rebase FETCH_HEAD
那么来看看git rebase,  在master执⾏git rebase tmp,操作之后的分⽀如下:
⼆者对⽐可知,rebase没有产⽣新的节点,使⽤rebase的git演进路线(提交树)是⼀直向前的,这样在版本回退时也很容易,⽤merge的git路线是跳跃的,如果版本回退你也不到⾃⼰想要的版本,如果在merge时出现了冲突那就⿇烦了,当前merge就不能继续进⾏下去,需要⼿动修改冲突内容后,add,commit, push. ⽽rebase 操作的话,会中断rebase,同时会提⽰去解决冲突。解决冲突后, 再执⾏ git rebase –continue 继续操作,再git push.
8、git的⼀些操作命令
1)git show branch:file
解释:
Where branch can be any ref (branch, tag, HEAD, ...) and file is the full path of the file. To export it you could use
2)git show branch:file > exported_file
解释:
A simple, newbie friendly way for looking into a file: git gui browser <branch> which lets you explore the contents of any file.
It's also there in the File menu of git gui. Most other -more advanced- GUI wrappers (Qgit, Egit, etc..) offer
browsing/opening files as well.
9、git add permission denied
git add .
当执⾏这步操作的时候,发现有permission denied的提⽰,这⾥报错的可能性
1)⽂本作为只读性⽂本,需要修改⽂件夹属性
2)就是ide 编辑器占⽤,没有保存等可能相关的操作都有可能致使我们提交不上我们的代码
这时候就需要关闭ide 当前所使⽤的编辑器,然后重开就可以,再继续往下做git的操作(我的是这个原因)
10、git push -u origin master 中-u的作⽤
加了参数-u后,以后即可直接⽤git push 代替git push origin master
11、git push <;远程主机名> <;本地分⽀名>  <;远程分⽀名>
git push的⼀般形式为 git push <;远程主机名> <;本地分⽀名> <;远程分⽀名> ,例如 git push origin master:refs/for/master ,即是将本地的master分⽀推送到远程主机origin上的对应master分⽀, origin 是远程主机名,第⼀个master是本地分⽀名,第⼆个master是远程分⽀名。
11.1 git push origin master
如果远程分⽀被省略,如上则表⽰将本地分⽀推送到与之存在追踪关系的远程分⽀(通常两者同名),如果该远程分⽀不存在,则会被新建
11.2 git push origin :refs/for/master
  如果省略本地分⽀名,则表⽰删除指定的远程分⽀,因为这等同于推送⼀个空的本地分⽀到远程分⽀,等同于 git push origin --delete master
git设置用户名和邮箱11.3 git push origin
  如果当前分⽀与远程分⽀存在追踪关系,则本地分⽀和远程分⽀都可以省略,将当前分⽀推送到origin主机的对应分⽀11.4 git push
  如果当前分⽀只有⼀个远程分⽀,那么主机名都可以省略,形如 git push,可以使⽤git branch -r ,查看远程的分⽀名11.5 git push 的其他命令
  这⼏个常见的⽤法已⾜以满⾜我们⽇常开发的使⽤了,还有⼏个扩展的⽤法,如下:
    (1) git push -u origin master 如果当前分⽀与多个主机存在追踪关系,则可以使⽤ -u 参数指定⼀个默认主机,这样后⾯就可以不加任何参数使⽤git push,
      不带任何参数的git push,默认只推送当前分⽀,这叫做simple⽅式,还有⼀种matching⽅式,会推送所有有对应的远程分⽀的本地分⽀, Git 2.0之前默认使⽤matching,现在改为simple⽅式
      如果想更改设置,可以使⽤git config命令。git config --global push.default matching OR git config --global push.default simple;可以使⽤git config -l 查看配置
    (2) git push --all origin 当遇到这种情况就是不管是否存在对应的远程分⽀,将本地的所有分⽀都推送到远程主机,这时需要 -all 选项
    (3) git push --force origin git push的时候需要本地先git pull更新到跟服务器版本⼀致,如果本地版本库⽐远程服务器上的低,那么⼀般会提⽰你git pull更新,如果⼀定要提交,那么可以使⽤这个命令。
    (4) git push origin --tags //git push 的时候不会推送分⽀,如果⼀定要推送标签的话那么可以使⽤这个命令
11.6 关于 refs/for
  // refs/for 的意义在于我们提交代码到服务器之后是需要经过code review 之后才能进⾏merge的,⽽refs/heads 不需要