fork github to gitlab

git remote add upstream https://github.com/user/repo

Now you can fetch and pull from the upstream should there be any changes. (You can also push or merge to it if you have access rights.)

git pull upstream master

Finally, push back to your own GitLab repository:

git push origin master

用語集

項目説明備考
Fast Forwardブランチ分岐したものの、その間に分岐下のコミットがない場合はコミットをブランチの先頭に進める。これをFast Forwardというブランチの作業内容がわからなくなるので人によっては嫌がる
merge commitブランチマージ時に作成される。--no-ffをつけるとfast forwardできる場合でもmerge commitを作成
git flowブランチ戦略のコマンドツール

基本操作一覧

タグを指定してチェックアウトgit checkout -b TAGNAME refs/tags/TAGNAME
新規作成したブランチをpushgit push origin HEAD
最新のコミットだけclonegit clone --depth 1 URL
shallow cloneを元に戻すgit fetch --unshallow
shallow clonにするgit fetch --depth 1

github

無料プランの制約

privateリポジトリが無制限になったので個人開発には十分

SSH公開鍵認証にする。

ssh-keygen -t ras -C コメント

.ssh/configに以下のような設定を入れる

Personal access tokens

https://github.com/settings/tokens

二段階認証を設定した時にgit cloneなどの時に必要なアクセストークン。普遍にできるし、いつでも無効化できるので、環境変数などに設定しておく。macかつbrewでgitインストールしているとosxkeychainを介して初回のみアクセストークンいれればOK

TIPS

特定のコミット時点に戻す

git reset --hard コミットハッシュ

一部のディレクトリだけチェックアウト

一回全部持ってきてから設定で対象パスのみ記載するという方法。

git clone https://YOUR_GIT_URL
git config core.sparsecheckout true
echo "reservepath/reserve" > .git/info/sparse-checkout
git read-tree -m -u HEAD

HISTORYを持ってこない(ちょっと目的とは違うが、ディスク容量は軽くなる)

ブランチ間の差分でファイル名のみ

今いるブランチとリモートのnew_branchの差分表示

git diff origin/new_branch --name-only

subversionからの移行

さすがに使う機会が増えてきたのでなれるべく一部移行。下記ドキュメントが良い。

http://d.hatena.ne.jp/idesaku/20090323/1237825080

用語集

難解かつ耳慣れない言葉ばかり

bareリポジトリ管理用のファイルのみ、ワーキングディレクトリを持たないリポジトリ。
non-bareリポジトリワーキングディレクトリを持つ
mirrorリポジトリbareリポジトリのリモートリポジトリバージョン

インストール

CentOS6系だとあまりに古いバージョンが入るのでコンパイルしたほうが良いだろう。

コンパイル

#必要ライブラリインストール
yum -y install curl-devel expat-devel gettext-devel openssl-devel zlib-devel perl-ExtUtils-MakeMaker
#ソースダウンロード
wget https://www.kernel.org/pub/software/scm/git/git-2.4.10.tar.gz
tar zxvf git-2.4.10.tar.gz
cd git-2.4.10
# コンパイル
make prefix=/usr/local all
make prefix=/usr/local install

参考サイト

https://try.github.io/levels/1/challenges/1

新しいブランチを作ってコミットするまで

git checkout -b feature/mynewbranch
git push origin feature/mynewbranch -n
git push origin feature/mynewbranch

チェックアウト手順

リモートリポジトリ

git remote showリモートとの接続情報
git branch -vvリモートとの接続情報。こちらのほうがより詳細でわかりやすい

クローンする

git clone http://example.com/git 任意のディレクトリ名
cd 任意のディレクトリ名
git config --global http.sslVerify false

pullする

git pull origin testing

コマンド版 gitローカルリポジトリ作成

リポジトリには作業ディレクトリを含むものと含まないベアリポジトリがある。サーバー側はベアリポジトリにするが、ローカルはノンベアリポジトリにするべし

mkdir testproject
cd testproject
git init
echo "git test" > readme
git add .
git commit -m "first commit"
git remote add origin http://www.example.com/testproject.git
git push origin master
git clone http://example.com/hoge/git.git [hoge以外の名前を付けたい場合]
git clone http://ID:PW@example.com/git

イメージ的には以下構成となる。

DIR/hoge/.git →http://example.com/hoge/git.git
DIR/fuga/.git →http://example.com/fuga/git.git
cd xxx
git branch -r
git branch 対象ブランチ
git checkout 対象ブランチ
#一度にやる場合は下記
git checkout -b 対象ブランチ
git push --delete origin ブランチ名
git remote -v
origin: レポジトリの場所(URL)の別名
master: ブランチの名前

