赞
踩
注:只有本地设置git仓库,才能被git识别,才能实现上传的功能。
注:目前的状态是 not a git repository,说明没有不是仓库。(我们还没有设置)
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo
- $ git status
- fatal: not a git repository (or any of the parent directories): .git
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo
- $ git init
- Initialized empty Git repository in F:/abcm/test/demo/.git/
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git status
- On branch master
-
- No commits yet
-
- nothing to commit (create/copy files and use "git add" to track)
注:git status显示在分支master上了。解释一下什么是分支:如下所示,是当前的两个branch分支。git init 就是默认在master分支上了。
我们在目录里新建一个txt文件做测试:
使用git add命令添加进仓库(虽然目录里有这个文件,但是不属于这个仓库):
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git add hit.txt
如下所示,git add只是把文件添加到了「临时缓冲区」,这个命令有效防止了我们错误提交的可能性。
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git status
- On branch master
-
- No commits yet
-
- Changes to be committed:
- (use "git rm --cached <file>..." to unstage)
- new file: hit.txt
所以我们需要进行事务的提交:(把txt文件提交,提交文件可以用后缀的方法)
git commit -m "text commit"
注:后续会说明提交事务的代码;
如果是新下载的git,需要利用ssh完成git和github的绑定:
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ ssh
- usage: ssh [-46AaCfGgKkMNnqsTtVvXxYy] [-B bind_interface]
- [-b bind_address] [-c cipher_spec] [-D [bind_address:]port]
- [-E log_file] [-e escape_char] [-F configfile] [-I pkcs11]
- [-i identity_file] [-J [user@]host[:port]] [-L address]
- [-l login_name] [-m mac_spec] [-O ctl_cmd] [-o option] [-p port]
- [-Q query_option] [-R address] [-S ctl_path] [-W host:port]
- [-w local_tun[:remote_tun]] destination [command [argument ...]]
-
注:表示我们指定 RSA 算法生成密钥,然后敲三次回车键,期间不需要输入密码,之后就就会生成两个文件,分别为id_rsa和id_rsa.pub,即密钥id_rsa和公钥id_rsa.pub.(会显示生成的具体目录)。
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ ssh -T git@github.com
- Hi AIhang427! You've successfully authenticated, but GitHub does not provide shell access.
- $ git config --global user.name 'runoob'
- $ git config --global user.email test@runoob.com
首先需要将本地的仓库和github的仓库进行关联:
注:demo_test这个名字是我自己随便起的,用来代替下面的github网址
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git remote add demo_test https://github.com/AIhang427/test.git
如果出现连接超时,基本就是代理出问题:(使用如下命令解决)
- git config --global --unset http.proxy
-
- git config --global --unset https.proxy
然后,我们就可以提交代码到github库了:
完整的流程:
- git add .(文件name) //添加文件到本地仓库
- git commit -m “first commit” //添加文件描述信息
- git remote add origin + 远程仓库地址 //链接远程仓库,创建主分支
- git pull origin master --allow-unrelated-histories // 把远程仓库的文件更新到本地
- git push -u origin master //把本地仓库的文件推送到远程仓库
注: 一般来说,自己本地建的仓库和远程仓库是两个仓库,所以直接 git pull origin master会报错。所以我们在后面加--allow-unrelated-histories,不然容易报错 fatal: refusing to merge unrelated histories,因为这是两个不同的仓库,git不会合并,会导致无法拉取。
如下命令:
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo
- $ git init
- Initialized empty Git repository in F:/abcm/test/demo/.git/
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git add new_git.txt
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git commit -m "text commit"
- [master (root-commit) 608fdb3] text commit
- 1 file changed, 0 insertions(+), 0 deletions(-)
- create mode 100644 new_git.txt
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git remote add origin https://github.com/AIhang427/test.git
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git config --global --unset http.proxy
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git config --global --unset https.proxy
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git pull origin master --allow-unrelated-histories
- From https://github.com/AIhang427/test
- * branch master -> FETCH_HEAD
- Merge made by the 'ort' strategy.
- hit.txt | 0
- tongbu | 1 +
- 2 files changed, 1 insertion(+)
- create mode 100644 hit.txt
- create mode 100644 tongbu
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/demo (master)
- $ git push origin master
- Enumerating objects: 5, done.
- Counting objects: 100% (5/5), done.
- Delta compression using up to 8 threads
- Compressing objects: 100% (3/3), done.
- Writing objects: 100% (4/4), 503 bytes | 251.00 KiB/s, done.
- Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
- To https://github.com/AIhang427/test.git
- df4bb58..2bab414 master -> master
注: git push -u origin master 是默认相同的分支进行上传和拉取。因此我们可以自定义远程的分支:
$ git push <远程主机名> <本地分支名>:<远程分支名>
远程拉取仓库
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test
- $ git clone -b master https://github.com/AIhang427/test.git
- Cloning into 'test'...
- remote: Enumerating objects: 16, done.
- remote: Counting objects: 100% (16/16), done.
- remote: Compressing objects: 100% (8/8), done.
- remote: Total 16 (delta 0), reused 7 (delta 0), pack-reused 0
- Receiving objects: 100% (16/16), done.
这里就不需要上面那么麻烦了:git push origin master 就可以直接上传提交代码了。
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/test (master)
- $ git add ceshi.txt
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/test (master)
- $ git commit -m "text commit"
- [master f495882] text commit
- 1 file changed, 0 insertions(+), 0 deletions(-)
- create mode 100644 ceshi.txt
-
- Legion@DESKTOP-50MDCO6 MINGW64 /f/abcm/test/test (master)
- $ git push origin master
- Enumerating objects: 3, done.
- Counting objects: 100% (3/3), done.
- Delta compression using up to 8 threads
- Compressing objects: 100% (2/2), done.
- Writing objects: 100% (2/2), 235 bytes | 235.00 KiB/s, done.
- Total 2 (delta 1), reused 0 (delta 0), pack-reused 0
- remote: Resolving deltas: 100% (1/1), completed with 1 local object.
- To https://github.com/AIhang427/test.git
- 2bab414..f495882 master -> master
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。