将本地项目上传到Github

阳光穿透心脏的1/2处 2022-05-29 04:27 425阅读 0赞

下次不要再忘了

虽然一直都在使用Github,但是经常不常用命令行都容易忘记掉,特意在此进行一次记录。

1、在GitHub创建一个项目
![这里写图片描述][Image 1]

2、在本地文件夹中,做一次Git初始化

  1. ~/Aliyun/alioss 17:27:15
  2. $ git init
  3. Initialized empty Git repository in /Users/wangdong/Aliyun/alioss/.git/

3、将所有文件都添加到Git中

  1. ~/Aliyun/alioss on master! 17:27:24
  2. $ git add .

4、将 Git中的地址复制出来
![这里写图片描述][Image 1]
5、本地项目和远程的GitHub关联起来

  1. ~/Aliyun/alioss on master! 17:28:54
  2. $ git remote add xiongben-tongxue https://github.com/xiongben-tongxue/alioss-demo.git

6、现在如果执行git push的话,就会报错

  1. ~/Aliyun/alioss on master! 17:29:18
  2. $ git push
  3. fatal: No configured push destination.
  4. Either specify the URL from the command-line or configure a remote repository using
  5. git remote add <name> <url>
  6. and then push using the remote name
  7. git push <name>

7、那么看看状态吧

  1. ~/Aliyun/alioss on master! 17:31:06
  2. $ git status
  3. On branch master
  4. No commits yet
  5. Changes to be committed:
  6. (use "git rm --cached <file>..." to unstage)
  7. new file: .gitignore
  8. new file: .mvn/wrapper/maven-wrapper.jar
  9. new file: .mvn/wrapper/maven-wrapper.properties
  10. new file: conf/log4j.properties
  11. new file: mvnw
  12. new file: mvnw.cmd
  13. new file: pom.xml

8、需要先添加文件,再提交文件

  1. ~/Aliyun/alioss on master! 17:31:15
  2. $ git add -A
  3. ~/Aliyun/alioss on master! 17:31:19
  4. $ git commit -m "修复文件上传到github" [master (root-commit) 79e296b] 修复文件上传到github 25 files changed, 1708 insertions(+) create mode 100644 .gitignore create mode 100644 .mvn/wrapper/maven-wrapper.jar create mode 100644 .mvn/wrapper/maven-wrapper.properties create mode 100755 conf/log4j.properties create mode 100755 mvnw create mode 100644 mvnw.cmd create mode 100644 pom.xml

9、再git pull一下

  1. ~/Aliyun/alioss on master 17:31:39
  2. $ git pull
  3. warning: no common commits
  4. remote: Counting objects: 3, done.
  5. remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0
  6. Unpacking objects: 100% (3/3), done.
  7. From https://github.com/xiongben-tongxue/alioss-demo
  8. * [new branch] master -> origin/master
  9. There is no tracking information for the current branch.
  10. Please specify which branch you want to merge with.
  11. See git-pull(1) for details.
  12. git pull <remote> <branch>
  13. If you wish to set tracking information for this branch you can do so with:
  14. git branch --set-upstream-to=<remote>/<branch> master

10、分支关联

  1. ~/Aliyun/alioss on master 17:42:35
  2. $ git branch --set-upstream-to=origin/master master
  3. Branch master set up to track remote branch master from origin.

11、git pull报错

  1. ~/Aliyun/alioss on master 17:43:53
  2. $ git pull
  3. fatal: refusing to merge unrelated histories

12、git push报错

  1. ~/Aliyun/alioss on master 17:49:17
  2. $ git push
  3. To https://github.com/xiongben-tongxue/alioss-demo.git
  4. ! [rejected] master -> master (non-fast-forward)
  5. error: failed to push some refs to 'https://github.com/xiongben-tongxue/alioss-demo.git'
  6. hint: Updates were rejected because the tip of your current branch is behind
  7. hint: its remote counterpart. Integrate the remote changes (e.g.
  8. hint: 'git pull ...') before pushing again.
  9. hint: See the 'Note about fast-forwards' in 'git push --help' for details.

13、原因是git pull需要允许拉取在GitHub上创建的不相关联的文件

  1. ~/Aliyun/alioss on master 17:49:43
  2. $ git pull origin master --allow-unrelated-histories
  3. From https://github.com/xiongben-tongxue/alioss-demo
  4. * branch master -> FETCH_HEAD
  5. Merge made by the 'recursive' strategy.
  6. README.md | 2 ++
  7. 1 file changed, 2 insertions(+)
  8. create mode 100644 README.md

14、最后再Git push 就成功了

  1. ~/Aliyun/alioss on master 17:50:09
  2. $ git push
  3. Counting objects: 52, done.
  4. Delta compression using up to 8 threads.
  5. Compressing objects: 100% (40/40), done.
  6. Writing objects: 100% (52/52), 60.60 KiB | 8.66 MiB/s, done.
  7. Total 52 (delta 4), reused 0 (delta 0)
  8. remote: Resolving deltas: 100% (4/4), done.
  9. To https://github.com/xiongben-tongxue/alioss-demo.git
  10. a6023fd..b3c0fcc master -> master

[Image 1]:

发表评论

表情:
评论列表 (有 0 条评论,425人围观)

还没有评论,来说两句吧...

相关阅读

    相关 本地项目Github

    前言:注册GitHub有一段时间了,之前有按照教程操作过,不过后来一段时间不用又不熟练了。以至于后期突然使用时出现一些错误,导致本地文件一直提交不了。今天又熟悉了一次,这里做个