Skip to main content

Posts about git

github

adding ssh public key

You can use github without ssh key. But when you add ssh key and you use ssh private key without passphrase, it is convinient for scripting.

Login and follow menu like below: Settings > SSH and GPG keys > New SSH key Then register name and content of your ssh public key.

edit ssh config file

cat << END | tee -a .ssh/config
Host github.com
    IdentityFile    ~/.ssh/id_rsa.mykey
    User            git
Host *.github.com
    IdentityFile    ~/.ssh/id_rsa.mykey
    User            git
END

test ssh connection. add -v to see debug messages

ssh -T github.com

When you've already work with any github repository, if you want to use git protocol instad of https, you can use it.

git config --local url.git@github.com:.insteadOf https://github.com/
git config --local url.git@gist.github.com:.insteadOf https://gist.github.com/
git config --local --list
git remote -v

After Create a new repository

when you don't have ever created a git repository

git init
git add README.md
git commit -m "first commit"

after that add remote to the repository, and push it.

git remote add origin git@github.com:username/myrepositoryname.git
git remote -v
git push -u origin master

To .gitignore file, add list of files which you don't want to add to the git repository

cat << END | tee -a .gitignore
id_rsa
END

initialize history of repository

rm -rf .git
git init
git add .
git commit -a -m "<commit message>"
git remote add origin <url>
git push -u origin master -f

git

オープンソースの分散型VCS。同種のアプリケーションと比較してgitが優れているところは、ユーザが多く情報が多いという点。

gitリポジトリの作成

cd myproject
git init
git add .
git status
git diff
git rm [-r] target # -rで、ディレクトリを削除
git commit -m "commit message"
git log

作業ディレクトリを持たない公開用リポジトリの作成

git init --bare
or
git clone --bare myproject myproject.git

git log
git branch

単純なリモート作業

git clone git://example.com/.../myproject.git # リポジトリのclone
cd myproject
:
git push [origin] [master] # 変更内容をpush

git pull # fetchしてmerge

ブランチを作成し作業対象とする

git branch master
git pull
git checkout -b mybranch
git branch

masterブランチにマージ。不要なブランチの削除

git checkout master
git merge mybranch
git branch -d mybranch

ブランチをリモートにpush

git push origin mybranch

ブランチをリモートからfetchして作業

git fetch origin
git branch -a
git checkout -b mybranch origin/mybranch # ブランチを作成してチェックアウト

リモートブランチの削除

git push origin :mybranch

change last commit

git add file
git commit --amend -m "commit message"
git push -f origin master

Stash the changes in a dirty working directory away

git stash
git stash list
git stash show -p stash@{0}

apply stash on top of the current working directory. and remove a stash from the list

git stash apply stash@{0}
git stash drop stash@{0}

reset indext to cancel add operation and leave the file as it is

git reset HEAD xxxx.txt

ignore file permission change (chmod)

git config --local core.filemode false
git config --local --list

example of URIs

ssh://username@serverhostname/var/lib/git/myrepository.git
file:///var/lib/git/myrepository.git