git常用命令
git config
$ git config --global user.name "wrd"
$ git config --global user.email "12134@gmail.com"
执行了上面的命令后,会在你的主目录(home directory)建立一个叫 ~/.gitconfig 的文件. 内容一般像下面这样:
[user]
name = wrd
email = 12134@gmail.com
这样的设置是全局设置,会影响此用户建立的每个项目.
不带 --global 选项
会使当前项目里的某个值与前面的全局设置有区别
会在你项目目录下的 .git/config 文件中增加一节[user]内容(如上所示).
git clone
Git能在许多协议下使用,所以Git URL可能以ssh://, http(s): //, git://,或是只是以一个用户名(git 会认为这是一个ssh 地址)为前辍,
执行:
git clone http://www.kernel.org/pub/scm/git/git.git
我们就可以得到一个项目的拷贝(copy),在默认情况下,Git会把"Git URL"里目录名的’.git’的后辍去掉,做为新克隆(clone)项目的目录名: (例如. git clone http://git.kernel.org/linux/kernel/git/torvalds/linux-2.6.git 会建立一个目录叫’linux-2.6’)
git init
在某个目录中执行
git init
在该目录中会有一个名叫”.git” 的目录被创建,这意味着一个仓库被初始化了。
git add
修改文件,将它们更新的内容添加到索引中.
git add file1
Git跟踪的是内容不是文件
很多版本控制系统都提供了一个 “add” 命令:告诉系统开始去跟踪某一个文件的改动。
但是Git里的 ”add” 命令从某种程度上讲更为简单和强大.
git add 不但是用来添加不在版本控制中的新文件,也用于添加已在版本控制中但是刚修改过的文件;
在这两种情况下, Git都会获得当前文件的快照并且把内容暂存(stage)到索引中,为下一次commit做好准备。
git status
git status命令来获得当前项目的一个状况:
$ git status
# On branch master
# Changes to be committed:
# (use "git reset HEAD <file>..." to unstage) #
# modified: file1
# modified: file2
# modified: file3
git commit
记录一个新的项目版本
git commit -m [message]
提交暂存区到本地仓库中,[message] 可以是一些备注信息
git commit -a
会自动把所有内容被修改的文件(不包括新创建的文件)都添加到索引中,并同时把它们提交。会弹出vim编辑器的界面,有提交信息,提示,提交时间,修改的文件
当然也可以 -am连写
git commit --amend
有两个作用:
- 追加提交,它可以在不增加一个新的commit记录的情况下将新修改的代码追加到前一次的commit中但是请注意commit-id会改变
- 覆盖上次提交的信息,也会生成一个新的commit-id
git commit --help
查看帮助,还有许多参数有其他效果,
commit注释最好以一行短句子作为开头,来简要描述一下这次 commit所作的修改(最好不要超过50个字符);然后空一行再把详细的注释写清楚。这样就可以很方便的用工具把 commit注释变成email通知,第一行作为标题,剩下的部分就作email的正文.
git log
显示所有的提交(commit)
git log 有很多命令参数可以控制输出的内容和格式
git log [ < options>] [ < revision range>] [[ --] < path >…]
- -decorate
打印显示的所有提交的引用名称
git log --decorate[=short|full|no]
默认为short
commit 7919e7674a467f0e5b1d13e99b26af5e6e1b212b (HEAD -> master, origin/master, origin/HEAD)\
full
commit 7919e7674a467f0e5b1d13e99b26af5e6e1b212b (HEAD -> refs/heads/master, refs/remotes/origin/master, refs/remotes/origin/HEAD)
no 等价于–no-decorate
commit 7919e7674a467f0e5b1d13e99b26af5e6e1b212b
- - log-size
在每个提交的输出中包括一行“日志大小<number>
”,其中<number>
是该提交消息的长度(以字节为单位)。旨在git log 通过允许工具提前分配空间来加快从输出中读取日志消息的工具。
commit cd30dbbfed2fdf2b5c6b4a4368a6d3933abc458b
log size 204
Author: wrd <@.com>
Date: Wed Feb 3 15:58:37 2021 +0800
-L
-L:<funcname>:<file>
Trace the evolution of the line range given by <start>,<end>,
or by the function name regex <funcname>, within the <file>.
You may not give any pathspec limiters.
This is currently limited to a walk starting from a single revision,
i.e., you may only give zero or one positive revision arguments,
and <start> and <end> (or <funcname>) must exist in the starting revision.
You can specify this option more than once. Implies --patch.
Patch output can be suppressed using --no-patch,
but other diff formats (namely --raw, --numstat, --shortstat, --dirstat,
--summary, --name-only, --name-status, --check) are not currently implemented.
<start> and <end> can take one of these forms:
number
If <start> or <end> is a number, it specifies an absolute line number
(lines count from 1).
/regex/
This form will use the first line matching the given POSIX regex.
If <start> is a regex, it will search from the end of the previous -L range,
if any, otherwise from the start of file. If <start> is ^/regex/,
it will search from the start of file. If <end> is a regex,
it will search starting at the line given by <start>.
+offset or -offset
This is only valid for <end> and will specify a number of
lines before or after the line given by <start>.
If :<funcname> is given in place of <start> and <end>,
it is a regular expression that denotes the range from
the first funcname line that matches <funcname>,
up to the next funcname line. :<funcname> searches
from the end of the previous -L range, if any,
otherwise from the start of file. ^:<funcname> searches
from the start of file. The function names are determined
in the same way as git diff works out patch hunk headers
(see Defining a custom hunk-header in gitattributes(5)).
分支
一个Git仓库可以维护很多开发分支。
git branch
执行git branch
会得到当前仓库中存在的所有分支列表
test master 分支是Git系统默认创建的主分支
* master 星号(*) 标识了你当工作在哪个分支下
git branch
:查看本地分支
git branch <branchName>
:在本地创建一个名字为branchName的分支。
git branch -d <branchName>
:删除一个名字为branchName的分支。如果该分支有提交未进行合并,则会删除失败。
git branch -D <branchName>
:强制删除一个名字为branchName 的分支。如果该分支有提交未进行合并,也会删除成功。
git checkout
git checkout <branchName>
:切换到名字为branchName的本地分支。
git checkout -b <branchName>
:创建一个名字为branchName的分支并且切换到改分支。
git checkout -B <branchName>
:创建并重置一个名字为branchName的分支并且切换到改分支。
git checkout -- filepathname
放弃未使用 git add 缓存时的单个文件修改,注意不要忘记中间的"–",不写就成了检出分支了!
git checkout
放弃放弃掉所有还没有加入到缓存区的文件修改,不会删除掉刚新建的文件。因为刚新建的文件还没已有加入到 git 的管理系统中。
git checkout -f
相当于git checkout --force
强制检出(丢弃本地修改)
git merge
将名字为branchName的分支合并到当前分支 有两种模式
git merge <branchName>
:fast forward模式,快速合并,看不出做过合并。 不会显示 feature,只保留单条分支记录
git merge <branchName> –no-ff
: --no-ff模式,普通合并,可以保存之前的分支历史。能够更好的查看 merge历史,以及branch 状态。会生成一个新的commit-id

通常,合并分支时,Git尽可能用 fast forward 模式,但这种模式,删除分支后,会丢掉分支信息。
git reset
git reset HEAD filepathname
放弃缓存区中某个文件的更改 本地的修改并不会消失,而是取消暂存。
git reset HEAD .
放弃所有的缓存区的更改 本地的修改并不会消失,而是取消暂存。
git reset --hard HEAD^
回退到上一次commit的状态。本地的修改丢失
git reset --hard commit-id
可以用来回退到任意版本。本地的修改丢失