使用git将项目提交至Github

在此之前我已经在github上面新建了这个项目的仓库,现在介绍一下,怎样将本地的项目提交上去。


11_20170920173911_287.png
  • 在Terminal中输入下面的命令,初始化git
git init
E:\Dome\Java EE\fulin>git init
Initialized empty Git repository in E:/Dome/Java EE/fulin/.git/
  • 查看项目中已变化的文件
git status
E:\Dome\Java EE\fulin>git status
On branch master
 
Initial commit
 
Untracked files:
  (use "git add ..." to include in what will be committed)
 
        .gitignore
        README.md
        pom.xml
        src/
 
nothing added to commit but untracked files present (use "git add" to track)
  • 将所有的变更文件添加到git
git add .
  • 查看变更文件
E:\Dome\Java EE\fulin>git status
On branch master
 
Initial commit
 
Changes to be committed:
  (use "git rm --cached ..." to unstage)
 
        new file:   .gitignore
        new file:   README.md
        new file:   pom.xml
        new file:   src/main/webapp/WEB-INF/web.xml
        new file:   src/main/webapp/index.jsp
  • 添加提交,-m的意思是提交信息
E:\Dome\Java EE\fulin>git commit -m "first commit init project"
[master (root-commit) aae9e32] first commit init project
 5 files changed, 69 insertions(+)
 create mode 100644 .gitignore
 create mode 100644 README.md
 create mode 100644 pom.xml
 create mode 100644 src/main/webapp/WEB-INF/web.xml
 create mode 100644 src/main/webapp/index.jsp
  • 查看提交状态
E:\Dome\Java EE\fulin>git status
On branch master
nothing to commit, working tree clean
  • 将本地项目与github项目相关联
git remote add origin https://github.com/depers/fulin.git
  • 查看分支
E:\Dome\Java EE\fulin>git branch
* master
  • 提交代码到远程( 如果当前分支与多个主机存在追踪关系,则可以使用 -u 参数指定一个默认主机,这样后面就可以不加任何参数使用git push)
E:\Dome\Java EE\fulin>git push -u origin master
Counting objects: 11, done.
Delta compression using up to 8 threads.
Compressing objects: 100% (7/7), done.
Writing objects: 100% (11/11), 1.22 KiB | 0 bytes/s, done.
Total 11 (delta 0), reused 0 (delta 0)
To https://github.com/depers/fulin.git
 * [new branch]      master -> master
Branch master set up to track remote branch master from origin.
  • 有时,如果你在github新建项目的时候添加了README.md和.gitigore文件,初次提交是会报错的,所以
// 先拉取远程项目
get pull
 
// 如果还有问题,强制(Forced)推送。执行
git push -u -f origin master
  • 列出远程分支
E:\Dome\Java EE\fulin>git branch -r
  origin/master
  • 在现有分支上创建新的分支,checkout是检出,-b是新的分支
E:\Dome\Java EE\fulin>git checkout -b v1.0 origin/master
Switched to a new branch 'v1.0'
  • 将新的分支推送到仓库
E:\Dome\Java EE\fulin>git push origin HEAD -u
Total 0 (delta 0), reused 0 (delta 0)
To https://github.com/depers/fulin.git
 * [new branch]      HEAD -> v1.0
Branch v1.0 set up to track remote branch v1.0 from origin.

此时,我们的git初始化工作就完成了。


12_20170922205429_148.png

你可能感兴趣的:(使用git将项目提交至Github)