groupdel

用于删除指定的工作组

补充说明

groupdel命令 用于删除指定的工作组,本命令要修改的系统文件包括/ect/group和/ect/gshadow。若该群组中仍包括某些用户,则必须先删除这些用户后,方能删除群组。

语法

1
groupdel(参数)

参数

组:要删除的工作组名。

实例

1
2
groupadd damon  //创建damon工作组
groupdel damon //删除这个工作组

groupadd

用于创建一个新的工作组

补充说明

groupadd命令 用于创建一个新的工作组,新工作组的信息将被添加到系统文件中。

语法

1
groupadd(选项)(参数)

选项

1
2
3
4
-g:指定新建工作组的id;
-r:创建系统工作组,系统工作组的组ID小于500;
-K:覆盖配置文件“/ect/login.defs”;
-o:允许添加组ID号不唯一的工作组。

参数

组名:指定新建工作组的组名。

实例

建立一个新组,并设置组ID加入系统:

1
groupadd -g 344 jsdigname

此时在/etc/passwd文件中产生一个组ID(GID)是344的项目。

grep

强大的文本搜索工具

补充说明

grep (global search regular expression(RE) and print out the line,全面搜索正则表达式并把行打印出来)是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹配的行打印出来。用于过滤/搜索的特定字符。可使用正则表达式能多种命令配合使用,使用上十分灵活。

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
-a --text  # 不要忽略二进制数据。
-A <显示行数> --after-context=<显示行数> # 除了显示符合范本样式的那一行之外,并显示该行之后的内容。
-b --byte-offset # 在显示符合范本样式的那一行之外,并显示该行之前的内容。
-B<显示行数> --before-context=<显示行数> # 除了显示符合样式的那一行之外,并显示该行之前的内容。
-c --count # 计算符合范本样式的列数。
-C<显示行数> --context=<显示行数>或-<显示行数> # 除了显示符合范本样式的那一列之外,并显示该列之前后的内容。
-d<进行动作> --directories=<动作> # 当指定要查找的是目录而非文件时,必须使用这项参数,否则grep命令将回报信息并停止动作。
-e<范本样式> --regexp=<范本样式> # 指定字符串作为查找文件内容的范本样式。
-E --extended-regexp # 将范本样式为延伸的普通表示法来使用,意味着使用能使用扩展正则表达式。
-f<范本文件> --file=<规则文件> # 指定范本文件,其内容有一个或多个范本样式,让grep查找符合范本条件的文件内容,格式为每一列的范本样式。
-F --fixed-regexp # 将范本样式视为固定字符串的列表。
-G --basic-regexp # 将范本样式视为普通的表示法来使用。
-h --no-filename # 在显示符合范本样式的那一列之前,不标示该列所属的文件名称。
-H --with-filename # 在显示符合范本样式的那一列之前,标示该列的文件名称。
-i --ignore-case # 忽略字符大小写的差别。
-l --file-with-matches # 列出文件内容符合指定的范本样式的文件名称。
-L --files-without-match # 列出文件内容不符合指定的范本样式的文件名称。
-n --line-number # 在显示符合范本样式的那一列之前,标示出该列的编号。
-q --quiet或--silent # 不显示任何信息。
-R/-r --recursive # 此参数的效果和指定“-d recurse”参数相同。
-s --no-messages # 不显示错误信息。
-v --revert-match # 反转查找。
-V --version # 显示版本信息。
-w --word-regexp # 只显示全字符合的列。
-x --line-regexp # 只显示全列符合的列。
-y # 此参数效果跟“-i”相同。
-o # 只输出文件中匹配到的部分。
-m <num> --max-count=<num> # 找到num行结果后停止查找,用来限制匹配行数

规则表达式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
^    # 锚定行的开始 如:'^grep'匹配所有以grep开头的行。    
$ # 锚定行的结束 如:'grep$' 匹配所有以grep结尾的行。
. # 匹配一个非换行符的字符 如:'gr.p'匹配gr后接一个任意字符,然后是p。
* # 匹配零个或多个先前字符 如:'*grep'匹配所有一个或多个空格后紧跟grep的行。
.* # 一起用代表任意字符。
[] # 匹配一个指定范围内的字符,如'[Gg]rep'匹配Grep和grep。
[^] # 匹配一个不在指定范围内的字符,如:'[^A-FH-Z]rep'匹配不包含A-R和T-Z的一个字母开头,紧跟rep的行。
\(..\) # 标记匹配字符,如'\(love\)',love被标记为1。
\< # 锚定单词的开始,如:'\<grep'匹配包含以grep开头的单词的行。
\> # 锚定单词的结束,如'grep\>'匹配包含以grep结尾的单词的行。
x\{m\} # 重复字符x,m次,如:'0\{5\}'匹配包含5个o的行。
x\{m,\} # 重复字符x,至少m次,如:'o\{5,\}'匹配至少有5个o的行。
x\{m,n\} # 重复字符x,至少m次,不多于n次,如:'o\{5,10\}'匹配5--10个o的行。
\w # 匹配文字和数字字符,也就是[A-Za-z0-9],如:'G\w*p'匹配以G后跟零个或多个文字或数字字符,然后是p。
\W # \w的反置形式,匹配一个或多个非单词字符,如点号句号等。
\b # 单词锁定符,如: '\bgrep\b'只匹配grep。

grep命令常见用法

在文件中搜索一个单词,命令会返回一个包含 “match_pattern” 的文本行:

1
2
grep match_pattern file_name
grep "match_pattern" file_name

在多个文件中查找:

1
grep "match_pattern" file_1 file_2 file_3 ...

输出除之外的所有行 -v 选项:

1
grep -v "match_pattern" file_name

标记匹配颜色 –color=auto 选项:

1
grep "match_pattern" file_name --color=auto

使用正则表达式 -E 选项:

1
2
3
grep -E "[1-9]+"
#
egrep "[1-9]+"

只输出文件中匹配到的部分 -o 选项:

1
2
3
4
5
echo this is a test line. | grep -o -E "[a-z]+\."
line.

echo this is a test line. | egrep -o "[a-z]+\."
line.

统计文件或者文本中包含匹配字符串的行数 -c 选项:

1
grep -c "text" file_name

输出包含匹配字符串的行数 -n 选项:

1
2
3
4
5
6
grep "text" -n file_name
#
cat file_name | grep "text" -n

#多个文件
grep "text" -n file_1 file_2

打印样式匹配所位于的字符或字节偏移:

1
2
3
echo gun is not unix | grep -b -o "not"
7:not
#一行中字符串的字符便宜是从该行的第一个字符开始计算,起始值为0。选项 **-b -o** 一般总是配合使用。

搜索多个文件并查找匹配文本在哪些文件中:

1
grep -l "text" file1 file2 file3...

grep递归搜索文件

在多级目录中对文本进行递归搜索:

1
2
grep "text" . -r -n
# .表示当前目录。

忽略匹配样式中的字符大小写:

1
2
echo "hello world" | grep -i "HELLO"
# hello

选项 -e 制动多个匹配样式:

1
2
3
4
5
6
7
8
9
10
echo this is a text line | grep -e "is" -e "line" -o
is
line

#也可以使用 **-f** 选项来匹配多个样式,在样式文件中逐行写出需要匹配的字符。
cat patfile
aaa
bbb

echo aaa bbb ccc ddd eee | grep -f patfile -o

在grep搜索结果中包括或者排除指定文件:

1
2
3
4
5
6
7
8
# 只在目录中所有的.php和.html文件中递归搜索字符"main()"
grep "main()" . -r --include *.{php,html}

