使用Git Bash push项目到github

杂记 2017-08-17

配置git:
打开准备建立仓库的文件夹如:
cd GithubBackUp

然后配置用户名和邮箱:
git config --global user.name "myusername"

git config --global user.email "myemail@email.com"

查看配置信息:
git config user.name
输出信息:
myusername
只后就会已这个用户名进行版本控制。

接下来创建仓库:
git init

创建一个文件,准备上传
touch 1.txt


  1. 首先在Github上创建一个新的项目(空项目,只有一个.gitignore文件)

  2. 在本地创建一个名字相同的项目。

  3. 接下来的一步非常重要, 我们需要将远程版本库克隆到本地。首先必须知道远程版本库的Git地址, 点击Clone or download,复制地址(如:https://github.com/gowufang/AndroidFramewrok.git)

  4. 打开Git Bash并切换到本地相应的工程目录下:
    如:
    cd D:
    cd 0MSE1/AndroidFramework

接着输入git clone https://github.com/gowufang/AndroidFramewrok.git 来把远程版本库克隆到本地

$ git clone https://github.com/gowufang/AndroidFramewrok.git
Cloning into 'AndroidFramewrok'...
remote: Counting objects: 312, done.
Receiving objecremote: Total 312 (delta 0), reused 0 (delta 0), pack-reused 312
Receiving objects: 100% (312/312), 496.77 KiB | 61.00 KiB/s, done.
Resolving deltas: 100% (126/126), done.

现在我们需要将克隆下来的目录中的所有文件全部复制粘贴到上一层目录中,
这样就能将整个CoolWeather工程目录添加到版本控制中去了。 注意.git
是一个隐藏目录
, 在复制的时候千万不要漏掉。 另外, 上一层目录中也
有一个.gitignore文件, 我们直接将其覆盖即可。 复制完之后可以把
AndroidFramework目录删除掉。

将所有文件添加到版本控制中

Administrator@PC201504081924 MINGW64 /d/0MSE1/AndroidFramework (master)
$ git add .

然后在本地执行提交操作:

Administrator@PC201504081924 MINGW64 /d/0MSE1/AndroidFramework (master)
$ git commit -m "First commit."
[master 250e875] First commit.
 1 file changed, 1 insertion(+)
 create mode 160000 coolweather

将提交的内容同步到远程版本库, 也就是GitHub上面:


Administrator@PC201504081924 MINGW64 /d/0MSE1/AndroidFramework (master)
$ git push origin master
To https://github.com/gowufang/AndroidFramewrok.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/gowufang/AndroidFramewrok.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

正常的话应该输入用户名和密码就能push了,但上面遇到了个尴尬的问题,网上找到的解决办法

http://jingyan.baidu.com/article/f3e34a12a25bc8f5ea65354a.html

Administrator@PC201504081924 MINGW64 /d/0MSE1/AndroidFramework (master)
$ git pull --rebase origin master
From https://github.com/gowufang/AndroidFramewrok
 * branch            master     -> FETCH_HEAD
First, rewinding head to replay your work on top of it...
warning: unable to rmdir coolweather: Directory not empty
Applying: my test push
Applying: my test push2
Applying: my test push3
Applying: First commit.

再次提交:

Administrator@PC201504081924 MINGW64 /d/0MSE1/AndroidFramework (master)
$ git push -u origin master
fatal: TaskCanceledException encountered.
   A task was canceled.
Username for 'https://github.com': gowufang
Counting objects: 83, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (51/51), done.
Writing objects: 100% (83/83), 87.61 KiB | 0 bytes/s, done.
Total 83 (delta 5), reused 0 (delta 0)
remote: Resolving deltas: 100% (5/5), done.
To https://github.com/gowufang/AndroidFramewrok.git
   37c1f0e..d15ab89  master -> master
Branch master set up to track remote branch master from origin.

在一台新电脑上使用git时,出现ssh警告:

wu@DESKTOP-94T1GA1 MINGW64 /d/0Mse1/1114learngit (master)
$ git push -u origin master
The authenticity of host 'github.com (192.30.255.112)' can't be established.
RSA key fingerprint is SHA256:nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'github.com,192.30.255.112' (RSA) to the list of known hosts.
git@github.com: Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

解决办法:

1,将连接方式从http更换为ssh。注意,github.com后面一定有(冒号):

git remote rm origin
git remote add origingit@github.com:username/respository.git

2,生成新的ssh key。这里会提示要不要rewrite,键入y,还有提示输入passPhrase,输入空格即可。

cd ~/.ssh
ssh-keygen
如下提示:

wu@DESKTOP-94T1GA1 MINGW64 ~/.ssh
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/wu/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Passphrases do not match.  Try again.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/wu/.ssh/id_rsa.
Your public key has been saved in /c/Users/wu/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:AHJ31GAS2n1VheQ82wmRBmzDGmbCxy5MzMXXAHaLemk wu@DESKTOP-94T1GA1
The key's randomart image is:
+---[RSA 2048]----+
|  . o BoO*++=++o.|
|   o = @.B+B.*o  |
|    . = B.*.o.+  |
|       +.+.   .+.|
|       .SE    ...|
|        o        |
|                 |
|                 |
|                 |
+----[SHA256]-----+

3,测试一下连接。
ssh -T -v git@github.com
发现最后几行出现:

debug1: Authentications that can continue: publickey
debug1: Trying private key: /c/Users/wu/.ssh/id_dsa
debug1: Trying private key: /c/Users/wu/.ssh/id_ecdsa
debug1: Trying private key: /c/Users/wu/.ssh/id_ed25519
debug1: No more authentication methods to try.
git@github.com: Permission denied (publickey).

解决方法:
1.打开yourpath/.ssh/id_rsa.pub(.pub为公钥文件,id_rsa为私钥文件),复制里面的内容。
2.打开https://github.com/settings/profile,在SSH key那栏选择New SSH Key,并将复制内容拷贝到其中,选择保存。

3.再次测试$ ssh -T git@github.com,提示

debug1: channel 0: new [client-session]
debug1: Entering interactive session.
debug1: pledge: network
debug1: client_input_channel_req: channel 0 rtype exit-status reply 0
Hi gowufang! You've successfully authenticated, but GitHub does not provide shell access.
debug1: channel 0: free: client-session, nchannels 1
Transferred: sent 2560, received 1856 bytes, in 0.4 seconds
Bytes per second: sent 6085.7, received 4412.1
debug1: Exit status 1

$ git push -u origin master时候出现:

To github.com:gowufang/learngit.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'git@github.com:gowufang/learngit.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上创建仓库时,如果新建了个README.md,可是GitHub远程仓库中的README.md文件不在本地仓库中,造成的冲突。
解决方案:
$ git pull --rebase origin master
$ git push -u origin master
之后即可正常同步。


本文由 方方無 创作,采用 知识共享署名 3.0,可自由转载、引用,但需署名作者且注明文章出处。

还不快抢沙发

添加新评论

shijiebei 365bet manbetx 188bet xinshui caipiao 95zz tongbaoyule beplay 88bifa 18luck betway bwin hg0088 aomenjinshayulecheng ca88 shenbotaiyangcheng vwin w88 weide