Git Basics

  • git init && git clone
#Initializing a Repository in an Existing Directory
$ git init

#Cloning an Existing Repository
$ git clone https://github.com/libgit2/libgit2

#Cloning an Existing Repository with another name
$ git clone https://github.com/libgit2/libgit2  mylibgit
  • git status
$ git status

#short status
$ git status -s
  • Ignoring Files
$ cat .gitignore

The rules for the patterns you can put in the .gitignore file are as follows:
Blank lines or lines starting with # are ignored.
Standard glob patterns work.
You can start patterns with a forward slash (/) to avoid recursivity.
You can end patterns with a forward slash (/) to specify a directory.
You can negate a pattern by starting it with an exclamation point (!).

# no .a files
*.a

# but do track lib.a, even though you're ignoring .a files above
!lib.a

# only ignore the TODO file in the current directory, not subdir/TODO
/TODO

# ignore all files in the build/ directory
build/

# ignore doc/notes.txt, but not doc/server/arch.txt
doc/*.txt

# ignore all .pdf files in the doc/ directory
doc/**/*.pdf
  • git diff
#see changes not yet staged
$ git diff

#see changes have staged
$ git diff --staged

  • git commit
#commit changes
$ git commit 

#commit with commit message inline
$ git commit -m "commit messages"

#commit to skip the staging area,for the traced files
$ git commit -a -m "commit messages"
  • git rm
#remove a file from Git,also remove it from working directory
$ git rm README.md

#remove a file from Git,keep it in working directory
$ git rm --cached README.md

#You can pass files, directories, and file-glob patterns to the git rm command.
$ git rm log/\*.log
  • git mv
#rename a file
$ git mv file_from file_to

#this is equivalent to running something like this:
$ mv README.md README
$ git rm README.md
$ git add README
  • git log
#view the commit history
$ git log

#-p shows the difference, -2 limits the output to last two entries:
$git log -p -2 

#--stat display what happened,see abbreviated stats for each commit
$ git log --stat

# --pretty,changes the log output to formats other than the default.
$ git log --pretty=oneline

# format, specify you own log output format.
$ git log --pretty=format:"%h - %an, %ar: %s"

# --graph,adds a nice little ASCII graph showing
$ git log --pretty=format:"%h %s" --graph

#limit log output
$ git log --since=2.weeks

#see which commits modifying test files in the Git source code history are merged and were committed by Junio Hamano in the month of October 2008
$ git log --pretty="%h - %s" --author=gitster --since="2008-10-01" \
   --before="2008-11-01" --no-merges -- t/


Option Description
-(n) Show only the last n commits
--since, --after Limit the commits to those made after the specified date.
--until, --before Limit the commits to those made before the specified date.
--author Only show commits in which the author entry matches the specified string.
--committer Only show commits in which the committer entry matches the specified string.
--grep Only show commits with a commit message containing the string
-S Only show commits adding or removing code matching the string
  • git undo
# --amend,amend to the last commit 
$ git commit --amend

#unstaging a staged file
$ git reset HEAD <file>....

#unmodify a modified file
$ git checkout -- <file>...
  • git remote
#list remotes
$ git remote

$ git remote -v

#add remote repository
$ git remote add pb https://github.com/paulboone/ticgit

#fetch from remote
$ git fetch pb

#push to remote
$ git push origin master

#inspect a remote
$ git remote show origin

#rename and remove
$ git remote rename pb paul

$ git remote rm paul
  • git tag
#list your tags
$ git tag

#search tags
$ git tag -l "v1.8.5*"

#create a tag
$ git tag -a v1.4 -m "my version 1.4"

#show a tag
$ git show v1.4

#push tag
$ git push origin [tagname]

#push all tags
$ git push origin --tags

#checkout tags
$ git checkout -b [branchname] [tagname]
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 199,711评论 5 468
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 83,932评论 2 376
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 146,770评论 0 330
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,799评论 1 271
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,697评论 5 359
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,069评论 1 276
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,535评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,200评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,353评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,290评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,331评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,020评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,610评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,694评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,927评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,330评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 41,904评论 2 341

推荐阅读更多精彩内容

  • 输入 y ,创建 git_hug 目录No githug directory found, do you wish...
    风花花阅读 1,913评论 0 4
  • 1. GIT命令 git init在本地新建一个repo,进入一个项目目录,执行git init,会初始化一个re...
    江边一蓑烟阅读 776评论 0 0
  • 查看、添加、提交、删除、找回,重置修改文件 git help # 显示command的help git sho...
    Swiftor阅读 2,086评论 0 2
  • UG培训第三十六天 第四道考核的分析建模
    阳光155阅读 100评论 0 0
  • 逻辑回归:假设数据服从伯努利分布的线性分类模型。 逻辑回归损失函数推导 一、逻辑回归模型 逻辑回归模型可以看作是由...
    菜鸟瞎编阅读 651评论 0 0