如何使用github上传与删除代码

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Dj_Fairy/article/details/84870539

最近用到了github上传代码也是跟着别人的教程学的,记载下来以防以后忘记

原链接:http://www.cnblogs.com/cxk1995/p/5800196.html

具体READ.md里面的教程参考https://blog.csdn.net/kaitiren/article/details/38513715

1、首先注册github账号,到github首页新建一个项目

点击右上角的+号 =》点击new repository =》进入下面这个页面

2、跟着上面的步骤创建完repository就会在你的账号中看到项目了

3、本地操作,下载一个git(网上一搜会特别多),然后出现一个git bash可视化工具,点开个git bash,

具体可以看廖雪峰老师写的git,很详细。

下面是一个配置账号的小结

ssh -key配置

1)、打开git bash,进入~路径下,必须保证当前路径在~路径下

2)、在git命令行敲击 ssh-keygen -t  rsa -C "[email protected]" //建议写自己真实有效的邮箱地址。注意:在敲代码是不要将双引号也敲击进去。

3)、

Enter file in which to save the key (/c/Users/xxxx_000/.ssh/id_rsa):   //此时我们什么都不需要操作,直接回车就好

Enter passphrase (empty for no passphrase):  //此时要你输入码(可以为空,直接回车就好,也可以输入你的密码,这个           密码在你最后把本地资源推送到github上面的时候回会让你填写密码,此时密码隐藏,你输入进去是看不到的)

Enter same passphrase again: //再次确认密码(如果你第一次有输入密码,这次就再输一次,如果没有直接回车就行了)

直到看到下面这些代码
Your identification has been saved in /c/Users/xxxx_000/.ssh/id_rsa. //生成的密钥
Your public key has been saved in /c/Users/xxxx_000/.ssh/id_rsa.pub. //生成的公钥
The key fingerprint is:e3:51:33:xx:xx:xx:xx:xxx:61:28:83:e2:81 [email protected]
*本机已完成ssh key设置,其存放路径为:c:/Users/xxxx_000/.ssh/下。其中xxxx_000为你的用户名

4)、添加ssh key 到Github上

       首先登陆Github,点击右上角的“▼”→Settings→SSH kyes→Add SSH key。
  然后在打开c:/Users/xxxx_000/.ssh里面的id_rsa.pub文件,全选复制公钥内容
  也可以在git bush中的命令行输入cat ~/.ssh/id_rsa.pub,将得到公钥

  Title自定义,将公钥粘贴到GitHub中Add an SSH key的key输入框,最后“Add Key“

5)、配置账户  

git config --global user.name “your_username” #设置用户名
git config --global user.email “your_registered_github_Email” #设置邮箱地址(建议用注册giuhub的邮箱)

6)、测试ssh keys是否设置成功。

 ssh -T [email protected]

4、在git bash中进入一路cd进入自己要上传的文件路径,如下是我的文件路径

5、把github上面的仓库clone到本地,

具体地址在github上clone or download

在git bash里面输入git clone https://github.com/DFairy/shopping-cart.git (刚刚复制的地址),

出现下面的字样表示clone成功(ps:在git bash里面没有ctrl+c、ctrl+v快捷键,粘贴的话要点击鼠标右键)

6、接下来进入你刚刚clone下来的文件 cd <文件名>

把你要传的文件复制进去

7、依次输入下面的命令完成上传

git add .        (注:别忘记后面的.,此操作是把Test文件夹下面的文件都添加进来)

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

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

8、现在打开刚刚项目的就能看见内容了

9、创建分支

git checkout -b branchname(分支名字)      (直接新建一个分支然后切换至新创建的分支).就是创建加切换分支.

git checkout  branchname(分支名字)          切换分支

git push origin branchname                         将分支推送到github

git branch -r                                                   查看所有远程分支的名字(a是本地分支加远程分支)

git branch -d branchname                            删除本地分支,要切换到master才能删除

git push origin: branchname                        删除github上面的远程分支

git checkout master                                      从分支切换到主支

10、git指定不上传的文件

找到文件.gitignore文件,在里面加上文件名就好了

.DS_Store
node_modules
/dist

# local env files
.env.local
.env.*.local

# Log files
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# Editor directories and files
.idea
.vscode
*.suo
*.ntvs*
*.njsproj
*.sln
*.sw*

二、用github展示你的网页

比如下面这个网址https://dfairy.github.io/i-manage-system/#/login

1、创建分支gh-pages(方法如上git checkout -b gh-pages)

2、将要展示的页面复制到这个文件夹中

3、上传到github的分支(步骤同上)

4、在github页面切换分支可以看到要展示的内容

5、打开网址https://[你的github名字].github.io/[你的项目名字]就可以看到展示的内容了

三、创建完项目如何删除呢?

首先点击上方的settings

接着拖动页面到底部,点击delete this repository,再输入你要删除的项目名就好了。

       

四、关于github其他的一些小方法

1、如何在READ.md中添加预览图

下面是我的一个方法:

首先建立一个项目,准备存放所有我以后的img图库

按照上面的方法将img图加进img图库里

点击图片,选择download,将上面的地址拷贝下来

到刚刚要显示的READ.md里面加入下面这句话,就会显示图片了。(ps:![Image text]()这个是一定要加的,指的是如果图片不存在了,要显示的文字说明。)

![Image text](https://raw.githubusercontent.com/DFairy/img/master/draw-write.png?token=Ab37jB8lTDR0kvevIltxVj3spRLdk8LZks5cE1B5wA%3D%3D)
 

五、解决报错

1、我在提交的时候报了一个错误:

$ git push -u origin master
To https://github.com/DFairy/img.git
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://github.com/DFairy/img.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

解决方法:

1: 进行push前先将远程仓库pull到本地仓库
$ git pull --rebase origin master
$ git push -u origin master

2: 强制push本地仓库到远程 (这种情况不会进行merge, 强制push后远程文件可能会丢失 不建议使用此方法)
$ git push -u origin master -f

3: 避开解决冲突, 将本地文件暂时提交到远程新建的分支中
$ git branch [name]
# 创建完branch后, 再进行push
$ git push -u origin [name] 

2、尝试新增一个文件夹,push完之后显示如下:

Everything up-to-date
Branch 'master' set up to track remote branch 'master' from 'origin'.

原因:

也就是说一开始 git 服务器仓库是完全空的,
不包含任何一个分支(branch),因此刚开始 Push 时需要指定一个。

解决方法:

$ git remote -v
origin  https://github.com/DFairy/essay (fetch)
origin  https://github.com/DFairy/essay (push)
$ git branch
* master

之后就可以正常的commit 与push

3、我从分支切换到主支的时候报了下面这样的一个错

git checkout master
error: The following untracked working tree files would be overwritten by checkout:

原因:

通过错误提示可知,是由于一些untracked working tree files引起的问题。所以只要解决了这些untracked的文件就能解决这个问题。

解决方法:

git clean -d -fx

git clean -d -fx表示:删除 一些 没有 git add 的 文件;

git clean 参数    

 -n 显示将要删除的文件和目录;
 -x -----删除忽略文件已经对git来说不识别的文件
 -d -----删除未被添加到git的路径中的文件
 -f -----强制运行
 git clean -n
 git clean -df
 git clean -f

猜你喜欢

转载自blog.csdn.net/Dj_Fairy/article/details/84870539