Git

Git

Local

Install Git

# install
sudo apt-get install git

# important config
# --global means it's working for current user (~/.gitconfig)
# without --global means for current repository (.git/config)
git config --global user.name "myname"
git config --global user.email "myemail"
# other config
git conifg --global color.ui true
git config --global alias alias_name raw_name

Basic Skills

# make directory
mkdir dir
cd dir

# initialize git repository
git init

# add files which ready to commit
git add file1
git add file2
# even the file3 is in .gitignore
git add -f file3
...

# delete files
rm file1
git rm file1
# introduce later, recovery file1
# git checkout -- file1

# commit changes
git commit -m "msg"

# branches
# create branch
git branch bra1
# switch to new branch ${bra1}
git checkout bra1
# create and switch
git checkout -b bra1
# merge branches
# merge specified branch with current branch
git merge bra1
git merge --no-ff -m "msg" bra1
# delete branch
git branch -d bra1
git branch -D feature-name

# stash
git stash
# recovery
git stash apply
git stash apply stash@{num}
# drop
git stash drop
# recovery and drop
git stash pop

# generate .gitignore and put the files or directories which you don't want track to it.

Check

# difference
git diff file1
git diff HEAD -- readme.txt

# add and commit status
git status

# log
# first commit to current commit
git log
git log --pretty=oneline
git log --graph --pretty=oneline --abbrev-commit
git log -1

# all commit
git reflog

# check status of current branches
git branch
git branch -a
git branch -vv

# check stash
git stash list

# check remote repository
git remote
git remote -v

# check .gitignore
# find the rule why file3 has been ignored
git check-ignore -v file3

Time Travel

# commitid fineness
git reset --hard HEAD^
git reset --hard HEAD^^
git reset --hard HEAD~100
git reset --hard commitid

# variant fineness
# file will change: discard changes after git add
# file will change: discard changes after git commit
git checkout -- file1

# variant fineness
# file don't change: discard git add
git reset HEAD file1

Remote

Link

# use ssh to communicate
# generate id_rsa and id_rsa.pub in ~/.ssh/
ssh-keygen -t rsa -C "myeami"

# add id_rsa.pub to SSH KEYS of remote servers
# remote server: GitHub, gitoschina etc.

Basic Skills

# fetch remote repository information
git fetch
# merge local branch with remote branch
git merge
git merge origin/remote_branch_name
git merge @{upstream}
git merge @{u}

# fetch and merge
git pull

# create remote branch the first time
# push local branch to the remote branch
git push origin remote_branch_name
# push specified local tag to the remote server
git push origin local_tag_name
# push all local tag (which havn't been pushed) to the remote server
git push origin --tags

# make tag (the name of commitid)
git tag tag_name
git tag tag_name commitid
git tag -a tag_name -m "msg" commitid
# use -s (instead of -a) to generate PGP signature, gpg has been needed.
git tag -s tag_name -m "msg" commitid
# delete local tag
git tag -d v0.1
# delete remote tag
# delete local tag first
git push origin :refs/tags/tag_name

Check

# show all tag names
git tag

# show one specific tag information
git show tag_name

Sync

# both local and remote repository already exists

# create new repository on remote server

# associate local repository with remote repository
# Git remote repository name: origin (default name)
git remote add origin remote.git.address

# push current branch (default master) to remote branch
# -u : used at the first time to associate local master branch with remote master branch.
git push -u origin master
git push origin master
git push origin remote_branch_name

Clone

# local repository doesn't exists

# clone repository from remote server
# create local directory itself
# can only see master branch
git clone remote.git.address

# get other branch
# create new remote branch
git push origin remote_branch_name
# create local branch and link it to remote branch
git branch local_branch_name
git branch -u origin/remote_branch_name
git branch --set-upstream local_branch_name origin/remote_branch_name
# remote branch already exists
git checkout -b other_branch origin/other_branch

Usage

  • master: only do release
  • dev: everyone commit to dev, dev commit to master.
  • feature-name: new feature, experiment.
  • bug:
  • everyone:

