github新建Repositories后具体操作步骤

添加用户信息

第一个要配置的是你个人的用户名称和电子邮件地址。这两条配置很重要,每次 Git 提交时都会引用这两条信息,说明是谁提交了更新,所以会随更新内容一起被永久纳入历史记录:

      
      
1
2
      
      
git config --global user.name "用户名"
git config --global user.email "你的Email"

生成SSH密钥并添加到github

查看是否已经有了ssh密钥:cd ~/.ssh。如果没有密钥则不会有此文件夹,有则备份删除。

ssh-keygen -t rsa -C “你的注册邮箱”

按3个回车,密码为空。最后得到了两个文件:id_rsa和id_rsa.pub,添加id_rsa.pub中的所有到github中的SSH Keys中。

新建工作目录,并初始化本地新仓库

      
      
1
2
3
4
      
      
git init
echo "# This is my README" >> README.md //建立README.md文件并且内容是# This is my README
git add . //把所有文件加入到索引(不想把所有文件加入,可以用gitignore或 add 具体文件)
git commit - m "First Commit. Adding a README." //提交到本地仓库,然后会填写更新日志( - m “更新日志”也可)

添加远程仓库并推送数据到远程仓库

  • 要添加一个新的远程仓库,可以指定一个简单的名字,以便将来引用,运行 git remote add [shortname] [url]

如下就是添加的名字是origin的仓库,一般默认的都是这个。

      
      
1
      
      
remote add origin git @github.com:zhongyouhuiwu/ceshi.git
  • 本地仓库中的数据推送到远程仓库。实现这个任务的命令很简单:git push [remote-name] [branch-name]

如果要把本地的 master 分支推送到 origin 服务器上(再次说明下,克隆操作会自动使用默认的 master 和 origin 名字),可以运行下面的命令:

大专栏 github新建Repositories后具体操作步骤

之后就是重复上述操作(排除init和romote的操作)

其他常用命令

  • 更新项目(新加了文件):
<  /table>
      
      
1
      
      
git push -u origin master
      
      
1
2
3
4
      
      
cd ~/hello-world
git add .
git commit //提交到本地仓库
git push origin master //不是新创建的,不用再add 到remote上了
  • 更新项目(没新加文件,只有删除或者修改文件):
      
      
1
2
3
      
      
cd ~/hello-world
git commit - a //记录删除或修改了哪些文件
git push origin master //提交到github
  • 忽略一些文件,比如*.o等:
      
      
1
2
3
      
      
cd ~/hello-world
vim .gitignore //把文件类型加入到.gitignore中,保存
然后就可以git add . 能自动过滤这种文件
  • clone代码到本地:
      
      
1
2
3
4
      
      
git clone git @github.com:zhongyouhuiwu/ceshi.git
假如本地已经存在了代码,而仓库里有更新,把更改的合并到本地的项目:
git fetch origin //获取远程更新
git merge origin/master //把更新的内容合并到本地分支
  • 撤销

git reset

  • 删除

git rm * // 不是用rm

常见错误

1.$ git remote add origin [email protected]:zhongyouhuiwu/ceshi.git

错误提示:fatal: remote origin already exists.

解决办法:$ git remote rm origin

扫描二维码关注公众号,回复: 8180562 查看本文章

然后在执行:$ git remote add origin [email protected]:zhongyouhuiwu/ceshi.git 就不会报错误了

  1. $ git push origin master

错误提示:error:failed to push som refs to

解决办法:$ git pull origin master //先把远程服务器github上面的文件拉先来,再push 上去。

猜你喜欢

转载自www.cnblogs.com/lijianming180/p/12037789.html