如何上傳代碼到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

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章