【Github】小试牛刀

【Github】小试牛刀

    • 参考资料
    • github 简明教程
      • 1.登陆并创建repository
      • 2.下载git客户端
      • 3. 绑定用户
      • 4.SSH key绑定
      • 5.使用github
    • git理解
    • git操作
    • github问题
      • 1.Large files detected
      • 2.VScode中的git配置
      • 3. .gitignore 文件 和README.md 文件

参考资料

如何在同一台电脑上使用 github 和 gitlab
Git 命令总结
Git教程
Using Version Control in VS Code
VSCode中的git使用篇
SourceTree安装与使用
SourceTree使用方法

github 简明教程

1.登陆并创建repository

2.下载git客户端

官网下载即可,选项全部默认

3. 绑定用户

打开git-bash.exe,输入以下命令来将你的git客户端和账户绑定

18120@DESKTOP-AG3HVB1 MINGW64 ~
$ git config --global user.name "synjoker"
	
18120@DESKTOP-AG3HVB1 MINGW64 ~
$ git config --global user.email "[email protected]"

C:\Users\Administator中可以发现.gitconfig文件

[core]
	editor = \"D:\\vscode\\Code.exe\" --wait
[gui]
	recentrepo = D:/vsproject
[user]
	name = synjoker
	email = [email protected]
[credential]  # 此处是设置保存密码使用(复制到文件时请删除备注)
    helper = store

4.SSH key绑定

  1. 在本地生成公钥和私钥

    18120@DESKTOP-AG3HVB1 MINGW64 ~
    $ cd ~/.ssh
    
    18120@DESKTOP-AG3HVB1 MINGW64 ~/.ssh
    $ ls
    cattle  cattle.pub  config  id_rsa  id_rsa.pub  known_hosts
    

    其中id_rsa、id_rsa.pub、known_hosts就是生成的密钥文件

    初始设置时请使用$ ssh-keygen -t rsa -C "[email protected]"来生成你的密钥,之后全部默认回车。

    然后请复制公钥id_rsa.pub内的文本内容

  2. 在github配置SSH key

    打开setting-SSH and GPG key
    【Github】小试牛刀_第1张图片
    点击右上角New SSH key,标题设置repository的名称,复制公钥id_rsa.pub内的文本内容,完成配置。

5.使用github

