将本地项目通过git管理:
cd 项目路径,执行:
git init
会在项目路径下生成.git目录,进入后 会看到新生成的文件和目录:
wangxiaodeMacBook-Pro:.git wangxiao$ ls
HEAD config description hooks info objects refs
初使化的动作执行一次就可以,若不想再通过git管理项目,直接删除掉.git目录就可以了。
linux操作系统下.文件夹不可见,可通过 open .git 打开文件夹了:
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.DS_Store
edu-common-config/
edu-common-core/
edu-common-parent/
edu-common-web/
edu-common/
edu-facade-user/
edu-service-user/
edu-web-boss/
"\345\210\235\345\247\213\345\214\226\350\204\232\346\234\254(MySQL).sql"
nothing added to commit but untracked files present (use "git add" to track)
接下来使用 git add 文件名来添加待提交的文件, git add . 表示添加所有待提交的文件
然后git status查看项目状态
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
new file: .DS_Store
new file: edu-common-config/.DS_Store
new file: edu-common-config/.classpath
new file: edu-common-config/.project
new file: edu-common-config/.settings/org.eclipse.core.resources.prefs
new file: edu-common-config/.settings/org.eclipse.jdt.core.prefs
new file: edu-common-config/.settings/org.eclipse.m2e.core.prefs
new file: edu-common-config/.settings/org.eclipse.wst.common.component
使用 git commit -m '注释' 提交代码,注释用来说明提交代码的功能
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git commit -m 'Dubbo实践项目'
[master (root-commit) 47a3011] Dubbo实践项目
951 files changed, 57796 insertions(+)
create mode 100644 .DS_Store
create mode 100644 edu-common-config/.DS_Store
create mode 100644 edu-common-config/.classpath
create mode 100644 edu-common-config/.project
create mode 100644 edu-common-config/.settings/org.eclipse.core.resources.prefs
create mode 100644 edu-common-config/.settings/org.eclipse.jdt.core.prefs
create mode 100644 edu-common-config/.settings/org.eclipse.m2e.core.prefs
create mode 100644 edu-common-config/.settings/org.eclipse.wst.common.component
create mode 100644 edu-common-config/.settings/org.eclipse.wst.common.project.facet.core.xml
create mode 100644 edu-common-config/pom.xml
这样就完成了第一次的提交,现在工作目录是干干净的,没有什么可提交的。
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git status
On branch master
nothing to commit, working tree clean
git log查看提交版本和日志
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git log
commit 47a30116aa22a39b114987b8b75cfd7c16a7aa9d
Author: 王晓 <wangxiao.xiw@gmail.com>
Date: Tue Jun 27 16:23:18 2017 +0800
Dubbo实践项目
当我们修改一个文件后,
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git status
On branch master
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: edu-common-parent/pom.xml
no changes added to commit (use "git add" and/or "git commit -a")
我们想查看一下文件修改前修改后的一个区别,可以使用 git diff命令
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git diff
diff --git a/edu-common-parent/pom.xml b/edu-common-parent/pom.xml
index e1f5f86..d344254 100755
--- a/edu-common-parent/pom.xml
+++ b/edu-common-parent/pom.xml
@@ -10,12 +10,6 @@
<name>edu-common-parent</name>
<url>http://maven.apache.org</url>
- <!-- <distributionManagement> <repository> <id>nexus-releases</id> <name>Nexus
- Release Repository</name> <url>http://192.168.100.66:8081/nexus/content/repositories/releases/</url>
- </repository> <snapshotRepository> <id>nexus-snapshots</id> <name>Nexus Snapshot
- Repository</name> <url>http://192.168.100.66:8081/nexus/content/repositories/snapshots/</url>
- </snapshotRepository> </distributionManagement> -->
-
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
对于git来说是一行一行对比的,可以看到我们删除了几行注释,下面使用git add 添加修改。
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git add .
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git commit -m '删除pom无效注释'
[master d9dee74] 删除pom无效注释
1 file changed, 6 deletions(-)
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git status
On branch master
nothing to commit, working tree clean
远程
我们可以为项目在远程服务器上创建一个版本库,再把本地推送到远程,好处是本地版本整丢了,可以去远程恢复,还可以让其它人协作开发。
我们可以使用github创建免费公共的版本库,但是github国内访问慢,我们改用国内github码云git.oschina.net创建也是一样的。
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git remote add origin https://git.oschina.net/xiw/edu-dubbo-demo.git
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git push -u origin master
To https://git.oschina.net/xiw/edu-dubbo-demo.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'https://git.oschina.net/xiw/edu-dubbo-demo.git'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git remote -v
origin https://git.oschina.net/xiw/edu-dubbo-demo.git (fetch)
origin https://git.oschina.net/xiw/edu-dubbo-demo.git (push)
push之前 需要先pull更新同步一把
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git pull --rebase origin master
warning: no common commits
remote: Counting objects: 5, done.
remote: Compressing objects: 100% (4/4), done.
remote: Total 5 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (5/5), done.
From https://git.oschina.net/xiw/edu-dubbo-demo
* branch master -> FETCH_HEAD
* [new branch] master -> origin/master
First, rewinding head to replay your work on top of it...
Applying: Dubbo实践项目
Applying: 删除pom无效注释
再同步代码到远程分支能正常了
wangxiaodeMacBook-Pro:基础篇--第01节--使用Dubbo对传统工程进行服务化改造的思路介绍--工程源码 wangxiao$ git push -u origin master
Counting objects: 1106, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (922/922), done.
Writing objects: 96% (1062/1106), 27.77 MiB | 3.27 MiB/s