本地代码首次git提交到远程仓库

场景:本地新建了一个工程文件,写好代码以后,如何首次提交到github?

在Github上新建repo,比如说vagrant-xwiki 

本地新建同名目录(vagrant-xwiki),编写完成代码文件,想用git提交到github,运行git remote -v报错:fatal: not a git repository (or any of the parent directories): .git

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki
$ git remote -v
fatal: not a git repository (or any of the parent directories): .git

原因:项目不是一个git仓库,缺少.git文件

解决方法:初始化本地仓库,把代码的文件夹变成一个仓库,使用git init命令

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki
$ git init
Initialized empty Git repository in D:/sandbox/code/vagrant-xwiki/.git/

添加远程仓库,仓库URL可通过Github查看

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (master)
$ git remote add origin git@github.com:bin9wei/vagrant-xwiki.git

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (master)
$ git remote -v
origin  git@github.com:bin9wei/vagrant-xwiki.git (fetch)
origin  git@github.com:bin9wei/vagrant-xwiki.git (push)

运行git pull拉取代码发现报错,原来是Github默认分支叫main,不叫master。

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (master)
$ git pull
remote: Enumerating objects: 4, done.
remote: Counting objects: 100% (4/4), done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 4 (delta 0), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (4/4), 1.26 KiB | 107.00 KiB/s, done.
From github.com:bin9wei/vagrant-xwiki
 * [new branch]      main       -> origin/main
There is no tracking information for the current branch.
Please specify which branch you want to merge with.
See git-pull(1) for details.

    git pull <remote> <branch>

If you wish to set tracking information for this branch you can do so with:

    git branch --set-upstream-to=origin/<branch> master

所以新建一个main分支。这时可以正常pull

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (master)
$ git checkout main
Switched to a new branch 'main'
Branch 'main' set up to track remote branch 'main' from 'origin'.

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git pull
Already up to date.

git add需要提交的文件

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git status
On branch main
Your branch is up to date with 'origin/main'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        .gitignore
        Vagrantfile
        apache-tomcat-9.0.69.tar.gz
        hibernate.cfg.xml
        pg_hba.conf
        provision.sh
        xwiki-platform-distribution-war-13.10.10.war

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

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git add .gitignore

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git add Vagrantfile

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git add hibernate.cfg.xml

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git add pg_hba.conf

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git add provision.sh

git commit 带上有意义的commit message,说明你这个改动是什么

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git commit -m "init box"
[main ce0af91] init box
 5 files changed, 438 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 Vagrantfile
 create mode 100644 hibernate.cfg.xml
 create mode 100644 pg_hba.conf
 create mode 100644 provision.sh

最后git push

bin9w@LAPTOP-ODF35RAF MINGW64 /d/sandbox/code/vagrant-xwiki (main)
$ git push
Enumerating objects: 8, done.
Counting objects: 100% (8/8), done.
Delta compression using up to 8 threads
Compressing objects: 100% (6/6), done.
Writing objects: 100% (7/7), 5.60 KiB | 2.80 MiB/s, done.
Total 7 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:bin9wei/vagrant-xwiki.git
   7630ec0..ce0af91  main -> main

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值