デフォルトの対象はorigin/masterだ。

git config --list

上記コマンドで今どこを示しているかを確認せよ。

git pull origin ブランチ名称
git remote add NEW_REMOTE git@bitbucket.org:xxxx/foo.git

コマンド版を使う場合の設定

[url "https://"]
        insteadOf = git://
[http]
        sslVerify = false
[user]
        email = username@example.com
        name = username
[remote "origin"]
        url = http://GIT_USER:GIT_PASSWORD@git.example.com/test.git

認証情報の保存

1.git config --global credential.helper store
2.git コマンドを実行してパスワードなどを入力
3.~/.git-credentialsが作成される

SVNとの違い

ローカルリポジトリ、index、リモートリポジトリと構成要素が複雑怪奇。ブランチがSVNより増える傾向がある。

addコミット準備領域であるindexに追加する
commitあくまでローカルリポジトリに対してのコミット。-aオプションをつけないと変更してもコミット対象にはならない
PUSHリモートリポジトリへのコミット。SVNでいうところのコミットと同義。リモートリポジトリがすっからかんの場合は git push -u origin master
FETCHSVNでいうところのアップデート(ローカルへのマージは行わない?)
PULLSVNでいうところのアップデート(ローカルへのマージまで行うFETCH+MERGE)
cloneチェックアウトだが、gitではブランチの切替に利用する
forkgithubの機能。cloneと似ているが、分散開発用に元の所有者に通知がいく。forkするという行為はオリジナルへの貢献を前提とする。
アップストリーム ブランチローカルに対して上流にあるブランチ。cloneした場合はclone元だが、指定もできる
branch -aリモート含むブランチ一覧の表示
branch -dブランチの削除。すっからかんじゃないと削除不可能
git log --pretty=oneline pom.xmlログを省略形の一行で表示。
git log --abbrev-commit pom.xmlコミットIDを省略系。一行で表示。
git log -p ファイル or ディレクトリ指定のファイル or ディレクトリ以下の更新のみ表示

checkout

ローカルブランチを作成して、そこにリモートを紐つけ。 まずはgit fetchしてリモートのリポジトリ情報を更新しておく

git checkout -b local_branch origin/remote_branch

このあとgit fetchが必要。←間違い!git pullと何が違うの?リモートブランチ情報が更新されているかもしれないので事前にfetchしておく!

リモートのリポジトリの内容に合わせる

git fetch origin
git reset --hard origin/master

push

引数なしのpushは非常に危険 http://dqn.sakusakutto.jp/2012/10/git_push.html

ブランチ指定してPUSHgit push --set-upstream origin testing
実際には実行しない(dry-run)git push -n

pull

リモートから取得する(fetch+merge)

git pull origin testingoriginリポジトリのtestingブランチを取り込む

元に戻す系

svnのように簡単ではない。svn revertはgit checkout file名

checkout ファイル名svn revert ファイル名
checkout .すべてのファイルのローカル変更を取消!
revertコミットを取り消す。git revert commit番号
resetある時点のコミット以降をすべて取り消す!
reset --hardrevertみたいなもの。ローカルコミットは取り消さない
showgit show commit番号:ファイル名

pushされたコミットを打ち消す

git log 
git revert コミットID
git push 

マージ

git checkout merged
git merge マージ元のブランチ(リモートを指定してOK)
git status でUnmergedになっているもの

チェックアウトなど

ファイルの変更サイクル

  1. 変更
  2. add(ステージングにファイルが追加)
  3. commit(ここまではローカルリポジトリ)
  4. push(リモートリポジトリにコミット)

ブランチ運用

masterブランチを統合ブランチとして、リリース向けに運用する。トピックブランチは機能追加、バグ修正のタイミングで作成され、リリース前に統合ブランチにマージされる。 branch -aでリモート含むブランチ状況を確認しながら作業する。

git branch testingtestingブランチ追加
git checkout testingtestingブランチに切替

マージ

first-forwardマージ

分岐以降統合ブランチに変更がない場合、統合ブランチのコミット履歴はトピックブランチのコミット履歴とイコールになる。シンプルなので早送りマージと呼ばれる。ただしトピックブランチの履歴は残らないので、たとえfirst-forwardが可能であってもやらないほうがよい。

マージ

通常のマージ。統合とトピックブランチをマージしたというコミットが作成される。

rebase

