您的位置:首页 > 其它

Git 安装/设置/使用

2015-03-30 11:20 288 查看

安装

shell

$sudo apt-get install git

$git version


设置

设置文件包括以下三个:

1. 系统级: /etc/gitconfig ==
$git config --system


2. 全局: ~/.gitconfig ==
$git config --global


3. 仓库级: .git/config ==
$git config


Note: .git/config 最后被执行,它可以覆盖前面配置文件变量。

配置语法:

'git config' [<file-option>] [type] [-z|--null] name [value [value_regex]]
'git config' [<file-option>] [type] --add name value
'git config' [<file-option>] [type] --replace-all name value [value_regex]
'git config' [<file-option>] [type] [-z|--null] --get name [value_regex]
'git config' [<file-option>] [type] [-z|--null] --get-all name [value_regex]
'git config' [<file-option>] [type] [-z|--null] --get-regexp name_regex [value_regex]
'git config' [<file-option>] [type] [-z|--null] --get-urlmatch name URL
'git config' [<file-option>] --unset name [value_regex]
'git config' [<file-option>] --unset-all name [value_regex]
'git config' [<file-option>] --rename-section old_name new_name
'git config' [<file-option>] --remove-section name
'git config' [<file-option>] [-z|--null] -l | --list
'git config' [<file-option>] --get-color name [default]
'git config' [<file-option>] --get-colorbool name [stdout-is-tty]
'git config' [<file-option>] -e | --edit


usage:

usage: git config [options]

Config file location
--global              use global config file
--system              use system config file
--local               use repository config file
-f, --file <file>     use given config file

Action
--get                 get value: name [value-regex]
--get-all             get all values: key [value-regex]
--get-regexp          get values for regexp: name-regex [value-regex]
--replace-all         replace all matching variables: name value [value_regex]
--add                 adds a new variable: name value
--unset               removes a variable: name [value-regex]
--unset-all           removes all matches: name [value-regex]
--rename-section      rename section: old-name new-name
--remove-section      remove a section: name
-l, --list            list all
-e, --edit            opens an editor
--get-color <slot>    find the color configured: [default]
--get-colorbool <slot>
find the color setting: [stdout-is-tty]

Type
--bool                value is "true" or "false"
--int                 value is decimal number
--bool-or-int         value is --bool or --int
--path                value is a path (file or directory name)

Other
-z, --null            terminate values with NUL byte


例如:

1. 查看配置:
git config -l


- 查看某个变量:
git config color.ui


- 查看global变量:
git config -l --global


- 查看包含字符串变量:
git config --get-regexp color


2. 添加删除修改变量

- 添加变量:
git config add cat.name Jerry


- 修改变量 :
git config --replace-all cat.name Tom


- 删除变量 :
git config unset cat.name Tom


3. 读取特定值

- 读取int 值:
git config --get cat.number --int


- 读取bool 值:
git config --get cat.coming --bool


- 读取path值:
git config --get cat.namelist --path


使用

这里仅仅列举下自己常用的功能。

1. 情景一: 拉代码–> 修改–> 提交 –> push

$git pull
$git add --> git commit -m "xxx" / git commit -a -m "xxx"
$git push origin HEAD:xxxx


情景二: 新建branch fix bug,再merge到 HEAD

$git checkout -b new_branch
$git branch -a #查看branch 信息,确认当前和master branch
$git add --> git commit
$git checkout master
$git pull --> git merge new_branch


情景三: 已经commit,但由于某某某,需要对上次commit 修改、更新

$git add #要更新的
$git commit --amend #当然你也可以先reset 再重新commit.
#如果是update 远程如gerrit的patch,那就得用到Change-ID了。
#Chenge-ID 必须在提交msg的最后一行
$git push origin HEAD:xxxx


情景四: 时光机(版本回退和前进)

#回退用git log 找过去的id,前进就得用git reflog去看了。
$git log/reflog # 得到commit ID
$git rebase <commit ID>#可以用"--hard"/"--soft" 来决定是否保留change
#当想废弃一切改变,退回到master 版本就可以用
$git reset origin master


情景五: 已经commit了,但发现本地代码太旧

#先pull--> 解决conflict --> 再push
$git pull
$git diff #解决conflict
$git pull
#觉得pull有风险可以用fetch+ rebase
$git fetch
$git rebase #解决conflict


情景六: 想把当前的修改暂存起来,到用的时候再拿出来

#暂存
$git stash
#提取
$git stash pop# 最后一次
$git stash list#查看暂存的
$git stash apply stash@{2}#取第二个
$git stash clear#清除


参考大神wiki

廖雪峰 git wiki

官网Docs
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: