Add an existing project to GIT repository


我想把 local 的一個 project source code 放到 git local / remote repository. 這邊 remote 我用的是 bitbucket. 在開始之前,先登入到 bitbucket 建立新的 Repository,如下圖:

Create repository in bitbucket

我建立的是一個 private repository, 可以在 repository -> wiki 找到 git URL.

接下來就是開始如在 local 建立 git repository,然後再把它 sync 到 remote.


步驟

在 local 建立目錄,把 source code 放到目錄

  1. 將原始擋複製到新的目錄 (如果原本的原始檔目錄下沒有 .git,那麼可以省略這個部分)
1
2
~$ mkdir ~/Repository/Git/ezdao
~$ cp -R ${SOURCE_CODE} ~/Repository/Git/bitbucket/ezdao

初始化 / 建立本地儲存庫 (Local Repository)

  1. 切換到將要建立 git repos 的目錄下
  2. 輸入 git init 初始本地儲存庫 (Local Repository),他會在本地端建立隱藏目錄 .git
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
~$ cd ~/Repository/Git/bitbucket/ezdao
~$ git init
Initialized empty Git repository in /Users/rick/Repository/Git/bitbucket/ezdao/.git/
~$ ls -al
total 48
drwxr-xr-x 14 rick staff 476 3 24 22:12 .
drwxr-xr-x 5 rick staff 170 3 24 22:11 ..
-rw-r--r-- 1 rick staff 368 3 24 22:10 .classpath
drwxr-xr-x 13 rick staff 442 3 24 22:18 .git
-rw-r--r-- 1 rick staff 24 3 24 22:10 .gitignore
-rw-r--r-- 1 rick staff 364 3 24 22:10 .project
drwxr-xr-x 4 rick staff 136 3 24 22:10 .settings
-rw-r--r-- 1 rick staff 89 3 24 22:10 README.md
drwxr-xr-x 3 rick staff 102 3 24 22:10 bin
-rw-r--r-- 1 rick staff 910 3 24 22:10 build.xml
drwxr-xr-x 3 rick staff 102 3 24 22:10 lib
drwxr-xr-x 3 rick staff 102 3 24 22:10 resource
-rw-r--r-- 1 rick staff 787 3 24 22:10 settings.properties.template
drwxr-xr-x 3 rick staff 102 3 24 22:10 src
~$

上述步驟,將會在 local 建立好 git 儲存庫 (Repository)。

新增遠端儲存庫 (Remote Repository)

  1. 檢查目前專案的 remote repos: git remote -v,會看到剛初始的儲存庫是沒有關聯到任何遠端儲存庫的。
  2. 接下來使用 git remote add ${REPOS_NAME} ${REMOTE_REPOS_URL} 建立遠端儲存庫。
  • ${REPOS_NAME}: 是遠端儲存庫的名稱,這邊例子是 origin
  • ${REMOTE_REPOS_URL}: 是遠端儲存庫的位址,它的結尾會是 .git
  1. 建立完成後,使用 git remote -v 可以看到關聯起來,一個是 fetch,一個是 push

如果還有使用其他的遠端儲存庫,像是 github,也可以一並加入。

1
2
3
4
5
6
7
~$ git remote -v
~$ git remote add origin https://rick_kyhwang@bitbucket.org/rick_kyhwang/ezdao.git
~$ git remote -v
origin https://rick_kyhwang@bitbucket.org/rick_kyhwang/ezdao.git (fetch)
origin https://rick_kyhwang@bitbucket.org/rick_kyhwang/ezdao.git (push)
~$

將專案放到本地儲存庫 (Local Repository)

  • 檢查目前狀態,指令:git status。這邊有幾個重要的訊息要注意:
    • 第一行會顯示目前的分支 (branch),預設是 master,有點類似 SVN 的 Trunk 意思。
    • Untracked files:顯示目前還沒有 commit 到儲存庫的檔案

git status

  • 把還沒有 commit 的檔案標記為 add:git add .,要注意最後是打一個點,表示目前位置。

git_add.png

  • commit 這些檔案:git commit -m 'init'
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
rick@iWish-3:22:18:09 ezdao$ git commit -m "init"
[master (root-commit) fa2f54f] init
26 files changed, 2621 insertions(+)
create mode 100644 .classpath
create mode 100644 .gitignore
create mode 100644 .project
create mode 100644 .settings/org.eclipse.core.resources.prefs
create mode 100644 .settings/org.eclipse.jdt.core.prefs
create mode 100644 README.md
create mode 100644 build.xml
create mode 100644 lib/mysql-connector-java-5.1.21.jar
create mode 100644 resource/daoTemplate/Dao.template
create mode 100644 resource/daoTemplate/Entity.template
create mode 100644 settings.properties.template
create mode 100644 src/com/gtcafe/ezdao/BaseEntity.java
create mode 100644 src/com/gtcafe/ezdao/DefaultEntity.java
create mode 100644 src/com/gtcafe/ezdao/Entity.java
create mode 100644 src/com/gtcafe/ezdao/IDao.java
create mode 100644 src/com/gtcafe/ezdao/IEntity.java
create mode 100644 src/com/gtcafe/ezdao/codegen/DaoCreator.java
create mode 100644 src/com/gtcafe/ezdao/codegen/EntityCreator.java

rick@iWish-3:22:18:14 ezdao$

這樣就把檔案都 commit 到本地儲存庫了

將本地儲存庫 push 到遠端儲存庫

指令:git push ${REPOS_NAME} ${BRANCH}

  • ${REPOS_NAME}: 在新增遠端儲存庫時給與的名字,這邊用的名稱是 origin
  • ${BRANCH}: 分支的名稱,git 預設的名稱是 master
1
2
3
4
5
6
7
8
9
~$ git push origin master
Counting objects: 41, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (36/36), done.
Writing objects: 100% (41/41), 791.47 KiB | 0 bytes/s, done.
Total 41 (delta 2), reused 0 (delta 0)
To https://rick_kyhwang@bitbucket.org/rick_kyhwang/ezdao.git
* [new branch] master -> master
~$

在回到遠端儲存庫就會看到檔案已經都上去了,如下圖:
remote_repos.png

如果根目錄有 README.md,將會被系統自動展開,放在 Overview。 .md 表示 Markdown 語法的 README

Relevant Documents

Reference



Comments

  • 全站索引
  • 關於這裏
  • 關於作者
  • 學習法則
  • 思考本質
  • 一些領悟
  • 分類哲學
  • ▲ TOP ▲