github远程仓库常用命令实战

  • 创建ssh key
    ssh-keygen -t rsa -C "email@example.com"

  • 生成ssh的秘钥

Administrator@USER-QKAROI6I4J MINGW64 /c/opt/demo2 (master)
$ ssh-keygen -t rsa -C "test@163.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Administrator/.ssh/id_rsa):
Created directory '/c/Users/Administrator/.ssh'.
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/Administrator/.ssh/id_rsa.
Your public key has been saved in /c/Users/Administrator/.ssh/id_rsa.pub.
The key fingerprint is:test@163.com
The key's randomart image is:
+---[RSA 2048]----+
|             .+..|
|             +E= |
|            o.o.+|
|      .  o ...++o|
|     o .S =  *oo.|
|      o .+. O.B. |
|     .  .oo* B.  |
|      . +=oo.    |
|       oo*O+.    |
+----[SHA256]-----+

Administrator@USER-QKAROI6I4J MINGW64 /c/opt/demo2 (master)
$ pwd
/c/opt/demo2
  • 切换到密钥目录
Administrator@USER-QKAROI6I4J MINGW64 /c/opt/demo2 (master)
$ cd /c/Users/Administrator/.ssh

Administrator@USER-QKAROI6I4J MINGW64 ~/.ssh
$ ls
id_rsa  id_rsa.pub

Administrator@USER-QKAROI6I4J MINGW64 ~/.ssh
$ ll
total 5
-rw-r--r-- 1 Administrator 197121 1766 六月  2 09:35 id_rsa
-rw-r--r-- 1 Administrator 197121  406 六月  2 09:35 id_rsa.pub
  • 查看密钥文件
Administrator@USER-QKAROI6I4J MINGW64 ~/.ssh
$ cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDEcQ1jpXSIHFrTrBL4U5BEiDcqw4Qo6WMY7R/p6On

+6mgwUSHyJSPg5xvR5g2lLkQhWdC56knFqrZcPIukcedwWooQ5aYn84FaKtULHZxUEju7nJWLTHZqNk/YddTu65AwV

+CyVroxqByrUAE6r80s6pX6YXoS83b55hNPSM1jYXS4nvJTe3kCrHiM9wCm8uKYljxSaoeK/QoyPO6xg7Cm0RNlu5+S

3LQc5FJgh6FEhhLJaNzI/wo36A3/O/ORt9+Bfpt2PbeAag04xqT7/ekDkunTG8iYYxDHFcbcFmfjgMkTTiV8jPyDIcr

viIXld56gXx0KUMWMKN7CJZoKFWxV test@163.com
  • 测试是否连接到github
Administrator@USER-QKAROI6I4J MINGW64 ~/.ssh
$ ssh -T git@github.com
The authenticity of host 'github.com (13.250.177.223)' 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,13.250.177.223' (RSA) to the list of known hosts.
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa':
Hi chenliang1234576! You've successfully authenticated, but GitHub does not provide shell 

access.
 
  • 添加远程仓库
    切换到本地仓库,输入以下命令将该仓库关联到github上
Administrator@USER-QKAROI6I4J MINGW64 /c/opt/repodemo2 (master)
$ git remote add origin  git@github.com:test/repodemo2.git
  • 将本地仓库推送到github上
Administrator@USER-QKAROI6I4J MINGW64 /c/opt/repodemo2 (master)
$ git push -u origin master
Warning: Permanently added the RSA host key for IP address '113.229.188.59' to the list of 

known hosts.
Enter passphrase for key '/c/Users/Administrator/.ssh/id_rsa': 123456
Counting objects: 3, done.
Writing objects: 100% (3/3), 235 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:test/repodemo2.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.

克隆github中的项目

Administrator@USER-QKAROI6I4J MINGW64 /c/opt/clone_demo1
$ git clone git@github.com:chenliang1234576/repodemo1.git
Cloning into 'repodemo1'...
remote: Enumerating objects: 10, done.
remote: Counting objects: 100% (10/10), done.
remote: Compressing objects: 100% (5/5), done.
remote: Total 10 (delta 1), reused 9 (delta 0), pack-reused 0
Receiving objects: 100% (10/10), done.
Resolving deltas: 100% (1/1), done.
Checking connectivity... done.
  
Administrator@USER-QKAROI6I4J MINGW64 /c/opt/clone_demo1/repodemo1 (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
nothing to commit, working directory clean
  • 本地仓库中增加内容,并且提交到github中
Administrator@USER-QKAROI6I4J MINGW64 /c/opt/clone_demo1/repodemo1 (master)
$ echo "clone demo" >> clone.txt
  
Administrator@USER-QKAROI6I4J MINGW64 /c/opt/clone_demo1/repodemo1 (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Untracked files:
  (use "git add <file>..." to include in what will be committed)

        clone.txt

nothing added to commit but untracked files present (use "git add" to track)

Administrator@USER-QKAROI6I4J MINGW64 /c/opt/clone_demo1/repodemo1 (master)
$ git add clone.txt
warning: LF will be replaced by CRLF in clone.txt.
The file will have its original line endings in your working directory.

Administrator@USER-QKAROI6I4J MINGW64 /c/opt/clone_demo1/repodemo1 (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   clone.txt


Administrator@USER-QKAROI6I4J MINGW64 /c/opt/clone_demo1/repodemo1 (master)
$ git commit -m " clone commit 001"
[master cc1f4cd]  clone commit 001
warning: LF will be replaced by CRLF in clone.txt.
The file will have its original line endings in your working directory.
 1 file changed, 1 insertion(+)
 create mode 100644 clone.txt

Administrator@USER-QKAROI6I4J MINGW64 /c/opt/clone_demo1/repodemo1 (master)
  • 提交到github中
$ git push
warning: push.default is unset; its implicit value has changed in
Git 2.0 from 'matching' to 'simple'. To squelch this message
and maintain the traditional behavior, use:

  git config --global push.default matching

To squelch this message and adopt the new behavior now, use:

  git config --global push.default simple

When push.default is set to 'matching', git will push local branches
to the remote branches that already exist with the same name.

Since Git 2.0, Git defaults to the more conservative 'simple'
behavior, which only pushes the current branch to the corresponding
remote branch that 'git pull' uses to update the current branch.

See 'git help config' and search for 'push.default' for further information.
(the 'simple' mode was introduced in Git 1.7.11. Use the similar mode
'current' instead of 'simple' if you sometimes use older versions of Git)

Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (2/2), done.
Writing objects: 100% (3/3), 280 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To git@github.com:chenliang1234576/repodemo1.git
   f7ec5bb..cc1f4cd  master -> master
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 215,923评论 6 498
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 92,154评论 3 392
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 161,775评论 0 351
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 57,960评论 1 290
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 66,976评论 6 388
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 50,972评论 1 295
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 39,893评论 3 416
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 38,709评论 0 271
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 45,159评论 1 308
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 37,400评论 2 331
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 39,552评论 1 346
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 35,265评论 5 341
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 40,876评论 3 325
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 31,528评论 0 21
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 32,701评论 1 268
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 47,552评论 2 368
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 44,451评论 2 352

推荐阅读更多精彩内容