GitHub

  • Fork other people's project
  • clone from self-repository on GitHub
  • pull request on self-repository of GitHub, waiting for raw project manager to accept.

Git server

  • install: sudo apt-get install git

  • add user: sudo adduser git

  • certificate: collect id_rsa.pub and put them into file /home/git/.ssh/authorized_keys

  • init git repository: do sudo git init --bare repository_name.git under /dirname/. (No working directory in it, because it's just for share, no one should login this server and change it.)

  • change owner: sudo chown -R git:git repository_name.git

  • forbiden shell: change /etc/passwd, change git:x:1001:1001:,,,:/home/git:bin/bash into git:x:1001:1001:,,,:/home/git:/user/bin/git-shell.
    (then we can use git by ssh, but we can't use shell.)

  • clone: git clone git@server:/dirname/repository_name.git

  • push etc.

  • Gitosis to manage id_rsa.pub

  • Gitolite to manage privilege

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子琢岩,更是在濱河造成了極大的恐慌善绎,老刑警劉巖祈远,帶你破解...
    沈念sama閱讀 206,311評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件膨处,死亡現(xiàn)場離奇詭異,居然都是意外死亡抑党,警方通過查閱死者的電腦和手機润绎,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,339評論 2 382
  • 文/潘曉璐 我一進店門撬碟,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人莉撇,你說我怎么就攤上這事呢蛤。” “怎么了棍郎?”我有些...
    開封第一講書人閱讀 152,671評論 0 342
  • 文/不壞的土叔 我叫張陵其障,是天一觀的道長。 經(jīng)常有香客問我涂佃,道長励翼,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 55,252評論 1 279
  • 正文 為了忘掉前任辜荠,我火速辦了婚禮汽抚,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘伯病。我一直安慰自己造烁,他們只是感情好,可當我...
    茶點故事閱讀 64,253評論 5 371
  • 文/花漫 我一把揭開白布午笛。 她就那樣靜靜地躺著惭蟋,像睡著了一般。 火紅的嫁衣襯著肌膚如雪药磺。 梳的紋絲不亂的頭發(fā)上告组,一...
    開封第一講書人閱讀 49,031評論 1 285
  • 那天,我揣著相機與錄音与涡,去河邊找鬼惹谐。 笑死,一個胖子當著我的面吹牛驼卖,可吹牛的內(nèi)容都是我干的氨肌。 我是一名探鬼主播,決...
    沈念sama閱讀 38,340評論 3 399
  • 文/蒼蘭香墨 我猛地睜開眼酌畜,長吁一口氣:“原來是場噩夢啊……” “哼怎囚!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起桥胞,我...
    開封第一講書人閱讀 36,973評論 0 259
  • 序言:老撾萬榮一對情侶失蹤恳守,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后贩虾,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體催烘,經(jīng)...
    沈念sama閱讀 43,466評論 1 300
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 35,937評論 2 323
  • 正文 我和宋清朗相戀三年缎罢,在試婚紗的時候發(fā)現(xiàn)自己被綠了伊群。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 38,039評論 1 333
  • 序言:一個原本活蹦亂跳的男人離奇死亡策精,死狀恐怖舰始,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情咽袜,我是刑警寧澤丸卷,帶...
    沈念sama閱讀 33,701評論 4 323
  • 正文 年R本政府宣布,位于F島的核電站询刹,受9級特大地震影響谜嫉,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜凹联,卻給世界環(huán)境...
    茶點故事閱讀 39,254評論 3 307
  • 文/蒙蒙 一骄恶、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧匕垫,春花似錦僧鲁、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,259評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至偶惠,卻和暖如春春寿,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背忽孽。 一陣腳步聲響...
    開封第一講書人閱讀 31,485評論 1 262
  • 我被黑心中介騙來泰國打工绑改, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留谢床,地道東北人。 一個月前我還...
    沈念sama閱讀 45,497評論 2 354
  • 正文 我出身青樓厘线,卻偏偏與公主長得像识腿,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子造壮,可洞房花燭夜當晚...
    茶點故事閱讀 42,786評論 2 345

推薦閱讀更多精彩內(nèi)容