git init / git clone
作業用ディレクトリを作成して初期化を行う。
mkdir gitdir
cd gitdir
git init
Initialized empty Git repository in gitdir
gitdir /.git が作成される。
mkdir dir1
cd dir1
git clone gitrepo
Cloning into gitrepo...
done.
dir1 / gitrepo / gitrepo.git が作成される。
git init --separate-git-dir <git dir>
git init --template=<repository>
git init -q
git init --bare
git clone --bare <repository>
git clone --no-hardlinks <repository>
git add / git mv /git rm / git reset
git branch
create branches
git branch branchname
master branch の存在しない状態では
git init
git branch branchname
fatal: Not a valid object name: 'master'.
と失敗する. 仕方がないので, commit して, master branch を作成しようとしたが,
git commit -m init
# On branch master
#
# Initial commit
#
nothing to commit (create/copy files and use "git add" to track)
となり, 有意の commit にならない.
git branch branchname
git checkout branchname
git checkout -b branchname
list branches
Branch の一覧を表示するには,
git branch
* branch1
branch2
branch3
とすればよい. 現在の branch は * で指示される.
Remote の branch も含めて表示するには,
git branch -a
* branch1
branch2
branch2
remote1/branch1
remote1/branch2
remote2/branch3
delete branches
git branch -d branchname
git checkout
Examples
Case: when switching branches.
git branch branch1
git branch branch2
branch1 と branch2 を用意. Branch の切替により, file がどう変化するかを列挙.
file11 is commited in branch1.
branch1 で file11 を作成し, commit する.
git checkout branch1
echo "made in branch1" > file11
git add file11
git commit -m "file11 is commited in branch1"
Created commit f62455c: file11 is commited in branch1
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 file11
branch2 に切替える.
git checkout branch2
- file11 は branch2 に登録されていないので削除される.
branch1 に切替える.
git checkout branch1
- file11 は branch1 に登録されているので再度作成される.
file11 is modified in branch1.
branch1 に切替えて, file11 を編集する.
git checkout branch1
echo "modified in branch1" >> file11
branch2 に切替える.
git checkout branch2
error: You have local changes to 'file11'; cannot switch branches.
file11 を HEAD に登録し, branch2 に切替える.
git add file11
git checkout branch2
error: Entry 'file11' would be overwritten by merge. Cannot merge.
file11 を commit し, branch2 に切替える.
git commit -m "file11 is modified in branch1"
Created commit 3a06f4a: file11 is modified in branch1
1 files changed, 1 insertions(+), 0 deletions(-)
git checkout branch2
- branch2 においては file11 は未登録なので削除される.
file12 is added to HEAD in branch1.
branch1 で file12 を作成し, HEAD に登録する.
git checkout branch1
echo "made in branch1" > file12
git add file12
branch2 に切替える.
git checkout branch2
A file12
Switched to branch "branch2"
この場合, file12 は branch1 にも branch2 にも登録されていないので, checkout の影響を受けない.
branch1 に切替える.
git checkout branch1
A file12
Switched to branch "branch1"
file12 is modified in branch1.
branch1 で file12 を編集する.
git checkout branch1
echo "modified in branch1" >> file12
file12 is modified in branch2.
branch2 で file12 を編集する.
git checkout branch2
echo "modified in branch2" >> file12
git checkout branch1
A file12
Switched to branch "branch1"
branch2 における変更を保持したまま branch1 に移る.
どちらの branch であれ, commit してしまえば
file13 is made in branch1 and commited in branch2.
branch2 でfile13 を commit.
git add file12 file13
git commit file13 -m branch1-commit1
この状態で branch2 に切替えると,
file13 は消えて, file12 は残る.
branch2 にて file_1-4 を作成し, branch1 に戻る.
echo "made in branch2" > file_2-1
git checkout branch1
問題ない.
今度は branch2 にて file13 を作成し, branch1 に戻る.
git checkout branch2
echo "made in branch2" > file13
git checkout branch1
error: Untracked working tree file 'file13' would be overwritten by merge.
git add file13
git checkout branch1
error: Entry 'file_1-3' would be overwritten by merge. Cannot merge.
git commit file13 -m branch2-commit1
git checkout branch1
git checkout branch2
echo "edited in branch2" >> file12
git checkout branch1
問題ない. 編集結果も残る.
Case: between two working trees.
wt1 wt2 二つの working trees を用意して, それぞれを git directory にする.
mkdir wt1
mkdir wt2
cd wt1
git init
cd ../wt2
git init
git pull wt1 branch1-1
Branch 間の切替に関して.