A Cup of Git Latte

Quick reference for some frequently used Git commands.

Set up a new repository

git init
git pull https://github.com/registercosmo/Cheatsheet.git
git remote add origin https://github.com/registercosmo/Cheatsheet.git
git add .
git status
git commit
git commit -m "comments"
git push -u origin master

Sync with the remote repository

# To pull changes to workspace (= fetch + merge)
git pull origin master

# To fetch lastest changes from other developers to local repo
git fetch <remote_name>

# To merge the local repo and the workspace
git merge origin master

Fix the previous commit

git commit --amend

Undo changes

# unstage changes but keep the local changes in the working space
git reset HEAD

# discard all the changes in the stage and working space
# and match the working space the commit B
git reset --hard B

# move the HEAD to commit B, no modification will be made
# to the stage and the working space
git reset --soft B

Untrack files

git rm --cached <file_name>

Set up remote repository

# set up a new remote repo (origin)
git remote add origin <new repo url>

# change URL for a remote repo (origin)
git remote set-url origin <new repo url>

# list all the remote repositories URLs
git remote -v

About configs

# configs for all repositories
git config --global user.name # check current global user.name
git config --global user.name <My username> # set global user.name
git config --global user.email # check current global user.email
git config --gloabl user.email=myemail@example.com # set global user.name

# configs for the current local repository
git config user.name # check current local user.name
git config user.name <My username> # set local user.name
git config user.email # check current local user.email
git config user.email=myemail@example.com # set local user.name

# list all the configs
git config --list

# Remove the configs
git config --global --unset-all user.name # global

# Change the configs
git config --global --replace-all user.name <New User Name> # global

About branches

# fetch remote branches from Github
git remote add origin <ULR> # we have to set origin first
git pull # it will pull(fetch + merge) all available branches from repositories

# check info for all available branches
git branch

# create a new branch
git branch  <new_branch_name>

# switch to a specific branch
git checkout  <new_branch_name>

# create and switch to a new branch
git checkout -b <new_branch_name>

# check the current branch
git status

# rename a branch
git branch -m <old_branch_name> <new_branch_name>

# merge a specific branch to the current branch
git merge <another_branch_name>

# delete a branch locally
git branch -d <branch_name>

# delete a branch remotely
git push origin :<branch_name>

About diff

# inspect all the modifications between workspace and stage
git diff

# inspect all the modifications between stage and local repo
git diff --cached

About resolving conflicts

git add <resolved file>

Advanced topics

# get the SHA-1 key for a specific contents
echo 'test content' | git hash-object -w --stdin

# get the content of a SHA-1 key
git cat-file -p <key>

# get a object type for a SHA-1 key
git cat-file -t <key>
最后編輯于
?著作權歸作者所有,轉載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市万俗,隨后出現(xiàn)的幾起案子胧卤,更是在濱河造成了極大的恐慌剧浸,老刑警劉巖,帶你破解...
    沈念sama閱讀 216,651評論 6 501
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件赞弥,死亡現(xiàn)場離奇詭異讯泣,居然都是意外死亡,警方通過查閱死者的電腦和手機峭弟,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,468評論 3 392
  • 文/潘曉璐 我一進店門赁濒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人孟害,你說我怎么就攤上這事∨材猓” “怎么了挨务?”我有些...
    開封第一講書人閱讀 162,931評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長玉组。 經(jīng)常有香客問我谎柄,道長,這世上最難降的妖魔是什么惯雳? 我笑而不...
    開封第一講書人閱讀 58,218評論 1 292
  • 正文 為了忘掉前任朝巫,我火速辦了婚禮,結果婚禮上石景,老公的妹妹穿的比我還像新娘劈猿。我一直安慰自己,他們只是感情好潮孽,可當我...
    茶點故事閱讀 67,234評論 6 388
  • 文/花漫 我一把揭開白布揪荣。 她就那樣靜靜地躺著,像睡著了一般往史。 火紅的嫁衣襯著肌膚如雪仗颈。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,198評論 1 299
  • 那天椎例,我揣著相機與錄音挨决,去河邊找鬼。 笑死订歪,一個胖子當著我的面吹牛脖祈,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播陌粹,決...
    沈念sama閱讀 40,084評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼撒犀,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了掏秩?” 一聲冷哼從身側響起或舞,我...
    開封第一講書人閱讀 38,926評論 0 274
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蒙幻,沒想到半個月后映凳,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 45,341評論 1 311
  • 正文 獨居荒郊野嶺守林人離奇死亡邮破,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,563評論 2 333
  • 正文 我和宋清朗相戀三年诈豌,在試婚紗的時候發(fā)現(xiàn)自己被綠了仆救。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,731評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡矫渔,死狀恐怖彤蔽,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情庙洼,我是刑警寧澤顿痪,帶...
    沈念sama閱讀 35,430評論 5 343
  • 正文 年R本政府宣布,位于F島的核電站油够,受9級特大地震影響蚁袭,放射性物質發(fā)生泄漏。R本人自食惡果不足惜石咬,卻給世界環(huán)境...
    茶點故事閱讀 41,036評論 3 326
  • 文/蒙蒙 一揩悄、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧鬼悠,春花似錦删性、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,676評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至袜啃,卻和暖如春汗侵,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背群发。 一陣腳步聲響...
    開封第一講書人閱讀 32,829評論 1 269
  • 我被黑心中介騙來泰國打工晰韵, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人熟妓。 一個月前我還...
    沈念sama閱讀 47,743評論 2 368
  • 正文 我出身青樓雪猪,卻偏偏與公主長得像,于是被迫代替她去往敵國和親起愈。 傳聞我的和親對象是個殘疾皇子只恨,可洞房花燭夜當晚...
    茶點故事閱讀 44,629評論 2 354

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