# 在搜索结果中排除所有README文件
grep "main()" . -r --exclude "README"

# 在搜索结果中排除filelist文件列表里的文件
grep "main()" . -r --exclude-from filelist

使用0值字节后缀的grep与xargs:

1
2
3
4
5
6
7
8
# 测试文件:
echo "aaa" > file1
echo "bbb" > file2
echo "aaa" > file3

grep "aaa" file* -lZ | xargs -0 rm

# 执行后会删除file1和file3,grep输出用-Z选项来指定以0值字节作为终结符文件名(\0),xargs -0 读取输入并用0值字节终结符分隔文件名,然后删除匹配文件,-Z通常和-l结合使用。

grep静默输出:

1
2
grep -q "test" filename
# 不会输出任何信息,如果命令运行成功返回0,失败则返回非0值。一般用于条件测试。

打印出匹配文本之前或者之后的行:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# 显示匹配某个结果之后的3行,使用 -A 选项:
seq 10 | grep "5" -A 3
5
6
7
8

# 显示匹配某个结果之前的3行,使用 -B 选项:
seq 10 | grep "5" -B 3
2
3
4
5

# 显示匹配某个结果的前三行和后三行,使用 -C 选项:
seq 10 | grep "5" -C 3
2
3
4
5
6
7
8

# 如果匹配结果有多个,会用“--”作为各匹配结果之间的分隔符:
echo -e "a\nb\nc\na\nb\nc" | grep a -A 1
a
b
--
a
b

gpm

提供文字模式下的滑鼠事件处理

补充说明

gpm命令 是Linux的虚拟控制台下的鼠标服务器,用于在虚拟控制台下实现鼠标复制和粘贴文本的功能。

语法

1
gpm(选项)

选项

1
2
3
4
5
-a:设置加速值;
-b:设置波特率;
-B:设置鼠标按键次序;
-m:指定鼠标设备文件;
-t:设置鼠标类型。

gpasswd

Linux下工作组文件的管理工具

补充说明

gpasswd命令 是Linux下工作组文件/etc/group/etc/gshadow管理工具。

语法

1
gpasswd(选项)(参数)

选项

1
2
3
4
5
6
-a:添加用户到组;
-d:从组删除用户;
-A:指定管理员;
-M:指定组成员和-A的用途差不多;
-r:删除密码;
-R:限制用户登入组,只有组中的成员才可以用newgrp加入该组。

参数

组:指定要管理的工作组。

实例

如系统有个peter账户,该账户本身不是groupname群组的成员,使用newgrp需要输入密码即可。

1
gpasswd groupname

让使用者暂时加入成为该组成员,之后peter建立的文件group也会是groupname。所以该方式可以暂时让peter建立文件时使用其他的组,而不是peter本身所在的组。

所以使用gpasswd groupname设定密码,就是让知道该群组密码的人可以暂时切换具备groupname群组功能的。

1
gpasswd -A peter users

这样peter就是users群组的管理员,就可以执行下面的操作:

1
2
gpasswd -a mary users
gpasswd -a allen users

注意:添加用户到某一个组 可以使用usermod -G group_name user_name这个命令可以添加一个用户到指定的组,但是以前添加的组就会清空掉。

所以想要添加一个用户到一个组,同时保留以前添加的组时,请使用gpasswd这个命令来添加操作用户:

1
gpasswd -a user_name group_name

git

是目前世界上最先进的分布式版本控制系统

补充说明

git命令 很多人都知道,Linus在1991年创建了开源的Linux,从此,Linux系统不断发展,已经成为最大的服务器系统软件了。

Linus虽然创建了Linux,但Linux的壮大是靠全世界热心的志愿者参与的,这么多人在世界各地为Linux编写代码,那Linux的代码是如何管理的呢?

事实是,在2002年以前,世界各地的志愿者把源代码文件通过diff的方式发给Linus,然后由Linus本人通过手工方式合并代码!

你也许会想,为什么Linus不把Linux代码放到版本控制系统里呢?不是有CVS、SVN这些免费的版本控制系统吗?因为Linus坚定地反对CVS和SVN,这些集中式的版本控制系统不但速度慢,而且必须联网才能使用。有一些商用的版本控制系统,虽然比CVS、SVN好用,但那是付费的,和Linux的开源精神不符。

不过,到了2002年,Linux系统已经发展了十年了,代码库之大让Linus很难继续通过手工方式管理了,社区的弟兄们也对这种方式表达了强烈不满,于是Linus选择了一个商业的版本控制系统BitKeeper,BitKeeper的东家BitMover公司出于人道主义精神,授权Linux社区免费使用这个版本控制系统。

安定团结的大好局面在2005年就被打破了,原因是Linux社区牛人聚集,不免沾染了一些梁山好汉的江湖习气。开发Samba的Andrew试图破解BitKeeper的协议(这么干的其实也不只他一个),被BitMover公司发现了(监控工作做得不错!),于是BitMover公司怒了,要收回Linux社区的免费使用权。

Linus可以向BitMover公司道个歉,保证以后严格管教弟兄们,嗯,这是不可能的。实际情况是这样的:

Linus花了两周时间自己用C写了一个分布式版本控制系统,这就是Git!一个月之内,Linux系统的源码已经由Git管理了!牛是怎么定义的呢?大家可以体会一下。

Git迅速成为最流行的分布式版本控制系统,尤其是2008年,GitHub网站上线了,它为开源项目免费提供Git存储,无数开源项目开始迁移至GitHub,包括jQuery,PHP,Ruby等等。

历史就是这么偶然,如果不是当年BitMover公司威胁Linux社区,可能现在我们就没有免费而超级好用的Git了。

Git常用命令清单

语法

