使用Git管理项目

作者ChihMinh,原作链接https://chihminh.github.io/2016/07/19/git-manage/,转载请注明出处

将项目chatroom push到GitHub仓库中去。Git文件有三种状态,已修改、已暂存、已提交,Git工作流程:
a. 在工作目录中修改某些文件。
b. 对修改后的文件进行快照,然后保存到暂存区域。
c. 提交更新,将保存在暂存区域的文件快照永久转储到 Git 目录中。
git的本地操作如下图:

1. 初始化本地仓库

在工作目录(即项目目录)中初始化新的本地仓库,执行

1
2
$ git init
Initialized empty Git repository in C:/Users/TMing/workspace/chatroom/.git/

2. 跟踪文件

在工作目录中初始化本地仓库的话,本地仓路是没有跟踪任何文件的;如果是直接克隆的现有仓库,那么状态如下:

1
2
3
$ git status
On branch master
nothing to commit, working directory clean

查看文件状态

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
$ git status
On branch master
Initial commit
Untracked files:
(use "git add <file>..." to include in what will be committed)
.classpath
.gitignore
.project
.settings/
pom.xml
src/
nothing added to commit but untracked files present (use "git add" to track)

需要跟踪当前目录的所有文件,那么输入

1
$ git add .

“.”表示当前目录,之后输入

1
2
3
4
5
6
7
8
$ git status
On branch master
Initial commit
Changes to be committed:
(use "git rm --cached <file>..." to unstage)
……

可以看到所有文件状态变为已暂存。

3. 首次提交到本地仓库

继上面操作后

1
$ git commit -m "first commit version"

4. push到远程仓库

添加远程仓库
运行 git remote add [shortname] [url]:

1
2
3
4
$ git remote add chatroom git@github.com:ChihMinh/chatroom.git
$ git remote -v
chatroom git@github.com:ChihMinh/chatroom.git (fetch)
chatroom git@github.com:ChihMinh/chatroom.git (push)

将项目push到远程仓库的master分支

1
2
3
4
5
6
7
8
9
$ git push chatroom master
To git@github.com:ChihMinh/chatroom.git
! [rejected] master -> master (fetch first)
error: failed to push some refs to 'git@github.com:ChihMinh/chatroom.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.

出错原因:之前已经用Eclipse提交过一次到该远程仓库,所以需要先pull到本地仓库,合并之后再push

1
2
3
4
5
$ git pull chatroom master
From github.com:ChihMinh/chatroom
* branch master -> FETCH_HEAD
Already up-to-date!
Merge made by the 'recursive' strategy.

注:Pull = fetch + merge再次push,结果ok

1
2
3
4
5
6
7
8
$ git push chatroom master
Counting objects: 170, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (148/148), done.
Writing objects: 100% (170/170), 3.81 MiB | 392.00 KiB/s, done.
Total 170 (delta 13), reused 0 (delta 0)
To git@github.com:ChihMinh/chatroom.git
808459b..2d9b8a2 master -> master

5. 修改后提交

新增README.md文件

1
2
3
4
5
6
7
8
$ git status
On branch master
Untracked files:
(use "git add <file>..." to include in what will be committed)
README.md
nothing added to commit but untracked files present (use "git add" to track)

跟踪该文件

1
2
3
4
5
6
7
$ git add README.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
new file: README.md

(说明该文件已加入暂存区,直接提交即可)

1
2
3
4
5
6
7
$ git commit -m "new add readme.md"
[master 43e3a92] new add readme.md
1 file changed, 1 insertion(+)
create mode 100644 README.md
$ git status
On branch master
nothing to commit, working directory clean

修改README.md文件

1
2
3
4
5
6
7
8
9
$ 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: README.md
no changes added to commit (use "git add" and/or "git commit -a")

(上述说明修改文件尚未加入暂存区域)
把已修改文件加入暂存区域

1
2
3
4
5
6
7
$ git add README.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD <file>..." to unstage)
modified: README.md

然后提交就可以啦

6. 跳过暂存直接commit

commit命令只会提交加入暂存区域文件到本地仓库,给git commit加上- a选项就可以把所有已跟踪的文件暂存起来一并提交,从而跳过git add 步骤。

未完待续……