[软件构造] Git 提交本地java工程到GitHub仓库的两种方法

[软件构造] Git 提交本地java工程到GitHub仓库

方法一 eclipse 自带插件

1.1 新建本地工程

为演示,新建一个工程文件TestGitUseEclipse,在src目录下新建HelloWorld的一个.java文件,在此类中调用main函数打印"Hello World!"字样。

1.2 GitHub新建同名仓库

1.3 创建本地仓库

1.4 Push & Commit操作

点击Commit后,侧边出现Git Staging弹窗:

无伤大雅的报错:

原因是未将此项目连接到远程仓库就想Push到远程仓库,故接下来将此项目连接到远程仓库就行。

此时Git Staging窗口原本的Commit and Push 按钮变成 Push HEAD按钮,点击即可将项目连接到远程仓库。

弹出此窗口:

去GitHub此仓库复制SSH链接(之前我已配好了SSH公钥),粘贴后点Preview。

再次点击Preview。

点Push。

上传成功:

GitHub仓库可看到提交的项目:

方法二 Git Bash

此方法演示过程中,将本地仓库位置和eclipse工程文件位置分开了(遵循了之前eclipse的建议)。

2.1 新建本地工程

2.2 GitHub 新建仓库

2.3 将工程放入本地仓库中

右键工程,Team,Share Project,在弹窗点击下列按钮,

2.4 Git Bash 中的系列操作

查看代码
bash: /c/Users/Matrix/.profile: is a directory

Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$ git status
On branch master

No commits yet

Untracked files:
  (use "git add <file>..." to include in what will be committed)
        TestGitUseBash/

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

Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$ git add .
warning: LF will be replaced by CRLF in TestGitUseBash/bin/.gitignore.
The file will have its original line endings in your working directory

Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$ git status
On branch master

No commits yet

Changes to be committed:
  (use "git rm --cached <file>..." to unstage)
        new file:   TestGitUseBash/.classpath
        new file:   TestGitUseBash/.project
        new file:   TestGitUseBash/.settings/org.eclipse.jdt.core.prefs
        new file:   TestGitUseBash/bin/.gitignore
        new file:   TestGitUseBash/src/HelloWorld.java


Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$ git commit -m "This is a repository for testing uploading projects using Git Bash."
[master (root-commit) 3be7d22] This is a repository for testing uploading projects using Git Bash.
 5 files changed, 50 insertions(+)
 create mode 100644 TestGitUseBash/.classpath
 create mode 100644 TestGitUseBash/.project
 create mode 100644 TestGitUseBash/.settings/org.eclipse.jdt.core.prefs
 create mode 100644 TestGitUseBash/bin/.gitignore
 create mode 100644 TestGitUseBash/src/HelloWorld.java

Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$ git push
fatal: No configured push destination.
Either specify the URL from the command-line or configure a remote repository using

    git remote add <name> <url>

and then push using the remote name

    git push <name>


Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$ git push git@github.com:waibiwaiba/TestGitUseBash.git
Enumerating objects: 11, done.
Counting objects: 100% (11/11), done.
Delta compression using up to 12 threads
Compressing objects: 100% (7/7), done.
Writing objects: 100% (11/11), 1.39 KiB | 473.00 KiB/s, done.
Total 11 (delta 0), reused 0 (delta 0), pack-reused 0
To github.com:waibiwaiba/TestGitUseBash.git
 * [new branch]      master -> master

Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$ git status
On branch master
nothing to commit, working tree clean

Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$ ^C

Matrix@LAPTOP-721O1CLU MINGW64 /d/CODE/git/repository (master)
$

在GitHub可看到仓库中已有工程文件。

 

 

 

 

 

posted @ 2022-05-05 17:44  Matrix_250  阅读(144)  评论(0编辑  收藏  举报