如何上传代码到github?

github是什么?

github是Git 远程仓库。

github是一个基于git的代码托管平台

Git是什么:

Git 是一个开源的分布式版本控制系统,用于敏捷高效地处理任何或小或大的项目。

如何上传代码到github?

参考:https://www.runoob.com/git/git-remote-repo.html

  • 在githbu上注册账号
  1. 注册账号
  2. 创建一个项目
  3. 获得项目的地址

二.下载git

三.本地操作: 使用git命令上传

 

以下是具体操作步骤:

一.在githbu上注册账号

1. 先到https://github.com/ 注册一个账号,此处忽略

2.创建一个项目

2.1.点击New

2.2填写相应信息后点击“Create repository创建项目

Repository name: 仓库名称

Description(可选): 仓库描述介绍

Public,Private: 仓库权限(公开共享,私有或指定合作者)

Initialize this repository with a README: 添加一个README.md

gitignore: 不需要进行版本管理的仓库类型,对应生成文件.gitignore

License: 证书类型,对应生成文件license,例如:GNU Genneral Public License v3.0

2.3 获取项目地址

在项目详情页面,点击”Clone or dowload”,看到的url是该项目的地址。

二.下载git,安装git

官网下载地址:https://git-scm.com/downloads ,选择windows版本,默认的版本是windows 64;也点击“Older releases”选择与系统匹配的版本进行下载(32bit/64bit)。

安装步骤可参考:https://blog.csdn.net/ezreal_tao/article/details/81609883

三.本地操作: 使用git命令上传

3.1 进入本地项目目录,在目录中点击右键,会出现两个新选项,分别为Git Gui Here,Git Bash Here,选择Git Bash Here,如图

3.2 把github上面的仓库克隆(复制)到本地,使用下面命令

#git clone https://github.com/XXX/python_apiAutotest.git

备注:Url是github的仓库地址,

执行命令后,本地会看到这个项目仓库,例如:python_apiAutotest

3.3.把需要上传的代码,放入项目目录:python_apiAutotest

3.4 进入项目目录,例如:cd python_apiAutotest

3.5.上传代码至github

git add <filename>   (注:filename是文件名)

或者git add .       (注:后面的.是把项目文件夹里面的文件都添加进来)

------这是 git 基本工作流程的第一步;使用如下命令以实际提交改动

git commit  -m  "代码提交信息"  (注:"代码提交信息"里面换成你需要,如“python apitest”)

    ----你的改动已经提交到了 HEAD,但是还没到你的远端仓库

git push -u origin master   (注:是把本地仓库push到github上面,此时需要输入你的github帐号和密码)

   ----将这些改动提交到远端仓库(可以把 master 换成你想要推送的任何分支)

其他:

1. #git status:查看是否还有文件未提交

$ git status

On branch master

Your branch is ahead of 'origin/master' by 1 commit.

  (use "git push" to publish your local commits)

 

nothing to commit, working tree clean

修改文件11.py后,再输入命令:git status

$ git status

On branch master

Your branch is ahead of 'origin/master' by 1 commit.

  (use "git push" to publish your local commits)

Changes not staged for commit:

  (use "git add <file>..." to update what will be committed)

  (use "git restore <file>..." to discard changes in working directory)

        modified:   11.py

 

no changes added to commit (use "git add" and/or "git commit -a")

提示:11.py文件已被修改,但是未被提交的修改

2. #git diff <filename> :查看文件修改了什么内容

 

3.回退版本(HEAD^是回退到上一个版本,HEAD^^是回退到上上版本)

$ git reset --hard HEAD^

HEAD is now at 1829f5e 新修改

如果是回退到100个版本,命令:git reset --hard HEAD~100

4. 如果想回到最新版本

$git reset --hard 版本号

查版本号:git reflog

 

5. 删除文件rm <filename>

#rm c.txt   

--删除的是本地版本库中的文件,如果需要彻底删除,需要git add c.txt 然后git commit -m "代码提交信息",然后$ git push -u origin master;执行一遍。

--如果不想删除了,想恢复,那么执行命令:$ git checkout -- c.txt

 

 

其他二.创建与合并分支

主分支:master分支

其他分支:例如:develop

分支策略:首先master主分支是非常稳定的,也就是用来发布新版本,一般情况下不允许在上面"新增代码","工作"一般情况下在新建的develop分支上"新增代码",新增后,比如上要发布,或者说develop分支代码稳定后可以合并到主分支master上来。

创建与合并分支命令总结如下:

查看分支:git branch

创建分支:git branch name

切换分支:git checkout name

创建+切换分支:git checkout –b name

合并某分支到当前分支:git merge name

删除分支:git branch –d name

2.1.创建并切换分支

$ git checkout -b develop

Switched to a new branch 'develop'

 

2.2.切换分支

$ git checkout develop

Switched to branch 'develop'

 

2.3.合并某分支到当前分支

$ git merge master   #把master分支的文件合并到develop分支上。

Updating b3dea2f..5c88f9e

Fast-forward

 11.txt | 1 +

 1 file changed, 1 insertion(+)

 create mode 100644 11.txt

 

2.4.在分支上修改文件并提交。

$ git add 11.txt

 

fenfen@DESKTOP-S8P29O9 MINGW64 /d/python_github/python_apiAutotest (develop)

$ git commit -m "11.txt在develop分支上加上2222"

[develop 1efe320] 11.txt在develop分支上加上2222

 1 file changed, 2 insertions(+), 1 deletion(-)

 

$ git push  origin develop    #push到远程仓库

Enumerating objects: 5, done.

Counting objects: 100% (5/5), done.

Delta compression using up to 4 threads

Compressing objects: 100% (3/3), done.

Writing objects: 100% (3/3), 304 bytes | 304.00 KiB/s, done.

Total 3 (delta 1), reused 0 (delta 0), pack-reused 0

remote: Resolving deltas: 100% (1/1), completed with 1 local object.

To https://github.com/fenfen532/python_apiAutotest.git

   c420d35..1efe320  develop -> develop

Branch 'develop' set up to track remote branch 'develop' from 'origin'.

$ cat 11.txt    #查看文件内容

11111111111111111111111111

22222222222222222222222222

 

2.6 #把远程仓库的develop分支的抓取到本地来。

$ git pull

Already up to date.    

 

参考:https://blog.csdn.net/qq_36150631/article/details/81038485

发布了135 篇原创文章 · 获赞 6 · 访问量 23万+

猜你喜欢

转载自blog.csdn.net/fen_fen/article/details/105428400