//第一步
git init // 把这个目录变成Git可以管理的仓库
//第二步
git add README.md // 文件添加到仓库
git add . // 不但可以跟单一文件,还可以跟通配符,更可以跟目录。一个点就把当前目录下所有未追踪的文件全部add了 
//第三步
git commit -m "first commit" // 把文件提交到仓库
//第四步
git remote add origin [email protected]:wangjiax9/practice.git // 关联远程仓库
git push -u origin master // 把本地库的所有内容推送到远程库上
  1. 建立本地仓库

    18120@DESKTOP-AG3HVB1 MINGW64 /d/vsproject (master)
    $ git init
    Reinitialized existing Git repository in D:/vsproject/.git/
    // 作者之前做过,所以这里是Reinitialized
    
  2. add文件

    18120@DESKTOP-AG3HVB1 MINGW64 /d/vsproject (master)
    $ git add .
    warning: LF will be replaced by CRLF in hello_django/.vscode/settings.json.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in helloworld/.vscode/tasks.json.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in pyhelloworld/.vscode/settings.json.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in pyhelloworld/NetworkLearning/.vscode/settings.json.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in pyhelloworld/SPACEROCKPROJECT/ClassifySpaceRockProgram.ipynb.
    The file will have its original line endings in your working directory
     giwarning: LF will be replaced by CRLF in pyhelloworld/success/1203/clientgps.py.
    The file will have its original line endings in your working directory
    warning: LF will be replaced by CRLF in pyhelloworld/success/120301/clientgps.py.
    The file will have its original line endings in your working directory
    
  3. commit 提交文件

    18120@DESKTOP-AG3HVB1 MINGW64 /d/vsproject (master)
    $ git commit -m "提交文件"
    [master (root-commit) 44fcdb7] 提交文件
     685 files changed, 5355 insertions(+)
     create mode 100644 error.txt
     create mode 100644 hello_django/.vscode/launch.json
     create mode 100644 hello_django/.vscode/settings.json
     create mode 100644 hello_django/db.sqlite3
     create mode 100644 hello_django/hello/__init__.py
     create mode 100644 hello_django/hello/__pycache__/__init__.cpython-37.pyc
     create mode 100644 hello_django/hello/__pycache__/urls.cpython-37.pyc
     create mode 100644 hello_django/hello/__pycache__/views.cpython-37.pyc
     create mode 100644 hello_django/hello/admin.py
     create mode 100644 hello_django/hello/apps.py
     create mode 100644 hello_django/hello/migrations/__init__.py
     ···
     create mode 100644 "pyhelloworld/\346\211\223\346\215\206\346\234\272\347\250\213\345\272\217/2.0newWINDOWNSserver.py"
     create mode 100644 "pyhelloworld/\346\211\223\346\215\206\346\234\272\347\250\213\345\272\217/2021-01-11/DevelopDocument.txt"
     create mode 100644 "pyhelloworld/\346\211\223\346\215\206\346\234\272\347\250\213\345\272\217/2021-01-11/client.py"
     create mode 100644 "pyhelloworld/\346\211\223\346\215\206\346\234\272\347\250\213\345\272\217/2021-01-11/server.py"
     create mode 100644 "pyhelloworld/\346\211\223\346\215\206\346\234\272\347\250\213\345\272\217/success/Clientimprove.py"
     create mode 100644 "pyhelloworld/\346\211\223\346\215\206\346\234\272\347\250\213\345\272\217/success/Serverimprove.py"
     create mode 100644 "pyhelloworld/\346\211\223\346\215\206\346\234\272\347\250\213\345\272\217/\347\250\213\345\272\217\347\262\276\350\257\273.md"
     create mode 100644 vsworkspace/demo
     create mode 100644 vsworkspace/socket
    
  4. 关联远程仓库并推送

    18120@DESKTOP-AG3HVB1 MINGW64 /d/vsproject (master)
    $ git remote add origin [email protected]:synjoker/PyVScode.git
    
    18120@DESKTOP-AG3HVB1 MINGW64 /d/vsproject (master)
    $ git push -u origin master
    The authenticity of host 'github.com (13.229.188.59)' can't be established.
    ···
    Warning: Permanently added 'github.com,13.229.188.59' (RSA) to the list of known hosts.
    Enumerating objects: 543, done.
    Counting objects: 100% (543/543), done.
    Delta compression using up to 4 threads
    Compressing objects: 100% (535/535), done.
    Writing objects: 100% (543/543), 194.46 MiB | 1.05 MiB/s, done.
    Total 543 (delta 33), reused 0 (delta 0), pack-reused 0
    remote: Resolving deltas: 100% (33/33), done.
    remote: warning: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
    remote: warning: See http://git.io/iEPt8g for more information.
    remote: warning: File pyhelloworld/SPACEROCKPROJECT/aerialmodel.pth is 94.00 MB; this is larger than GitHub's recommended maximum file size of 50.00 MB
    To github.com:synjoker/PyVScode.git
     * [new branch]      master -> master
    Branch 'master' set up to track remote branch 'master' from 'origin'.
    

效果显示:synjoker/PyVScode
【Github】小试牛刀_第2张图片

git理解

git 是一个 分布式/版本控制/系统,它分为两个部分:本地和互联网
在本地端,Git仓库需要指定一个用户名和Email地址,作为所有版本库的标志。然后便可以创建版本库
版本库的文件夹中附带.git目录,其中文件负责git版本库的管理,所以千万不要修改这个目录
git文件的上传远程仓库主要分为3步:

  • git add:将文件添加到暂存区(就是类似于货车重复装载货物)
  • git commit:把文件提交到本地仓库,并添加注释(货车发车,前往git仓库)
  • push:提交到远程仓库

【Github】小试牛刀_第3张图片
另外最主要的便是分支的理解:
git主要记录master分支的版本,当我们需要测试一些新功能的代码而不暂时修改当前的master分支,我们可以新建一个dev分支,并修改HEAD指向。我们的修改可以提交到dev分支,当我们完成后,可以将dev合并到master上提交,就完成了合并。
冲突管理:修改并删除分支
分支管理:master-dev-各个人的小分支
BUG修复:git stash保护现场;新建分支修复BUG后合并;

git操作

$ git status //查看git库状态
$ git diff readme.txt //查看工作区文件和版本库当前分支上文件的差异

