Pycharm同步本地代碼至GitHub


注冊github賬號

github地址,進入注冊賬號

安裝git

Windows下載地址1
Windows下載地址2
在官方下載完后,雙擊exe文件進行安裝,安裝到Windows Explorer integration的時候,將選項中的“Git Bash here”和“Git GUI here”打對勾。

獲取SSH key

1、 右鍵鼠標,選中 “Git Bash here”,或者安裝目錄打開它

2、 進入Git Bash Here后輸入以下命令:

cd ~/.ssh/

如果出現提示:No such file or directory(沒有這樣的文件或目錄)

執行以下命令創建即可:

mkdir ~/.ssh

3、 執行以下兩個命令配置全局的name和email,這里是的你github登陸名和郵件地址

git config --global user.name "xxx"

git config --global user.email "xxx@163.com"

就是注冊時輸入的這兩個內容,如下圖:

4、 生成key

ssh-keygen -t rsa -C "xxx@163.com"

連續按三次回車,設置了密碼為空,並且創建了key,最后得到了兩個文件:id_rsa和id_rsa.pub(記住生成過程中提示的生成key文件路徑,后面要用到)

在github配置SSH key

1、 登陸github → 進入設置(Settings)

2、 新建SSH key

3、 將本地生成的key放入github,步驟如下:

3.1 進入我們前面生成SSH key的路徑C:\Users\xxx\.ssh(根據自己生成時的路徑去找)
3.2 用記事本打開id_rsa.pub,將內容全部復制

放入github

添加后

測試是否成功
進入Git Bash Here后輸入以下命令:

ssh git@github.com

出現類似提示就代表成功了:Hi xxx! You’ve successfully authenticated, but GitHub does not provide shell access. Connection to github.com closed

在Pycharm添加github賬戶

進入pycharm,settings → Version Control → 添加github賬戶

開始上傳

1、進入Pycharm選擇VCS → Import into Version Control → Share Project on GitHub

接着會讓你選擇項目文件,如果不太熟悉建議把項目都選免得遺漏,出現以下提示就證明成功了。

進入github主頁/我的倉庫就可以看到我們新同步的代碼了

Pycharm拉取或更新代碼

DOS中使用的常用命令

1、獲取github上的項目代碼命令如下:

git clone https://github.com/weibgg/django.git

2、配置git默認配置為忽略大小寫:

git config core.ignorecase false

3、更新github代碼

第一步:查看當前的git倉庫狀態,可以使用git status

D:\xuexi\python\django (master)
>>> git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        ../.idea/
        ../__init__.py
        my_site/my_site/__pycache__/
        test_push.txt

nothing added to commit but untracked files present (use "git add" to track)

第二步:更新全部git add *git add .當前目錄

D:\xuexi\python\django (master)
>>> git add *

第三步:接着輸入git commit -m "更新說明"

D:\xuexi\python\Python基礎\django (master)
>>> git commit -m "20180001bug修復"
[master d1ee53c] 20180001bug修復
 5 files changed, 1 insertion(+)
 create mode 100644 django/my_site/my_site/__pycache__/__init__.cpython-36.pyc
 create mode 100644 django/my_site/my_site/__pycache__/settings.cpython-36.pyc
 create mode 100644 django/my_site/my_site/__pycache__/urls.cpython-36.pyc
 create mode 100644 django/my_site/my_site/__pycache__/wsgi.cpython-36.pyc
 create mode 100644 django/test_push.txt

第四步:git fetch --all拉取當前分支最新代碼(如果是多人同時開發,類似SVN提交前先update)

D:\xuexi\python\django (master)
>>> git fetch --all
Fetching origin

注意: git fetch --all(只是下載代碼到本地,不進行合並操作)git pull(下載並覆蓋本地代碼)

第五步:push到遠程master分支上git push origin master

D:\xuexi\python\django (master)
>>> git push origin master
Enumerating objects: 15, done.
Counting objects: 100% (15/15), done.
Delta compression using up to 4 threads
Compressing objects: 100% (9/9), done.
Writing objects: 100% (11/11), 2.89 KiB | 422.00 KiB/s, done.
Total 11 (delta 2), reused 0 (delta 0)
remote: Resolving deltas: 100% (2/2), completed with 2 local objects.
To https://github.com/weibgg/django_blog.git
   369f642..d1ee53c  master -> master

執行到這里不出意外在github就能看到更新后的代碼了

常見錯誤

因為項目的變更需要同步的github項目地址變動,如下面示例項目地址已修改找不到路徑,需要同步變更地址最簡單的辦法刪除項目中的.get文件夾重新配置遠程倉庫,也可以查看此教程 git 修改遠程倉庫地址詳解

D:\xuexi\python (master)
>>> git push origin master
remote: Repository not found.
fatal: repository 'https://github.com/weibgg/test.git/' not found

出現如下報錯:

>>> git push origin master
To https://github.com/weibgg/django.git
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://github.com/weibgg/django.git'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解決參考

出現如下提示:

>>> git add *
warning: LF will be replaced by CRLF in .idea/dataSources/6c0c039e-d9ac-4487-9fc3-ffda4f80776b.xml.
The file will have its original line endings in your working directory
warning: LF will be replaced by CRLF in .idea/workspace.xml.
The file will have its original line endings in your working directory

原因:在非項目文件執行了更新操作

跳轉至頂部


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM