当前位置:   article > 正文

github教程_git init 之后就位于分支master上

git init 之后就位于分支master上

一:本地设置git仓库并上传到github

注:只有本地设置git仓库,才能被git识别,才能实现上传的功能。

(1)在需要设置仓库的目录右键git bash here 进入命令行模式

 (2)查看当前仓库的状态

:目前的状态是 not a git repository,说明没有不是仓库。(我们还没有设置

  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo
  2. $ git status
  3. fatal: not a git repository (or any of the parent directories): .git

(3)开始设置本地仓库

  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo
  2. $ git init
  3. Initialized empty Git repository in F:/abcm/test/demo/.git/
  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  2. $ git status
  3. On branch master
  4. No commits yet
  5. nothing to commit (create/copy files and use "git add" to track)

 注:git status显示在分支master上了。解释一下什么是分支:如下所示,是当前的两个branch分支。git init 就是默认在master分支上了。

(4)在仓库里添加文件

我们在目录里新建一个txt文件做测试:

 使用git add命令添加进仓库(虽然目录里有这个文件,但是不属于这个仓库):

  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  2. $ git add hit.txt

如下所示,git add只是把文件添加到了「临时缓冲区」,这个命令有效防止了我们错误提交的可能性。

  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  2. $ git status
  3. On branch master
  4. No commits yet
  5. Changes to be committed:
  6. (use "git rm --cached <file>..." to unstage)
  7. new file: hit.txt

所以我们需要进行事务的提交:(把txt文件提交,提交文件可以用后缀的方法

git commit -m "text commit"

注:后续会说明提交事务的代码;

(5)利用ssh完成git和github的绑定(绑定完的可以跳过)

如果是新下载的git,需要利用ssh完成git和github的绑定:

  • 首先查看本机是否装有SSH key:
  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  2. $ ssh
  3. usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
  4. [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
  5. [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
  6. [-i identity_file] [-J [user@]host[:port]] [-L address]
  7. [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
  8. [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
  9. [-w local_tun[:remote_tun]] destination [command [argument ...]]
  • 输入ssh-keygen -t rsa

:表示我们指定 RSA 算法生成密钥,然后敲三次回车键,期间不需要输入密码,之后就就会生成两个文件,分别为id_rsa和id_rsa.pub,即密钥id_rsa和公钥id_rsa.pub.(会显示生成的具体目录)。

  • 我们要做的事情就是把公钥id_rsa.pub的内容添加到 GitHub。

 

  • 测试绑定是否成功:
  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  2. $ ssh -T git@github.com
  3. Hi AIhang427! You've successfully authenticated, but GitHub does not provide shell access.
  • 开始前我们需要先设置提交的用户信息,包括用户名和邮箱:: 
  1. $ git config --global user.name 'runoob'
  2. $ git config --global user.email test@runoob.com

 (6)提交代码

首先需要将本地的仓库和github的仓库进行关联:

:demo_test这个名字是我自己随便起的,用来代替下面的github网址
 

  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  2. $ git remote add demo_test https://github.com/AIhang427/test.git

如果出现连接超时,基本就是代理出问题:(使用如下命令解决

  1. git config --global --unset http.proxy
  2. git config --global --unset https.proxy

然后,我们就可以提交代码到github库了:

完整的流程:

  1. git add .(文件name) //添加文件到本地仓库
  2. git commit -m “first commit” //添加文件描述信息
  3. git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支
  4. git pull origin master --allow-unrelated-histories // 把远程仓库的文件更新到本地
  5. git push -u origin master //把本地仓库的文件推送到远程仓库

注: 一般来说,自己本地建的仓库和远程仓库是两个仓库,所以直接 git pull origin master会报错。所以我们在后面加--allow-unrelated-histories,不然容易报错 fatal: refusing to merge unrelated histories,因为这是两个不同的仓库,git不会合并,会导致无法拉取。

如下命令

  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo
  2. $ git init
  3. Initialized empty Git repository in F:/abcm/test/demo/.git/
  4. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  5. $ git add new_git.txt
  6. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  7. $ git commit -m "text commit"
  8. [master (root-commit) 608fdb3] text commit
  9. 1 file changed, 0 insertions(+), 0 deletions(-)
  10. create mode 100644 new_git.txt
  11. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  12. $ git remote add origin https://github.com/AIhang427/test.git
  13. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  14. $ git config --global --unset http.proxy
  15. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  16. $ git config --global --unset https.proxy
  17. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  18. $ git pull origin master --allow-unrelated-histories
  19. From https://github.com/AIhang427/test
  20. * branch master -> FETCH_HEAD
  21. Merge made by the 'ort' strategy.
  22. hit.txt | 0
  23. tongbu | 1 +
  24. 2 files changed, 1 insertion(+)
  25. create mode 100644 hit.txt
  26. create mode 100644 tongbu
  27. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
  28. $ git push origin master
  29. Enumerating objects: 5, done.
  30. Counting objects: 100% (5/5), done.
  31. Delta compression using up to 8 threads
  32. Compressing objects: 100% (3/3), done.
  33. Writing objects: 100% (4/4), 503 bytes | 251.00 KiB/s, done.
  34. Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
  35. To https://github.com/AIhang427/test.git
  36. df4bb58..2bab414 master -> master

:          git push -u origin master 是默认相同的分支进行上传和拉取。因此我们可以自定义远程的分支:

$ git push <远程主机名> <本地分支名>:<远程分支名>

二:远程拉取github仓库

远程拉取仓库

  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test
  2. $ git clone -b master https://github.com/AIhang427/test.git
  3. Cloning into 'test'...
  4. remote: Enumerating objects: 16, done.
  5. remote: Counting objects: 100% (16/16), done.
  6. remote: Compressing objects: 100% (8/8), done.
  7. remote: Total 16 (delta 0), reused 7 (delta 0), pack-reused 0
  8. Receiving objects: 100% (16/16), done.

这里就不需要上面那么麻烦了:git push origin master 就可以直接上传提交代码了。

  1. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/test (master)
  2. $ git add ceshi.txt
  3. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/test (master)
  4. $ git commit -m "text commit"
  5. [master f495882] text commit
  6. 1 file changed, 0 insertions(+), 0 deletions(-)
  7. create mode 100644 ceshi.txt
  8. Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/test (master)
  9. $ git push origin master
  10. Enumerating objects: 3, done.
  11. Counting objects: 100% (3/3), done.
  12. Delta compression using up to 8 threads
  13. Compressing objects: 100% (2/2), done.
  14. Writing objects: 100% (2/2), 235 bytes | 235.00 KiB/s, done.
  15. Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
  16. remote: Resolving deltas: 100% (1/1), completed with 1 local object.
  17. To https://github.com/AIhang427/test.git
  18. 2bab414..f495882 master -> master

三:事务提交等一些操作的简化和细节

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/901745
推荐阅读
相关标签
  

闽ICP备14008679号