ブランチの履歴を消してマージ。トピックブランチが小さいものが多く、頻発するのであればこちらの運用が良いかもしれぬ。

コメント修正

git commit --amend

特定のコミットのみマージ

git cherry-pick 5d0b85e02f5ae4c4984fac388015fe5ce1918673
git cherry-pick 1ea6be61498f49f00cfd0fd5a0a08590087930a6

いろいろためした

$ git status
On branch MyBranche
Your branch is up-to-date with 'origin/NewBranch'.
nothing to commit, working directory clean
$ git status
On branch development
Your branch is behind 'origin/Develop' by 6 commits, and can be  fast-forwarded.
 (use "git pull" to update your local branch)
nothing to commit, working directory clean

トラブルシューティング

error: failed to push some refs to 'http://example.com/git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes before pushing again.  See the 'Note about
fast-forwards' section of 'git push --help' for details.

Your local changes to the following files would be overwritten by checkout:

ブランチ移動時に未コミットのファイルがあると上記エラー。 消してもいいなら以下のコマンドでローカルの変更を戻す。

git checkout .

Install

It's easy to install from source! just configure and make and make install!

apache

git-http-backendがソースコンパイルだとないため、下記はバイナリーインストールのときのみ可能。

http://www.torutk.com/projects/swe/wiki/CentOS_7%E3%81%A7apache%E7%B5%8C%E7%94%B1%E3%81%AEgit%E3%82%B5%E3%83%BC%E3%83%90%E3%83%BC%E3%82%92%E6%A7%8B%E7%AF%89

http://www.proton.jp/main/programming/git/smart-http.html

SetEnv GIT_PROJECT_ROOT /usr/local/git
SetEnv GIT_HTTP_EXPORT_ALL
 
ScriptAlias /git/ /usr/local/libexec/git-core/git-http-backend/
 
<locationMatch "/git">
   AuthType Basic
   AuthName "Git Area"
   AuthUserFile /usr/local/apache2/htdocs/.htpasswd
   require valid-user
   Order allow,deny
   Allow from all
</locationMatch>

AWSの場合

[root@ip-172-31-17-50 git]# git init
Initialized empty Git repository in /var/www/git/.git/
#Alias /git /var/www/git
SetEnv GIT_PROJECT_ROOT /var/www/git/.git
SetEnv GIT_HTTP_EXPORT_ALL
ScriptAlias /git/ /usr/libexec/git-core/git-http-backend/
<Directory /var/www/git>
  Options +ExecCGI
  AddHandler cgi-script .cgi
  DirectoryIndex gitweb.cgi
</Directory>

gitstatsの使い方

まずはgitをクローンする。そのうえで以下のように実行するとoutput_dirにhtmlが出力される。

gitstats git_clone_dir/ /var/www/html/new_dir
gitstats -c commit_begin=ハッシュ(短いのでもOK)

http://manpages.ubuntu.com/manpages/trusty/man1/gitstats.1.html

文字化け

内部でgnuplotを呼んでいる。フォントがないとarialを使おうとするので、以下のコマンドで回避

yum install vlgothic-fonts.noarch
export GDFONTPATH=/usr/share/fonts/vlgothic
export GNUPLOT_DEFAULT_GDFONT=VL-Gothic-Regular

httpとの連携

http://int128.hatenablog.com/entry/20130118/1358440428

ハマリポイント

リモートと同名のブランチがローカルにも簡単に作れてしまう

既存branchの切り替えにはcheckoutを使うが、間違ってbranchを使うと作成されてしまう。 オペレーションミスだが、警告すら出ないのが問題。一度そうなると修正するのが面倒なのでgit cloneしたほうがよい。

アップストリームとブランチ名の関係が不明瞭

分かりにくい原因がこれに尽きる。サーバーの最新状態をorigin(接続先のエイリアス)/masterであらわされたとするとローカルのmasterはこの時点のスナップショット。リモートと同期するにはgit fetch originを行う。

svnのリポジトリ移行

#ローカルリポジトリをチェックアウト
git svn clone -s https://REPO_URL/
cd xxx
git status

ブランチ間の差分チェック

git diff master:対象ファイル branch:対象ファイル
#!/bin/sh
LEFT=origin/master
RIGHT=origin/branch
DIFFLIST=$1
while read x; do
#  echo $x
  git diff $LEFT:$x $RIGHT:$x >> diff_result.txt
done < $DIFFLIST

トップ   編集 凍結 差分 履歴 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2022-10-14 (金) 13:16:52