1
2
3
4
5
git [--version] [--help] [-C <path>] [-c name=value]
[--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
[-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
[--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
<command> [<args>]

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
add              将文件内容添加到索引
bisect 通过二进制查找引入错误的更改
branch 列出,创建或删除分支
checkout 检查分支或路径到工作树
clone 将存储库克隆到新目录中
commit 将更改记录到存储库
diff 显示提交,提交和工作树等之间的更改
fetch 从另一个存储库下载对象和引用
grep 打印匹配图案的行
init 创建一个空的Git仓库或重新初始化一个现有的
log 显示提交日志
merge 加入两个或更多的开发历史
mv 移动或重命名文件,目录或符号链接
pull 从另一个存储库或本地分支获取并合并
push 更新远程引用以及相关对象
rebase 转发端口本地提交到更新的上游头
reset 将当前HEAD复位到指定状态
rm 从工作树和索引中删除文件
show 显示各种类型的对象
status 显示工作树状态
tag 创建,列出,删除或验证使用GPG签名的标签对象

例子

init

git init #初始化

status

git status #获取状态

add

git add file # .或*代表全部添加
git rm --cached <added_file_to_undo> # 在commit之前撤销git add操作
git reset head # 好像比上面git rm --cached更方便

commit

git commit -m "message" #此处注意乱码

remote

git remote add origin git@github.com:JSLite/test.git #添加源

push

1
2
3
git push -u origin master # push同事设置默认跟踪分支  
git push origin master
git push -f origin master # 强制推送文件,缩写 -f(全写--force)

clone

git clone git://github.com/JSLite/JSLite.js.git
git clone git://github.com/JSLite/JSLite.js.git mypro #克隆到自定义文件夹
git clone [user@]example.com:path/to/repo.git/ #SSH协议还有另一种写法。

git clone支持多种协议,除了HTTP(s)以外,还支持SSH、Git、本地文件协议等,下面是一些例子。git clone <版本库的网址> <本地目录名>

1
2
3
4
5
6
7
$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/

配置

首先是配置帐号信息 ssh -T git@github.com 测试。

修改项目中的个人信息

1
2
3
4
git help config # 获取帮助信息,查看修改个人信息的参数  
git config --global user.name "小弟调调" # 修改全局名字
git config --global user.email "wowohoo@qq.com" # 修改全局邮箱
git config --list # 查看配置的信息

配置自动换行

自动转换坑太大,提交到git是自动将换行符转换为lf

1
git config --global core.autocrlf input

常见使用场景

创建SSH密钥

这个密钥用来跟 github 通信,在本地终端里生成然后上传到 github

1
2
3
ssh-keygen -t rsa -C 'wowohoo@qq.com' # 生成密钥  
ssh-keygen -t rsa -C "wowohoo@qq.com" -f ~/.ssh/ww_rsa # 指定生成目录文件名字
ssh -T git@github.com # 测试是否成功

多账号ssh配置

1.生成指定名字的密钥

ssh-keygen -t rsa -C "邮箱地址" -f ~/.ssh/jslite_rsa
会生成 jslite_rsajslite_rsa.pub 这两个文件

2.密钥复制到托管平台上

vim ~/.ssh/jslite_rsa.pub
打开公钥文件 jslite_rsa.pub ,并把内容复制至代码托管平台上

3.修改config文件

vim ~/.ssh/config #修改config文件,如果没有创建 config

1
2
3
4
5
6
7
8
9
10
11
12
Host jslite.github.com
HostName github.com
User git
IdentityFile ~/.ssh/jslite_rsa

Host work.github.com
HostName github.com
# Port 服务器open-ssh端口(默认:22,默认时一般不写此行)
# PreferredAuthentications 配置登录时用什么权限认证
# publickey|password publickey|keyboard-interactive等
User git
IdentityFile ~/.ssh/work_rsa
  • Host 这里是个别名可以随便命名
  • HostName 一般是网站如:`git@ss.github.com:username/repo.git填写github.com`
  • User 通常填写git
  • IdentityFile 使用的公钥文件地址

4.测试

1
2
3
ssh -T git@jslite.github.com  # `@`后面跟上定义的Host  
ssh -T work.github.com # 通过别名测试
ssh -i ~/公钥文件地址 Host别名 # 如 ssh -i ~/.ssh/work_rsa work.github.com

5.使用

1
2
3
4
5
# 原来的写法
git clone git@github.com:<jslite的用户名>/learngit.git
# 现在的写法
git clone git@jslite.github.com:<jslite的用户名>/learngit.git
git clone git@work.github.com:<work的用户名>/learngit.git

5.注意

如果你修改了id_rsa的名字,你需要将ssh key添加到SSH agent中,如:

1
2
3
4
ssh-add ~/.ssh/jslite_rsa
ssh-add -l # 查看所有的key
ssh-add -D # 删除所有的key
ssh-add -d ~/.ssh/jslite_rsa # 删除指定的key

免密码登录远程服务器

1
2
$ ssh-keygen -t rsa -P '' -f ~/.ssh/aliyunserver.key
$ ssh-copy-id -i ~/.ssh/aliyunserver.key.pub root@192.168.182.112 # 这里需要输入密码一次

编辑 ~/.ssh/config

1
2
3
4
5
Host aliyun1
HostName 192.168.182.112
User root
PreferredAuthentications publickey
IdentityFile ~/.ssh/aliyunserver.key

上面配置完了,可以通过命令登录,不需要输入IP地址和密码 ssh aliyun1

https协议下提交代码免密码

1
git clone https://github.com/username/rep.git

通过上面方式克隆可能需要密码,解决办法:进入当前克隆的项目 vi rep/.git/config 编辑 config, 按照下面方式修改,你就可以提交代码不用输入密码了。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
ignorecase = true
precomposeunicode = true
[remote "origin"]
- url = https://github.com/username/rep.git
+ url = https://用户名:密码@github.com/username/rep.git
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master

文件推向3个git库

1. 增加3个远程库地址

1
2
3
git remote add origin https://github.com/JSLite/JSLite.git  
git remote set-url --add origin https://gitlab.com/wang/JSLite.js.git
git remote set-url --add origin https://oschina.net/wang/JSLite.js.git

2. 删除其中一个 set-url 地址

1
2
3
usage: git remote set-url [--push] <name> <newurl> [<oldurl>]
or: git remote set-url --add <name> <newurl>
or: git remote set-url --delete <name> <url>

git remote set-url --delete origin https://oschina.net/wang/JSLite.js.git

3.推送代码

1
2
git push origin master
git push -f origin master # 强制推送

4.拉代码

只能拉取 origin 里的一个url地址,这个fetch-url
默认为你添加的到 origin的第一个地址

1
2
3
4
5
6
7
8
git pull origin master   
git pull --all # 获取远程所有内容包括tag
git pull origin next:master # 取回origin主机的next分支,与本地的master分支合并
git pull origin next # 远程分支是与当前分支合并

# 上面一条命令等同于下面两条命令
git fetch origin
git merge origin/next

如果远程主机删除了某个分支,默认情况下,git pull 不会在拉取远程分支的时候,删除对应的本地分支。这是为了防止,由于其他人操作了远程主机,导致git pull不知不觉删除了本地分支。
但是,你可以改变这个行为,加上参数 -p 就会在本地删除远程已经删除的分支。

1
2
3
4
$ git pull -p
# 等同于下面的命令
$ git fetch --prune origin
$ git fetch -p

5.更改pull

只需要更改config文件里,那三个url的顺序即可,fetch-url会直接对应排行第一的那个utl连接。

修改远程仓库地址

1
2
git remote remove origin  # 删除该远程路径  
git remote add origin git@jslite.github.com:JSLite/JSLite.git # 添加远程路径

撤销远程记录

1
2
git reset --hard HEAD~1 # 撤销一条记录   
git push -f origin HEAD:master # 同步到远程仓库

放弃本地的文件修改

1
git reset --hard FETCH_HEAD # FETCH_HEAD表示上一次成功git pull之后形成的commit点。然后git pull

git reset --hard FETCH_HEAD 出现错误

1
2
3
4
5
6
git pull
You are not currently on a branch, so I cannot use any
'branch.<branchname>.merge' in your configuration file.
Please specify which remote branch you want to use on the command
line and try again (e.g. 'git pull <repository> <refspec>').
See git-pull(1) FOR details.

解决方法:

1
2
git checkout -b temp # 新建+切换到temp分支 
git checkout master

最简单放弃本地修改内容

1
2
3
4
5
6
# 如果有的修改以及加入暂存区的话
git reset --hard
# 还原所有修改,不会删除新增的文件
git checkout .
# 下面命令会删除新增的文件
git clean -xdf

通过存储暂存区stash,在删除暂存区的方法放弃本地修改。

1
git stash && git stash drop

回滚到某个commit提交

1
2
git revert HEAD~1 # 撤销一条记录 会弹出 commit 编辑
git push # 提交回滚

回退到某一个版本

1
2
3
4
git reset --hard <hash>
# 例如 git reset --hard a3hd73r
# --hard代表丢弃工作区的修改,让工作区与版本代码一模一样,与之对应,
# --soft参数代表保留工作区的修改。

去掉某个commit

1
2
# 实质是新建了一个与原来完全相反的commit,抵消了原来commit的效果
git revert <commit-hash>

新建一个空分支

1
2
3
4
5
6
# 这种方式新建的分支(gh-pages)是没有 commit 记录的
git checkout --orphan gh-pages
# 删除新建的gh-pages分支原本的内容,如果不删除,提交将作为当前分支的第一个commit
git rm -rf .
# 查看一下状态 有可能上面一条命令,没有删除还没有提交的的文件
git state

合并多个commit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 这个命令,将最近4个commit合并为1个,HEAD代表当前版本。
# 将进入VIM界面,你可以修改提交信息。
git rebase -i HEAD~4
# 可以看到其中分为两个部分,上方未注释的部分是填写要执行的指令,
# 而下方注释的部分则是指令的提示说明。指令部分中由前方的命令名称、commit hash 和 commit message 组成
# 当前我们只要知道 pick 和 squash 这两个命令即可。
# --> pick 的意思是要会执行这个 commit
# --> squash 的意思是这个 commit 会被合并到前一个commit

# 我们将 需要保留的 这个 commit 前方的命令改成 squash 或 s,然后输入:wq以保存并退出
# 这是我们会看到 commit message 的编辑界面

# 其中, 非注释部分就是两次的 commit message, 你要做的就是将这两个修改成新的 commit message。
#
# 输入wq保存并推出, 再次输入git log查看 commit 历史信息,你会发现这两个 commit 已经合并了。
# 将修改强制推送到前端
git push -f origin master

修改远程Commit记录

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
git commit --amend
# amend只能修改没有提交到线上的,最后一次commit记录
git rebase -i HEAD~3
# 表示要修改当前版本的倒数第三次状态
# 将要更改的记录行首单词 pick 改为 edit
pick 96dc3f9 doc: Update quick-start.md
pick f1cce8a test(Transition):Add transition test (#47)
pick 6293516 feat(Divider): Add Divider component.
# Rebase eeb03a4..6293516 onto eeb03a4 (3 commands)
#
# Commands:
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit

保存并退出,会弹出下面提示

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# You can amend the commit now, with
#
# git commit --amend
#
# Once you are satisfied with your changes, run
#
# git rebase --continue

# 通过这条命令进入编辑页面更改commit,保存退出
git commit --amend
# 保存退出确认修改,继续执行 rebase,
git rebase --continue
# 如果修改多条记录反复执行上面两条命令直到完成所有修改

# 最后,确保别人没有提交进行push,最好不要加 -f 强制推送
git push -f origin master

添加忽略文件

1
echo node_modules/ >> .gitignore

利用commit关闭一个issue

这个功能在Github上可以玩儿,Gitlab上特别老的版本不能玩儿哦,那么如何跟随着commit关闭一个issue呢? 在confirm merge的时候可以使用一下命令来关闭相关issue:

fixes #xxxfixed #xxxfix #xxxcloses #xxxclose #xxxclosed #xxx

同步fork的上游仓库

Github教程同步fork教程在Github上同步一个分支(fork)

设置添加多个远程仓库地址。

在同步之前,需要创建一个远程点指向上游仓库(repo).如果你已经派生了一个原始仓库,可以按照如下方法做。

1
2
3
4
5
6
7
8
9
10
11
12
$ git remote -v
# List the current remotes (列出当前远程仓库)
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
$ git remote add upstream https://github.com/otheruser/repo.git
# Set a new remote (设置一个新的远程仓库)
$ git remote -v
# Verify new remote (验证新的原唱仓库)
# origin https://github.com/user/repo.git (fetch)
# origin https://github.com/user/repo.git (push)
# upstream https://github.com/otheruser/repo.git (fetch)
# upstream https://github.com/otheruser/repo.git (push)

同步更新仓库内容

同步上游仓库到你的仓库需要执行两步:首先你需要从远程拉去,之后你需要合并你希望的分支到你的本地副本分支。从上游的存储库中提取分支以及各自的提交内容。 master 将被存储在本地分支机构 upstream/master

1
2
3
4
5
6
7
git fetch upstream
# remote: Counting objects: 75, done.
# remote: Compressing objects: 100% (53/53), done.
# remote: Total 62 (delta 27), reused 44 (delta 9)
# Unpacking objects: 100% (62/62), done.
# From https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY
# * [new branch] master -> upstream/master

检查你的 fork’s 本地 master 分支

1
2
git checkout master
# Switched to branch 'master'

合并来自 upstream/master 的更改到本地 master 分支上。 这使你的前 fork’s master 分支与上游资源库同步,而不会丢失你本地修改。

1
2
3
4
5
6
7
8
git merge upstream/master
# Updating a422352..5fdff0f
# Fast-forward
# README | 9 -------
# README.md | 7 ++++++
# 2 files changed, 7 insertions(+), 9 deletions(-)
# delete mode 100644 README
# create mode 100644 README.md

批量修改历史commit中的名字和邮箱

1.克隆仓库

注意参数,这个不是普通的clone,clone下来的仓库并不能参与开发

1
2
git clone --bare https://github.com/user/repo.git
cd repo.git

2.命令行中运行代码

OLD_EMAIL原来的邮箱
CORRECT_NAME更正的名字
CORRECT_EMAIL更正的邮箱

将下面代码复制放到命令行中执行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
git filter-branch -f --env-filter '
OLD_EMAIL="wowohoo@qq.com"
CORRECT_NAME="小弟调调"
CORRECT_EMAIL="更正的邮箱@qq.com"
if [ "$GIT_COMMITTER_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_COMMITTER_NAME="$CORRECT_NAME"
export GIT_COMMITTER_EMAIL="$CORRECT_EMAIL"
fi
if [ "$GIT_AUTHOR_EMAIL" = "$OLD_EMAIL" ]
then
export GIT_AUTHOR_NAME="$CORRECT_NAME"
export GIT_AUTHOR_EMAIL="$CORRECT_EMAIL"
fi
' --tag-name-filter cat -- --branches --tags

执行过程

1
2
3
Rewrite 160d4df2689ff6df3820563bfd13b5f1fb9ba832 (479/508) (16 seconds passed, remaining 0 predicted)
Ref 'refs/heads/dev' was rewritten
Ref 'refs/heads/master' was rewritten

3.同步到远程仓库

同步到push远程git仓库

1
git push --force --tags origin 'refs/heads/*'

我还遇到了如下面错误,lab默认给master分支加了保护,不允许强制覆盖。Project(项目)->Setting->Repository 菜单下面的Protected branches把master的保护去掉就可以了。修改完之后,建议把master的保护再加回来,毕竟强推不是件好事。

1
remote: GitLab: You are not allowed to force push code to a protected branch on this project.

当上面的push 不上去的时候,先 git pull 确保最新代码

1
2
3
git pull  --allow-unrelated-histories
# 或者指定分枝
git pull origin master --allow-unrelated-histories

查看某个文件历史

1
2
3
4
5
git log --pretty=oneline 文件名  # 列出文件的所有改动历史  
git show c178bf49 # 某次的改动的修改记录
git log -p c178bf49 # 某次的改动的修改记录
git blame 文件名 # 显示文件的每一行是在那个版本最后修改。
git whatchanged 文件名 # 显示某个文件的每个版本提交信息:提交日期,提交人员,版本号,提交备注(没有修改细节)

打造自己的git命令

1
2
3
4
git config --global alias.st status
git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.ci commit

配置好后再输入git命令的时候就不用再输入一大段了,例如我们要查看状态,只需:

1
git st

中文乱码的解决方案

1
git config --global core.quotepath false

新建仓库

init

git init #初始化

status

git status #获取状态

add

git add file # .或*代表全部添加
git rm --cached <added_file_to_undo> # 在commit之前撤销git add操作
git reset head # 好像比上面git rm --cached更方便

commit

git commit -m "message" #此处注意乱码

remote

git remote add origin git@github.com:JSLite/test.git #添加源

push

1
2
3
git push -u origin master # push同事设置默认跟踪分支  
git push origin master
git push -f origin master # 强制推送文件,缩写 -f(全写--force)

clone

git clone git://github.com/JSLite/JSLite.js.git
git clone git://github.com/JSLite/JSLite.js.git mypro #克隆到自定义文件夹
git clone [user@]example.com:path/to/repo.git/ #SSH协议还有另一种写法。

git clone支持多种协议,除了HTTP(s)以外,还支持SSH、Git、本地文件协议等,下面是一些例子。git clone <版本库的网址> <本地目录名>

1
2
3
4
5
6
7
$ git clone http[s]://example.com/path/to/repo.git/
$ git clone ssh://example.com/path/to/repo.git/
$ git clone git://example.com/path/to/repo.git/
$ git clone /opt/git/project.git
$ git clone file:///opt/git/project.git
$ git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/

本地

help

1
git help config # 获取帮助信息

add

1
2
git add *   # 跟踪新文件   
git add -u [path] # 添加[指定路径下]已跟踪文件

rm

1
2
3
4
5
rm *&git rm *          # 移除文件  
git rm -f * # 移除文件
git rm --cached * # 取消跟踪
git mv file_from file_to # 重命名跟踪文件
git log # 查看提交记录

commit

1
2
3
4
5
6
7
8
git commit #提交更新   
git commit -m 'message' #提交说明
git commit -a #跳过使用暂存区域,把所有已经跟踪过的文件暂存起来一并提交
git commit --amend #修改最后一次提交
git commit log #查看所有提交,包括没有push的commit
git commit -m "#133" #关联issue 任意位置带上# 符号加上issue号码
git commit -m "fix #133" commit关闭issue
git commit -m '概要描述'$'\n\n''1.详细描述'$'\n''2.详细描述' #提交简要描述和详细描述

reset

1
2
3
4
5
git reset HEAD *  # 取消已经暂存的文件   
git reset --mixed HEAD * # 同上
git reset --soft HEAD * # 重置到指定状态,不会修改索引区和工作树
git reset --hard HEAD * # 重置到指定状态,会修改索引区和工作树
git reset -- files * # 重置index区文件

revert

1
2
3
git revert HEAD   # 撤销前一次操作   
git revert HEAD~ # 撤销前前一次操作
git revert commit # 撤销指定操作

checkout

1
2
3
4
5
git checkout -- file  # 取消对文件的修改(从暂存区——覆盖worktree file)  
git checkout branch|tag|commit -- file_name # 从仓库取出file覆盖当前分支
git checkout HEAD~1 [文件] # 将会更新 working directory 去匹配某次 commit
git checkout -- . # 从暂存区取出文件覆盖工作区
git checkout -b gh-pages 0c304c9 # 这个表示 从当前分支 commit 哈希值为 0c304c9 的节点,分一个新的分支gh-pages出来,并切换到 gh-pages

diff

1
2
3
4
5
6
7
8
9
10
git diff file     # 查看指定文件的差异   
git diff --stat # 查看简单的diff结果
git diff # 比较Worktree和Index之间的差异
git diff --cached # 比较Index和HEAD之间的差异
git diff HEAD # 比较Worktree和HEAD之间的差异
git diff branch # 比较Worktree和branch之间的差异
git diff branch1 branch2 # 比较两次分支之间的差异
git diff commit commit # 比较两次提交之间的差异
git diff master..test # 上面这条命令只显示两个分支间的差异
git diff master...test # 你想找出‘master’,‘test’的共有 父分支和'test'分支之间的差异,你用3个‘.'来取代前面的两个'.'

stash

1
2
3
4
5
6
git stash # 将工作区现场(已跟踪文件)储藏起来,等以后恢复后继续工作。   
git stash list # 查看保存的工作现场
git stash apply # 恢复工作现场
git stash drop # 删除stash内容
git stash pop # 恢复的同时直接删除stash内容
git stash apply stash@{0} # 恢复指定的工作现场,当你保存了不只一份工作现场时。

merge

1
git merge --squash test # 合并压缩,将test上的commit压缩为一条

cherry-pick

1
2
git cherry-pick commit    # 拣选合并,将commit合并到当前分支   
git cherry-pick -n commit # 拣选多个提交,合并完后可以继续拣选下一个提交

rebase

1
2
3
4
5
6
git rebase master   # 将master分之上超前的提交,变基到当前分支  
git rebase --onto master 169a6 # 限制回滚范围,rebase当前分支从169a6以后的提交
git rebase --interactive # 交互模式,修改commit
git rebase --continue # 处理完冲突继续合并
git rebase --skip # 跳过
git rebase --abort # 取消合并

分支branch

删除

1
2
3
4
5
6
git push origin :branchName  # 删除远程分支  
git push origin --delete new # 删除远程分支new
git branch -d branchName # 删除本地分支,强制删除用-D
git branch -d test # 删除本地test分支
git branch -D test # 强制删除本地test分支
git remote prune origin # 远程删除了,本地还能看到远程存在,这条命令删除远程不存在的分支

提交

1
git push -u origin branchName # 提交分支到远程origin主机中

拉取

git fetch -p #拉取远程分支时,自动清理 远程分支已删除,本地还存在的对应同名分支。

分支合并

1
2
3
4
git merge branchName      # 合并分支 - 将分支branchName和当前所在分支合并   
git merge origin/master # 在本地分支上合并远程分支。
git rebase origin/master # 在本地分支上合并远程分支。
git merge test # 将test分支合并到当前分支

重命名

git branch -m old new #重命名分支

查看

1
2
3
4
5
6
7
git branch      # 列出本地分支   
git branch -r # 列出远端分支
git branch -a # 列出所有分支
git branch -v # 查看各个分支最后一个提交对象的信息
git branch --merge # 查看已经合并到当前分支的分支
git branch --no-merge # 查看为合并到当前分支的分支
git remote show origin # 可以查看remote地址,远程分支

新建

1
2
3
4
git branch test # 新建test分支  
git branch newBrach 3defc69 # 指定哈希3defc69,新建分支名字为newBrach
git checkout -b newBrach origin/master # 取回远程主机的更新以后,在它的基础上创建一个新的分支
git checkout -b newBrach 3defc69 # 以哈希值3defc69,新建 newBrach 分支,并切换到该分支

连接

1
2
git branch --set-upstream dev origin/dev     # 将本地dev分支与远程dev分支之间建立链接  
git branch --set-upstream master origin/next # 手动建立追踪关系

分支切换

1
2
3
git checkout test     # 切换到test分支   
git checkout -b test # 新建+切换到test分支
git checkout -b test dev # 基于dev新建test分支,并切换

远端

1
2
3
4
5
6
7
8
git fetch <远程主机名> <分支名>   # fetch取回所有分支(branch)的更新  
git fetch origin remotebranch[:localbranch] # 从远端拉去分支[到本地指定分支]
git merge origin/branch # 合并远端上指定分支
git pull origin remotebranch:localbranch # 拉去远端分支到本地分支
git push origin branch # 将当前分支,推送到远端上指定分支
git push origin localbranch:remotebranch # 推送本地指定分支,到远端上指定分支
git push origin :remotebranch # 删除远端指定分支
git checkout -b [--track] test origin/dev # 基于远端dev分支,新建本地test分支[同时设置跟踪]

submodule

克隆项目同时克隆submodule

1
git clone https://github.com/jaywcjlove/handbook.git --depth=1 --recurse-submodules

克隆项目,之后再手动克隆 submodule 子项目

1
2
3
4
5
6
git submodule add --force '仓库地址' '路径'
# 其中,仓库地址是指子模块仓库地址,路径指将子模块放置在当前工程下的路径。
# 注意:路径不能以 / 结尾(会造成修改不生效)、不能是现有工程已有的目录(不能順利 Clone)
git submodule init # 初始化submodule
git submodule update # 更新submodule(必须在根目录执行命令)
git submodule update --init --recursive # 下载的工程带有submodule

当使用git clone下来的工程中带有submodule时,初始的时候,submodule的内容并不会自动下载下来的,此时,只需执行如下命令:

1
2
3
4
5
git submodule foreach git pull  # submodule 里有其他的 submodule 一次更新
git submodule foreach git pull origin master # submodule更新

git submodule foreach --recursive git submodule init
git submodule foreach --recursive git submodule update

删除文件

1
git rm -rf node_modules/

remote

git是一个分布式代码管理工具,所以可以支持多个仓库,在git里,服务器上的仓库在本地称之为remote。个人开发时,多源用的可能不多,但多源其实非常有用。

1
2
3
4
5
6
git remote add origin1 git@github.com:yanhaijing/data.js.git  
git remote # 显示全部源
git remote -v # 显示全部源+详细信息
git remote rename origin1 origin2 # 重命名
git remote rm origin # 删除
git remote show origin # 查看指定源的全部信息

标签tag

当开发到一定阶段时,给程序打标签是非常棒的功能。

1
2
3
4
5
6
7
8
9
10
11
12
13
git tag -a v0.1 -m 'my version 1.4' # 新建带注释标签   
git push origin --tags # 一次性推送所有分支
git push origin v1.5 # 推送单个tag到orgin源上
git tag -v v1.4.2.1 # 验证标签,验证已经签署的标签
git show v1.5 # 看到对应的 GPG 签

git tag # 列出现有标签
git tag v0gi.1 # 新建标签
git checkout tagname # 切换到标签
git tag -d v0.1 # 删除标签
git push origin :refs/tags/v0.1 # 删除远程标签
git pull --all # 获取远程所有内容包括tag
git --git-dir='<绝对地址>/.git' describe --tags HEAD # 查看本地版本信息

日志log

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
git config format.pretty oneline  #显示历史记录时,每个提交的信息只显示一行   
git config color.ui true #彩色的 git 输出
git log #查看最近的提交日志
git log --pretty=oneline #单行显示提交日志
git log --graph --pretty=oneline --abbrev-commit
git log -num #显示第几条log(倒数)
git reflog #查看所有分支的所有操作记录
git log --since=1.day #一天内的提交;你可以给出各种时间格式,比如说具体的某一天(“2008-01-15”),或者是多久以前(“2 years 1 day 3 minutes ago”)。
git log --pretty="%h - %s" --author=自己的名字 #查看自己的日志
git log -p -2 #展开两次更新显示每次提交的内容差异
git log --stat #要快速浏览其他协作者提交的更新都作了哪些改动
git log --pretty=format:"%h - %an, %ar : %s"#定制要显示的记录格式
git log --pretty=format:'%h : %s' --date-order --graph # 拓扑顺序展示
git log --pretty=format:'%h : %s - %ad' --date=short #日期YYYY-MM-DD显示
git log <last tag> HEAD --pretty=format:%s # 只显示commit
git config --global format.pretty '%h : %s - %ad' --date=short #日期YYYY-MM-DD显示 写入全局配置
选项 说明 选项 说明
%H 提交对象(commit)的完整哈希字串 %ad 作者修订日期(可以用 -date= 选项定制格式)
%h 提交对象的简短哈希字串 %ar 作者修订日期,按多久以前的方式显示
%T 树对象(tree)的完整哈希字串 %cn 提交者(committer)的名字
%t 树对象的简短哈希字串 %ce 提交者的电子邮件地址
%P 父对象(parent)的完整哈希字串 %cd 提交日期
%p 父对象的简短哈希字串 %cr 提交日期,按多久以前的方式显示
%an 作者(author)的名字 %s 提交说明
%ae 作者的电子邮件地址 - -

Pretty Formats

重写历史

1
2
3
4
git commit --amend    # 改变最近一次提交  
git rebase -i HEAD~3 # 修改最近三次的提交说明,或者其中任意一次
git commit --amend # 保存好了,这些指示很明确地告诉了你该干什么
git rebase --continue # 修改提交说明,退出编辑器。
1
2
3
pick f7f3f6d changed my name a bit
pick 310154e updated README formatting and added blame
pick a5f4a0d added cat-file

改成

1
2
pick 310154e updated README formatting and added blame
pick f7f3f6d changed my name a bit

删除仓库

1
2
cd ..
rm -rf repo.git

Github官方教程

其它

1
2
git help *  # 获取命令的帮助信息  
git status # 获取当前的状态,非常有用,因为git会提示接下来的能做的操作

报错问题解决

1. git fatal: protocol error: bad line length character: No s

解决办法:更换remote地址为 http/https

2. The requested URL returned error: 403 Forbidden while accessing

解决github push错误的办法:

1
2
3
4
5
6
7
8
9
10
#vim 编辑器打开 当前项目中的config文件
vim .git/config

#修改
[remote "origin"]
url = https://github.com/jaywcjlove/example.git

#为下面代码
[remote "origin"]
url = https://jaywcjlove@github.com/jaywcjlove/example.git

3. git status 显示中文问题

在查看状态的时候 git status 如果是中文就显示下面的情况

1
\344\272\247\345\223\201\351\234\200\346\261\202

解决这个问题方法是:

1
git config --global core.quotepath false

参考资料

get_module

获取Linux内核模块的详细信息

补充说明

get_module命令 用于获取Linux内核模块的详细信息。

语法

1
get_module 模块名

实例

使用lsmod命令查看内核模块:

1
2
3
4
5
6
lsmod | head -5
Module Size Used by
ipv6 272801 15
xfrm_nalgo 13381 1 ipv6
crypto_api 12609 1 xfrm_nalgo
ip_conntrack_ftp 11569 0

使用get_module命令查看模块详细信息:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
get_module ipv6
refcnt : 15
srcversion : 8CC9C024755B4483E56C0EF

Parameters:
autoconf : 1
disable : 0
disable_ipv6 : 0
Sections:
.altinstr_replacement : 0xf8f1a3cf
.altinstructions : 0xf8f1d03c
.bss : 0xf8f36000
.data.read_mostly : 0xf8f34d20
.data : 0xf8f2f7a0
.exit.text : 0xf8f1a234
.gnu.linkonce.this_module : 0xf8f34e00
.init.data : 0xf8a16a60
.init.text : 0xf8a16000
.module_sig : 0xf8f37960
.rodata.str1.1 : 0xf8f1ae46
.rodata : 0xf8f1a420
.smp_locks : 0xf8f1d150
.strtab : 0xf8f29840
.symtab : 0xf8f24000
.text : 0xf8ef5000
__kcrctab : 0xf8f1de70
__kcrctab_gpl : 0xf8f1d9cc
__ksymtab : 0xf8f1dd78
__ksymtab_gpl : 0xf8f1d954
__ksymtab_strings : 0xf8f1da44
__param : 0xf8f1da08
__versions : 0xf8f1df00

getsebool

查询SElinux策略内各项规则的布尔值

补充说明

getsebool命令 是用来查询SElinux策略内各项规则的布尔值。SELinux的策略与规则管理相关命令:seinfo命令、sesearch命令、getsebool命令、setsebool命令、semanage命令。

语法

1
getsebool [-a] [布尔值条款]

选项

1
-a:列出目前系统上面的所有布尔值条款设置为开启或关闭值。

实例

查询本系统内所有的布尔值设置状况:

1
2
3
4
5
6
getsebool -a
NetworkManager_disable_trans --> off
allow_console_login --> off
allow_cvs_read_shadow --> off
allow_daemons_dump_core --> on
....(底下省略)....

查询httpd_enable_homedirs是否为关闭,若没关闭,请关闭它:

1
2
getsebool httpd_enable_homedirs
setsebool -P httpd_enable_homedirs=0 //0是关闭 1是开启

getenforce

显示当前SELinux的应用模式,是强制、执行还是停用。

补充说明

grename命令 可以重命名卷组的名称。

语法

1
getenforce

例子

查看当前SELinux的应用模式。

1
2
[root@localhost ~]# getenforce
Enforcing

gdb

功能强大的程序调试器

补充说明

gdb命令 包含在GNU的gcc开发套件中,是功能强大的程序调试器。GDB中的命令固然很多,但我们只需掌握其中十个左右的命令,就大致可以完成日常的基本的程序调试工作。































































































































命令解释示例
file <文件名>加载被调试的可执行程序文件。
因为一般都在被调试程序所在目录下执行GDB,因而文本名不需要带路径。
(gdb) file gdb-sample
rRun的简写,运行被调试的程序。
如果此前没有下过断点,则执行完整个程序;如果有断点,则程序暂停在第一个可用断点处。
(gdb) r
cContinue的简写,继续执行被调试程序,直至下一个断点或程序结束。(gdb) c
b <行号>
b <函数名称>
b <函数名称>
b
<代码地址> d [编号]
b: Breakpoint的简写,设置断点。两可以使用“行号”“函数名称”“执行地址”等方式指定断点位置。
其中在函数名称前面加“”符号表示将断点设置在“由编译器生成的prolog代码处”。如果不了解汇编,可以不予理会此用法。 d: Delete breakpoint的简写,删除指定编号的某个断点,或删除所有断点。断点编号从1开始递增。
(gdb) b 8
(gdb) b main
(gdb) b main
(gdb) b *0x804835c (gdb) d
s, ns: 执行一行源程序代码,如果此行代码中有函数调用,则进入该函数;
n: 执行一行源程序代码,此行代码中的函数调用也一并执行。 s 相当于其它调试器中的“Step Into (单步跟踪进入)”;
n 相当于其它调试器中的“Step Over (单步跟踪)”。 这两个命令必须在有源代码调试信息的情况下才可以使用(GCC编译时使用“-g”参数)。
(gdb) s
(gdb) n
si, nisi命令类似于s命令,ni命令类似于n命令。所不同的是,这两个命令(si/ni)所针对的是汇编指令,而s/n针对的是源代码。(gdb) si
(gdb) ni
p <变量名称>Print的简写,显示指定变量(临时变量或全局变量)的值。(gdb) p i
(gdb) p nGlobalVar
display … undisplay <编号>display,设置程序中断后欲显示的数据及其格式。
例如,如果希望每次程序中断后可以看到即将被执行的下一条汇编指令,可以使用命令
“display /i $pc”
其中 $pc 代表当前汇编指令,/i 表示以十六进行显示。当需要关心汇编代码时,此命令相当有用。 undispaly,取消先前的display设置,编号从1开始递增。
(gdb) display /i $pc (gdb) undisplay 1
iinfo的简写,用于显示各类信息,详情请查阅“help i”。(gdb) i r
qQuit的简写,退出GDB调试环境。(gdb) q
help [命令名称]GDB帮助命令,提供对GDB名种命令的解释说明。
如果指定了“命令名称”参数,则显示该命令的详细说明;如果没有指定参数,则分类显示所有GDB命令,供用户进一步浏览和查询。
(gdb) help

语法

1
gdb(选项)(参数)

选项

1
2
3
4
5
-cd:设置工作目录;
-q:安静模式,不打印介绍信息和版本信息;
-d:添加文件查找路径;
-x:从指定文件中执行GDB指令;
-s:设置读取的符号表文件。

参数

文件:二进制可执行程序。

实例

以下是linux下dgb调试的一个实例,先给出一个示例用的小程序,C语言代码:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include <stdio.h>
int nGlobalVar = 0;

int tempFunction(int a, int b)
{
printf("tempFunction is called, a = %d, b = %d /n", a, b);
return (a + b);
}

int main()
{
int n;
n = 1;
n++;
n--;

nGlobalVar += 100;
nGlobalVar -= 12;

printf("n = %d, nGlobalVar = %d /n", n, nGlobalVar);

n = tempFunction(1, 2);
printf("n = %d", n);

return 0;
}

请将此代码复制出来并保存到文件 gdb-sample.c 中,然后切换到此文件所在目录,用GCC编译之:

1
gcc gdb-sample.c -o gdb-sample -g

在上面的命令行中,使用 -o 参数指定了编译生成的可执行文件名为 gdb-sample,使用参数 -g 表示将源代码信息编译到可执行文件中。如果不使用参数 -g,会给后面的GDB调试造成不便。当然,如果我们没有程序的源代码,自然也无从使用 -g 参数,调试/跟踪时也只能是汇编代码级别的调试/跟踪。

下面“gdb”命令启动GDB,将首先显示GDB说明,不管它:

1
2
3
4
5
6
7
8
GNU gdb Red Hat Linux (5.3post-0.20021129.18rh)
Copyright 2003 free Software Foundation, Inc.
GDB is free software, covered by the GNU General Public License, and you are
welcome to change it and/or distribute copies of it under certain conditions.
type "show copying" to see the conditions.
There is absolutely no warranty for GDB. Type "show warranty" for details.
This GDB was configured as "i386-redhat-linux-gnu".
(gdb)

上面最后一行“(gdb)”为GDB内部命令引导符,等待用户输入GDB命令。

下面使用“file”命令载入被调试程序 gdb-sample(这里的 gdb-sample 即前面 GCC 编译输出的可执行文件):

1
2
(gdb) file gdb-sample
Reading symbols from gdb-sample...done.

上面最后一行提示已经加载成功。

下面使用“r”命令执行(Run)被调试文件,因为尚未设置任何断点,将直接执行到程序结束:

1
2
3
4
5
6
(gdb) r
Starting program: /home/liigo/temp/test_jmp/test_jmp/gdb-sample
n = 1, nGlobalVar = 88
tempFunction is called, a = 1, b = 2
n = 3
Program exited normally.

下面使用“b”命令在 main 函数开头设置一个断点(Breakpoint):

1
2
(gdb) b main
Breakpoint 1 at 0x804835c: file gdb-sample.c, line 19.

上面最后一行提示已经成功设置断点,并给出了该断点信息:在源文件 gdb-sample.c 第19行处设置断点;这是本程序的第一个断点(序号为1);断点处的代码地址为 0x804835c(此值可能仅在本次调试过程中有效)。回过头去看源代码,第19行中的代码为“n = 1”,恰好是 main 函数中的第一个可执行语句(前面的“int n;”为变量定义语句,并非可执行语句)。

再次使用“r”命令执行(Run)被调试程序:

1
2
3
4
5
(gdb) r
Starting program: /home/liigo/temp/gdb-sample

Breakpoint 1, main () at gdb-sample.c:19
19 n = 1;

程序中断在gdb-sample.c第19行处,即main函数是第一个可执行语句处。

上面最后一行信息为:下一条将要执行的源代码为“n = 1;”,它是源代码文件gdb-sample.c中的第19行。

下面使用“s”命令(Step)执行下一行代码(即第19行“n = 1;”):

1
2
(gdb) s
20 n++;

上面的信息表示已经执行完“n = 1;”,并显示下一条要执行的代码为第20行的“n++;”。

既然已经执行了“n = 1;”,即给变量 n 赋值为 1,那我们用“p”命令(Print)看一下变量 n 的值是不是 1 :

1
2
(gdb) p n
$1 = 1

果然是 1。($1大致是表示这是第一次使用“p”命令——再次执行“p n”将显示“$2 = 1”——此信息应该没有什么用处。)

下面我们分别在第26行、tempFunction 函数开头各设置一个断点(分别使用命令“b 26”“b tempFunction”):

1
2
3
4
(gdb) b 26
Breakpoint 2 at 0x804837b: file gdb-sample.c, line 26.
(gdb) b tempFunction
Breakpoint 3 at 0x804832e: file gdb-sample.c, line 12.

使用“c”命令继续(Continue)执行被调试程序,程序将中断在第二 个断点(26行),此时全局变量 nGlobalVar 的值应该是 88;再一次执行“c”命令,程序将中断于第三个断点(12行,tempFunction 函数开头处),此时tempFunction 函数的两个参数 a、b 的值应分别是 1 和 2:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(gdb) c
Continuing.

Breakpoint 2, main () at gdb-sample.c:26
26 printf("n = %d, nGlobalVar = %d /n", n, nGlobalVar);
(gdb) p nGlobalVar
$2 = 88
(gdb) c
Continuing.
n = 1, nGlobalVar = 88

Breakpoint 3, tempFunction (a=1, b=2) at gdb-sample.c:12
12 printf("tempFunction is called, a = %d, b = %d /n", a, b);
(gdb) p a
$3 = 1
(gdb) p b
$4 = 2

上面反馈的信息一切都在我们预料之中~~

再一次执行“c”命令(Continue),因为后面再也没有其它断点,程序将一直执行到结束:

1
2
3
4
5
(gdb) c
Continuing.
tempFunction is called, a = 1, b = 2
n = 3
Program exited normally.

有时候需要看到编译器生成的汇编代码,以进行汇编级的调试或跟踪,又该如何操作呢?

这就要用到display命令“display /i $pc”了(此命令前面已有详细解释):

1
2
(gdb) display /i $pc
(gdb)

此后程序再中断时,就可以显示出汇编代码了:

1
2
3
4
5
6
(gdb) r
Starting program: /home/liigo/temp/test_jmp/test_jmp/gdb-sample

Breakpoint 1, main () at gdb-sample.c:19
19 n = 1;
1: x/i $pc 0x804835c <main+16>: movl $0x1,0xfffffffc(%ebp)

看到了汇编代码,“n = 1;”对应的汇编代码是“movl $0x1,0xfffffffc(%ebp)”。

并且以后程序每次中断都将显示下一条汇编指定(“si”命令用于执行一条汇编代码——区别于“s”执行一行C代码):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
(gdb) si
20 n++;
1: x/i $pc 0x8048363 <main+23>: lea 0xfffffffc(%ebp),%eax
(gdb) si
0x08048366 20 n++;
1: x/i $pc 0x8048366 <main+26>: incl (%eax)
(gdb) si
21 n--;
1: x/i $pc 0x8048368 <main+28>: lea 0xfffffffc(%ebp),%eax
(gdb) si
0x0804836b 21 n--;
1: x/i $pc 0x804836b <main+31>: decl (%eax)
(gdb) si
23 nGlobalVar += 100;
1: x/i $pc 0x804836d <main+33>: addl $0x64,0x80494fc

接下来我们试一下命令“b *<函数名称>”。

为了更简明,有必要先删除目前所有断点(使用“d”命令——Delete breakpoint):

1
2
3
(gdb) d
Delete all breakpoints? (y or n) y
(gdb)

当被询问是否删除所有断点时,输入“y”并按回车键即可。

下面使用命令“b *main”在 main 函数的 prolog 代码处设置断点(prolog、epilog,分别表示编译器在每个函数的开头和结尾自行插入的代码):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
(gdb) b *main
Breakpoint 4 at 0x804834c: file gdb-sample.c, line 17.
(gdb) r
The program being debugged has been started already.
Start it from the beginning? (y or n) y
Starting program: /home/liigo/temp/test_jmp/test_jmp/gdb-sample

Breakpoint 4, main () at gdb-sample.c:17
17 {
1: x/i $pc 0x804834c <main>: push %ebp
(gdb) si
0x0804834d 17 {
1: x/i $pc 0x804834d <main+1>: mov %esp,%ebp
(gdb) si
0x0804834f in main () at gdb-sample.c:17
17 {
1: x/i $pc 0x804834f <main+3>: sub $0x8,%esp
(gdb) si
0x08048352 17 {
1: x/i $pc 0x8048352 <main+6>: and $0xfffffff0,%esp
(gdb) si
0x08048355 17 {
1: x/i $pc 0x8048355 <main+9>: mov $0x0,%eax
(gdb) si
0x0804835a 17 {
1: x/i $pc 0x804835a <main+14>: sub %eax,%esp
(gdb) si
19 n = 1;
1: x/i $pc 0x804835c <main+16>: movl $0x1,0xfffffffc(%ebp)

此时可以使用“i r”命令显示寄存器中的当前值———“i r”即“Infomation Register”:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
(gdb) i r
eax 0xbffff6a4 -1073744220
ecx 0x42015554 1107383636
edx 0x40016bc8 1073834952
ebx 0x42130a14 1108544020
esp 0xbffff6a0 0xbffff6a0
ebp 0xbffff6a8 0xbffff6a8
esi 0x40015360 1073828704
edi 0x80483f0 134513648
eip 0x8048366 0x8048366
eflags 0x386 902
cs 0x23 35
ss 0x2b 43
ds 0x2b 43
es 0x2b 43
fs 0x0 0
gs 0x33 51

当然也可以显示任意一个指定的寄存器值:

1
2
(gdb) i r eax
eax 0xbffff6a4 -1073744220

最后一个要介绍的命令是“q”,退出(Quit)GDB调试环境:

1
2
(gdb) q
The program is running. exit anyway? (y or n)

补充内容

gdb 教程:慕课网-Linux C语言指针与内存-第三章

如果删除源代码, 就无法显示行号等辅助信息了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
gcc -g gdb.c -o gdb.out # -g 支持gdb调试; -o 输出, 默认为 a.out

gdb gdb.out # 进入 gdb 调试环境
enter # 继续执行上条命令
l # 列出源代码, 默认 10 行, 按 l 继续

start # 开始单步调试, 默认 main() 第一行
p a # 查看 a 变量的值
n # 继续到下一行
s # 进入子函数
bt # 查看函数栈
f 1 # 切换函数栈

q 退出调试
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
# 测试用代码
#include <stdio.h>

void change(int a, int b){
int tmp=a;
a=b; b=tmp;
}

void change2(int *a, int *b){
int tmp=*a;
*a=*b; *b=tmp;
}

int main(){
int a=5,b=3;
change(a,b);
printf("change:\na=%d\nb=%d\n", a,b);
change2(&a,&b);
printf("change2:\na=%d\nb=%d\n", a,b);
}