要明白这3个概念,工作区(working tree),暂存区(index /stage),本地仓库(repository)

git跟不同的参数,比较不同的区间的版本。

  • git diff:是查看working tree与index的差别的。
  • git diff --cached:是查看index与repository的差别的。
  • git diff HEAD:是查看working tree和repository的差别的。其中:HEAD代表的是最近的一次commit的信息。
$ git log	// 命令的历史记录
commit 6d57f39d7957a7dbe72d986cf942b087d5923648 (HEAD -> master, origin/master)
Author: synjoker <[email protected]>
Date:   Tue Jan 12 14:57:09 2021 +0800

    修改readme.md

commit 69e82a635825eef9180c010c6532f493390dca78
Merge: 918b869 e0861f9
Author: synjoker <[email protected]>
Date:   Tue Jan 12 14:45:32 2021 +0800

    Merge branch 'master' of github.com:synjoker/PyVScode

commit 918b8699e6c2c35922f0868b7865e1be46ec0ed9
Author: synjoker <[email protected]>
Date:   Tue Jan 12 14:44:44 2021 +0800

    测试2021-01-12

commit 9ac5bbdf6924baaa9da0d955d6925b4e8305db16
Author: synjoker <[email protected]>
Date:   Tue Jan 12 14:38:51 2021 +0800

    删除2021-01-12分支

commit c454d2cd04a726896e8b1906afca52f4956870d9
Author: synjoker <[email protected]>
Date:   Tue Jan 12 14:33:41 2021 +0800

    上传2021-01-12新的文件

commit e0861f98beff12c9b1da9558712362b206af77e9
Author: synjoker <[email protected]>
Date:   Tue Jan 12 13:44:51 2021 +0800

    Create README.md

commit 44fcdb7b054c35d831c4156e47b13eab6cf5e594
Author: synjoker <[email protected]>
Date:   Tue Jan 12 13:31:55 2021 +0800

    提交文件

$ git log --graph --pretty=oneline --abbrev-commit

回退版本

$ git reset --hard HEAD^/HEAD~100
$ git reflog

克隆git库

$ git clone [email protected]:michaelliao/gitskills.git

分支管理

$ git checkout -b dev // 创建并切换dev分支
$ git branch dev	// 创建dev分支
$ git checkout dev	// 切换dev分支
$ git branch		//查看当前分支
$ git add readme.txt 
$ git commit -m "branch test"	// 在dev分支上提交

$ git checkout master	//切换回master分支
$ git merge dev		//合并dev分支
$ git branch -d dev	//删除分支

$ git switch -c dev	//创建并切换到新的dev分支
$ git switch master	//直接切换到已有的master分支

恢复工作区

$ git merge --no-ff -m "merged bug fix 101" issue-101
$ git switch dev
$ git stash list

//恢复
$ git stash apply
$ git stash drop
//or
$ git stash pop

开发实战

$ git switch -c feature-vulcan
$ git add vulcan.py
$ git status
$ git commit -m "add feature vulcan"
$ git switch dev

$ git branch -d feature-vulcan	//合并后可删除
$ git branch -D feature-vulcan	//强制删除

多人开发实战

$ git remote	//查看远程仓库
$ git remote -v
$ git push origin dev
// 本地和其它成员冲突解决
$ git pull
$ git branch --set-upstream-to=origin/dev dev

github问题

1.Large files detected

remote: warning: GH001: Large files detected. You may want to try Git Large File Storage - https://git-lfs.github.com.
remote: warning: See http://git.io/iEPt8g for more information.
remote: warning: File pyhelloworld/SPACEROCKPROJECT/aerialmodel.pth is 94.00 MB; this is larger than GitHub’s recommended maximum file size of 50.00 MB

2.VScode中的git配置

问题描述:
先安装VScode,后安装git-bash后,VScode中显示没有git输出

问题解决:
vscode内搜索git.path,将安装好的"git.path": "C:/Program Files/Git/bin/git.exe"路径输入进来(一定要是反斜杠/,连双斜杠\\也不可以),重启VScode,就可以看到
【Github】小试牛刀_第4张图片

3. .gitignore 文件 和README.md 文件

配置需要忽略更新的文件对象。

github/gitignore

你可能感兴趣的:(笔记,github)