Pycharm同步本地代码至GitHub

版权声明:编写不易,可看不可转,请知悉! https://blog.csdn.net/zha6476003/article/details/83052032

注册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 "[email protected]"

就是注册时输入的这两个内容,如下图:

4、 生成key

ssh-keygen -t rsa -C "[email protected]"

连续按三次回车,设置了密码为空,并且创建了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 [email protected]

出现类似提示就代表成功了: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

原因:在非项目文件执行了更新操作

跳转至顶部

猜你喜欢

转载自blog.csdn.net/zha6476003/article/details/83052032