1.首先创建 SHH Key
在用户主目录下,看看有没有.ssh目录,如果有,再看看这个目录下有没有id_rsa和id_rsa.pub这两个文件,如果已经有了,可直接跳到下一步。
登陆GitHub,打开“Account settings”,“SSH Keys”页面:
然后,点“Add SSH Key”,填上任意Title,在Key文本框里粘贴id_rsa.pub文件的内容:
$ ssh-keygen -t rsa -C "[email protected]"
2.从github上将代码克隆至本地(blog目录)
注意:这里只是将代码复制到本地,并不是将代码复制到本地并且创建自己的本地库。复制到本地之后还要自己创建本地库
$ git clone [email protected]:jaanuser/jaanuser.github.io.git
当你第一次使用Git的clone或者push命令连接GitHub时,会得到一个警告
这是因为Git使用SSH连接,而SSH连接在第一次验证GitHub服务器的Key时,需要你确认GitHub的Key的指纹信息是否真的来自GitHub的服务器,输入yes回车即可。
clone完成后会发现你的当前目录下多了一个 jaanuser.github.io的目录 (blog/jaanuser.github.io)
3.建立本地库,并修改文件
在克隆下来的文件夹,右击,git bash here,然后执行 git init (初始化本地仓库)
我到这一步当前目录还在blog目录 我这里弄错了 把blog弄成了本地库
然后在win下把jaanuser.github.io目录中的一些文件删除了
执行git status(在blog下)
aan@DESKTOP-D2VK7FF MINGW64 /h/blog (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
(commit or discard the untracked or modified content in submodules)
modified: jaanuser.github.io (modified content)
no changes added to commit (use "git add" and/or "git commit -a")
这里只显示了jaanuser.github.io目录下有改动过,但是并没有显示具体删除了哪些文件
然后将当前目录切换到jaanuser.github.io下
执行git status,就显示了具体内容
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: CNAME
deleted: fancybox/blank.gif
deleted: fancybox/fancybox_loading.gif
deleted: fancybox/[email protected]
deleted: fancybox/fancybox_overlay.png
deleted: fancybox/fancybox_sprite.png
deleted: fancybox/[email protected]
deleted: fancybox/helpers/fancybox_buttons.png
deleted: fancybox/helpers/jquery.fancybox-buttons.css
deleted: fancybox/helpers/jquery.fancybox-buttons.js
deleted: fancybox/helpers/jquery.fancybox-media.js
deleted: fancybox/helpers/jquery.fancybox-thumbs.css
deleted: fancybox/helpers/jquery.fancybox-thumbs.js
deleted: fancybox/jquery.fancybox.css
deleted: fancybox/jquery.fancybox.js
deleted: fancybox/jquery.fancybox.pack.js
deleted: index.html
no changes added to commit (use "git add" and/or "git commit -a")
然后将其中部分文件 rm
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git rm fancybox*
rm 'fancybox/blank.gif'
rm 'fancybox/fancybox_loading.gif'
rm 'fancybox/[email protected]'
rm 'fancybox/fancybox_overlay.png'
rm 'fancybox/fancybox_sprite.png'
rm 'fancybox/[email protected]'
rm 'fancybox/helpers/fancybox_buttons.png'
rm 'fancybox/helpers/jquery.fancybox-buttons.css'
rm 'fancybox/helpers/jquery.fancybox-buttons.js'
rm 'fancybox/helpers/jquery.fancybox-media.js'
rm 'fancybox/helpers/jquery.fancybox-thumbs.css'
rm 'fancybox/helpers/jquery.fancybox-thumbs.js'
rm 'fancybox/jquery.fancybox.css'
rm 'fancybox/jquery.fancybox.js'
rm 'fancybox/jquery.fancybox.pack.js'
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes to be committed:
(use "git reset HEAD ..." to unstage)
deleted: fancybox/blank.gif
deleted: fancybox/fancybox_loading.gif
deleted: fancybox/[email protected]
deleted: fancybox/fancybox_overlay.png
deleted: fancybox/fancybox_sprite.png
deleted: fancybox/[email protected]
deleted: fancybox/helpers/fancybox_buttons.png
deleted: fancybox/helpers/jquery.fancybox-buttons.css
deleted: fancybox/helpers/jquery.fancybox-buttons.js
deleted: fancybox/helpers/jquery.fancybox-media.js
deleted: fancybox/helpers/jquery.fancybox-thumbs.css
deleted: fancybox/helpers/jquery.fancybox-thumbs.js
deleted: fancybox/jquery.fancybox.css
deleted: fancybox/jquery.fancybox.js
deleted: fancybox/jquery.fancybox.pack.js
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: CNAME
deleted: index.html
可以看到 rm 的这部分已经从工作区添加到了暂存区(虽然这时候win上的文件已经删了,在前面我说在win上删除一些文件的时候就已经删除了,但是本地库还没变),如果再commit的话 本地库也就改变了
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git reset HEAD fanc*
从暂存区中撤回到暂存区
然后看现在的状态
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git status
On branch master
Your branch is up to date with 'origin/master'.
Changes not staged for commit:
(use "git add/rm ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
deleted: CNAME
deleted: fancybox/blank.gif
deleted: fancybox/fancybox_loading.gif
deleted: fancybox/[email protected]
deleted: fancybox/fancybox_overlay.png
deleted: fancybox/fancybox_sprite.png
deleted: fancybox/[email protected]
deleted: fancybox/helpers/fancybox_buttons.png
deleted: fancybox/helpers/jquery.fancybox-buttons.css
deleted: fancybox/helpers/jquery.fancybox-buttons.js
deleted: fancybox/helpers/jquery.fancybox-media.js
deleted: fancybox/helpers/jquery.fancybox-thumbs.css
deleted: fancybox/helpers/jquery.fancybox-thumbs.js
deleted: fancybox/jquery.fancybox.css
deleted: fancybox/jquery.fancybox.js
deleted: fancybox/jquery.fancybox.pack.js
deleted: index.html
no changes added to commit (use "git add" and/or "git commit -a")
现在都到了 工作区,这时win上的文件还是没有,暂时还没恢复回来
$ git checkout -- CNAME
$ git checkout -- fan*
$ git checkout -- index.html
(git checkout -- file命令中的--很重要,没有--,就变成了“切换到另一个分支”的命令,我们在后面的分支管理中会再次遇到git checkout命令)
checkout 之后撤销工作区的内容,这时候win上面的文件恢复了回来
-------------------------------------------------------上面为删除文件------------------------------------------下面进行新建文件--------------------------------------------------
在win上的jaanuser.github.io 目录下新建了文件test.txt
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git status
On branch master
Untracked files:
(use "git add ..." to include in what will be committed)
test.txt
nothing added to commit but untracked files present (use "git add" to track)
//这个目录下看到的变化
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog (master)
$ git status
On branch master
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
(commit or discard the untracked or modified content in submodules)
modified: jaanuser.github.io (untracked content)//这里并不是删除文件时的(modified content)
no changes added to commit (use "git add" and/or "git commit -a")
//blog下看到的变化
然后我想撤销新建
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git checkout -- test.txt
error: pathspec 'test.txt' did not match any file(s) known to git
//好像并不能撤销
//我将这个add一下放入暂存区
//然后再撤销
$ git reset HEAD test.txt //撤销成功 现在跑到了工作区
//然后再次执行
$ git checkout -- test.txt
error: pathspec 'test.txt' did not match any file(s) known to git//还是撤销不了, 为什么新建的文件撤销了?
//我手动在win下将新建的text.txt删除,然后执行 git status
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git status
On branch master
nothing to commit, working tree clean
刚才测试的删除的文件都删除了,现在我们重新删除一些文件,然后下一步连接远程库进行修改。
删除完rm完commit的是时候如果忘记 写 -m “XXX”时,会进去一个vi页面 :wq 返回就会终止提交
4.添加远程库
$ git remote add origin [email protected]:michaelliao/learngit.git
删除远程库,是这样删除的(这里了解就好,现在我们不执行,因为刚连接上还要修改远程库内容呢)
git remote remove origin //是在本地库根目录下删除,还是 在哪个目录下连接的远程库,在哪个目录下删除呢?
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git push
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using
git remote add
and then push using the remote name
git push
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git push origin
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git push origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git push -u origin master
fatal: 'origin' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ cd ..
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog (master)
$ git push origin
fatal: The current branch master has no upstream branch.
To push the current branch and set the remote as upstream, use
git push --set-upstream origin master
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog (master)
$ git push origin master
Warning: Permanently added the RSA host key for IP address '52.74.223.119' to the list of known hosts.
To github.com:jaanuser/jaanuser.github.io.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to '[email protected]:jaanuser/jaanuser.github.io.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.
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog (master)
$ cd jaanuser.github.io
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git remote add origin [email protected]:jaanuser/jaanuser.github.io.git
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)
$ git push origin master //上面再该目录下也执行了这个命令,但是我这次执行之前再这个目录下执行了$ git remote add origin [email protected]:jaanuser/jaanuser.github.io.git
Enumerating objects: 5, done. //不知道上次失败是不是是因为我在blog下执行的$ git remote add origin [email protected]:jaanuser/jaanuser.github.io.git
Counting objects: 100% (5/5), done.
Delta compression using up to 4 threads
Compressing objects: 100% (4/4), done.
Writing objects: 100% (4/4), 402 bytes | 201.00 KiB/s, done.
Total 4 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 1 local object.
To github.com:jaanuser/jaanuser.github.io.git
de55061..b1edbe2 master -> master
jaan@DESKTOP-D2VK7FF MINGW64 /h/blog/jaanuser.github.io (master)