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 |
新規作成したブランチをpush | git push origin HEAD |
最新のコミットだけclone | git clone --depth 1 URL |
shallow cloneを元に戻す | git fetch --unshallow |
shallow clonにする | git fetch --depth 1 |
privateリポジトリが無制限になったので個人開発には十分
ssh-keygen -t ras -C コメント
https://github.com/settings/tokens
二段階認証を設定した時にgit cloneなどの時に必要なアクセストークン。普遍にできるし、いつでも無効化できるので、環境変数などに設定しておく。macかつbrewでgitインストールしているとosxkeychainを介して初回のみアクセストークンいれればOK
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
今いるブランチとリモートのnew_branchの差分表示
git diff origin/new_branch --name-only
さすがに使う機会が増えてきたのでなれるべく一部移行。下記ドキュメントが良い。
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
git pull origin testing
リポジトリには作業ディレクトリを含むものと含まないベアリポジトリがある。サーバー側はベアリポジトリにするが、ローカルはノンベアリポジトリにするべし
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
/.gitconfigに設定を保存しておける。
[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が作成される
ローカルリポジトリ、index、リモートリポジトリと構成要素が複雑怪奇。ブランチがSVNより増える傾向がある。
add | コミット準備領域であるindexに追加する |
commit | あくまでローカルリポジトリに対してのコミット。-aオプションをつけないと変更してもコミット対象にはならない |
PUSH | リモートリポジトリへのコミット。SVNでいうところのコミットと同義。リモートリポジトリがすっからかんの場合は git push -u origin master |
FETCH | SVNでいうところのアップデート(ローカルへのマージは行わない?) |
PULL | SVNでいうところのアップデート(ローカルへのマージまで行うFETCH+MERGE) |
clone | チェックアウトだが、gitではブランチの切替に利用する |
fork | githubの機能。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 ディレクトリ以下の更新のみ表示 |
ローカルブランチを作成して、そこにリモートを紐つけ。 まずはgit fetchしてリモートのリポジトリ情報を更新しておく
git checkout -b local_branch origin/remote_branch |
このあとgit fetchが必要。←間違い!git pullと何が違うの?リモートブランチ情報が更新されているかもしれないので事前にfetchしておく!
git fetch origin git reset --hard origin/master
引数なしのpushは非常に危険 http://dqn.sakusakutto.jp/2012/10/git_push.html
ブランチ指定してPUSH | git push --set-upstream origin testing |
実際には実行しない(dry-run) | git push -n |
リモートから取得する(fetch+merge)
git pull origin testing | originリポジトリのtestingブランチを取り込む |
svnのように簡単ではない。svn revertはgit checkout file名
checkout ファイル名 | svn revert ファイル名 |
checkout . | すべてのファイルのローカル変更を取消! |
revert | コミットを取り消す。git revert commit番号 |
reset | ある時点のコミット以降をすべて取り消す! |
reset --hard | revertみたいなもの。ローカルコミットは取り消さない |
show | git show commit番号:ファイル名 |
git log git revert コミットID git push
git checkout merged
git merge マージ元のブランチ(リモートを指定してOK)
git status でUnmergedになっているもの
masterブランチを統合ブランチとして、リリース向けに運用する。トピックブランチは機能追加、バグ修正のタイミングで作成され、リリース前に統合ブランチにマージされる。 branch -aでリモート含むブランチ状況を確認しながら作業する。
git branch testing | testingブランチ追加 |
git checkout testing | testingブランチに切替 |
分岐以降統合ブランチに変更がない場合、統合ブランチのコミット履歴はトピックブランチのコミット履歴とイコールになる。シンプルなので早送りマージと呼ばれる。ただしトピックブランチの履歴は残らないので、たとえfirst-forwardが可能であってもやらないほうがよい。
通常のマージ。統合とトピックブランチをマージしたというコミットが作成される。
ブランチの履歴を消してマージ。トピックブランチが小さいものが多く、頻発するのであればこちらの運用が良いかもしれぬ。
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.
ブランチ移動時に未コミットのファイルがあると上記エラー。 消してもいいなら以下のコマンドでローカルの変更を戻す。
git checkout .
It's easy to install from source! just configure and make and make install!
git-http-backendがソースコンパイルだとないため、下記はバイナリーインストールのときのみ可能。
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>
[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>
まずは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://int128.hatenablog.com/entry/20130118/1358440428
既存branchの切り替えにはcheckoutを使うが、間違ってbranchを使うと作成されてしまう。 オペレーションミスだが、警告すら出ないのが問題。一度そうなると修正するのが面倒なのでgit cloneしたほうがよい。
分かりにくい原因がこれに尽きる。サーバーの最新状態をorigin(接続先のエイリアス)/masterであらわされたとするとローカルのmasterはこの時点のスナップショット。リモートと同期するにはgit fetch originを行う。
#ローカルリポジトリをチェックアウト 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