您的位置:首页 > 其它

Git 常用命令汇总

2016-06-29 17:19 441 查看
引言:

关于Git的教程网络上大神们都已经写的非常详细完备了,再加上Git非常友好的提示说明与用户文档,原理与操作我就不在这里做过多冗余的介绍了,主要将一些常用的命令总结出来,方便自己查看回顾。

相比于CVS和SVN这些集中式的版本控制系统不但速度慢,而且必须联网才能使用。Git不单是不必联网这么简单,Git极其强大的分支管理,分布式版本控制。让Git成为目前世界上最火爆的版本控制系统。

安装初始化:

Linux安装git,红帽系列yum install git/乌班图apt-get install git

git安装好后还需要进行设置:设置你的用户名和邮箱

#git config --global user.name "Your Name"
#git config --global user.email "email@example.com"


Linux系统下直接输入git若出现以下信息,表示已经安装成功。

usage: git [--version] [--exec-path[=GIT_EXEC_PATH]] [--html-path]
[-p|--paginate|--no-pager] [--no-replace-objects]
[--bare] [--git-dir=GIT_DIR] [--work-tree=GIT_WORK_TREE]
[--help] COMMAND [ARGS]

The most commonly used git commands are:
add        Add file contents to the index
bisect     Find by binary search the change that introduced a bug
branch     List, create, or delete branches
checkout   Checkout a branch or paths to the working tree
clone      Clone a repository into a new directory
commit     Record changes to the repository
diff       Show changes between commits, commit and working tree, etc
fetch      Download objects and refs from another repository
grep       Print lines matching a pattern
init       Create an empty git repository or reinitialize an existing one
log        Show commit logs
merge      Join two or more development histories together
mv         Move or rename a file, a directory, or a symlink
pull       Fetch from and merge with another repository or a local branch
push       Update remote refs along with associated objects
rebase     Forward-port local commits to the updated upstream head
reset      Reset current HEAD to the specified state
rm         Remove files from the working tree and from the index
show       Show various types of objects
status     Show the working tree status
tag        Create, list, delete or verify a tag object signed with GPG

See 'git help COMMAND' for more information on a specific command.


接下来你就可以创建git仓库安静的使用git了,创建完git仓库别忘了初始化git

#git init


出现以下提示说明git初始化完成。

Initialized empty Git repository in /Test/learngit/.git/


初始化完成后使用

#ls -a


可以发现目录下多了一个.git文件夹,这个目录下的文件是用来管理版本库的。(但是我们的git仓库目录仍然是/Test/learngit/)

现在我们就可以将自己需要进行版本控制的文件放到git仓库中了。

常用命令详解:

从上我们可以看到Git的大部分常用命令,下来我们就对这些命令进行解释说明:

【添加和提交】

例如我们现在有一个first.txt文件,

我们将其放到/Test/learngit/下的子目录test中,

用命令git add告诉Git,把文件添加到仓库:

#git add first.txt


add成功后没有提示。

用命令git commit告诉Git,把文件提交到仓库:

#git commit -m "this is a test file"


-m 后面输入的是本次提交的说明。也可以不加-m 直接提交,出现警告后wq保存退出。

出现以下提示说明提交成功。

[master (root-commit) a1767f9] this is a test file
1 files changed, 1 insertions(+), 0 deletions(-)
create mode 100644 test/firstone.txt


【注意】

我们在git仓库文件夹下建立文件实际就是将文件放到了工作区。

第一步是用git add把文件添加进去,实际上就是把文件修改添加到暂存区;

第二步是用git commit提交更改,实际上就是把暂存区的所有内容提交到当前分支(也即版本库)。

【显示Git仓库当前状态】 

#git status     命令可以显示仓库当前的状态。
# On branch master
# Changes to be committed:
#   (use "git reset HEAD <file>..." to unstage)
#
#	modified:   firstone.txt
#
# Changed but not updated:
#   (use "git add <file>..." to update what will be committed)
#   (use "git checkout -- <file>..." to discard changes in working directory)
#
#	modified:   firstone.txt
#


【查看当前改动】

#git diff first.txt  可以查看当前已经改动但还未提交的内容与先前的差异。
diff --git a/test/firstone.txt b/test/firstone.txt
index b7ee639..0497bd7 100644
--- a/test/firstone.txt
+++ b/test/firstone.txt
@@ -1,2 +1,3 @@
This is a test file.
Hello world!
+Modify


【显示提交日志】

#git log   显示提交日志

commit 610471ea2cbcec59860547a5b7d1899504f45315    (版本号)
Author: tx_mazi <2298929097@qq.com>
Date:   Tue Jun 21 09:52:07 2016 +0800

add modify

commit a1767f9df286f997ce727ecbcaa04731e01fa72e
Author: tx_mazi <2298929097@qq.com>
Date:   Tue Jun 21 09:34:22 2016 +0800

this is a test file


【版本回退】

[撤销工作区修改]

#git checkout -- file


把文件在工作区的修改全部撤销,让这个文件回到最近一次git commit或git add时的状态。

如果暂存区有该文件的上个版本(修改前git add但没有git commit)就把工作区文件恢复至和暂存区版本一样,如果暂存区没有该文件的上个版本就把工作区的文件恢复至和版本库中一样。

[撤销暂存区修改]

在git commit之前我们还可以通过git reset命令来回退版本

#git reset HEAD file


可以把暂存区的修改撤销掉(unstage),重新放回工作区。

git reset命令:版本回退 可以直接用HEAD^ 也可以后跟commit_id版本号

#git reset --hard HEAD^


Git必须知道当前版本是哪个版本,在Git中,用HEAD表示当前版本,上一个版本就是HEAD^,上上一个版本就是HEAD^^,当然往上100个版本写100个^比较容易数不
过来,所以写成HEAD~100。

#git reset --hard 610471ea


git reflog用来记录你的每一次git命令。用它你可以找回之前的版本号。

【注意】
git checkout — file撤销工作区的修改
git reset HEAD file命令 撤销暂存区的修改

【删除文件】

#git rm file


用于删除一个文件(或文件夹 -r)[注意:手动rm后一定要再git rm一下,然后再git commit然后再git push才会删除远程仓库中的文件]

【关联远程仓库】
生成本机的公钥和私钥:

# ssh-keygen -t rsa -C "youremail@example.com"


将公钥添加到github上

[在本地关联远程库]
现在github上创建一个Git仓库。在本地创建同名本地库用于和远程仓库关联。
要关联一个远程库,使用命令

#git remote add origin git@SERVER-NAME:PATH/REPO-NAME.git;


#git remote add origin git@github.com:mazilaile/tree.git

#git push -u origin master


由于远程库是空的,我们第一次推送master分支时,加上了-u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,在以后的推送或者拉取时就可以简化命令。

把本地库的内容推送到远程,用git push命令,实际上是把当前分支master推送到远程。

#git push origin master


[在本地对远程库进行克隆]
git clone命令克隆远程仓库的内容到本机

#git clone git@github.com:YourGit/YourRepo.git


【分支】
创建、切换和删除分支

[创建并切换分支]

#git checkout -b tst


git checkout命令加上-b参数表示创建并切换,相当于以下两条命令:
#git branch tst   创建分支
* master
tst


#git checkout tst   切换分支
Switched to branch 'tst'


[查看都有哪些分支]

#git branch 查看都有哪些分支,带星号的是当前分支
master
* tst


[合并分支]

#git merge tst 合并分支到主分支

#git merge --no-ff -m "merge with no-ff" tst


合并分支到主分支但是禁用Fast forward,删除分支后,仍然保留分支信息。合并分支时,加上–no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast
forward合并就看不出来曾经做过合并。

成功

Updating 9d85dda..23b93d4
Fast-forward
README.md |    1 +
1 files changed, 1 insertions(+), 0 deletions(-)


有冲突失败

Auto-merging README.md
CONFLICT (content): Merge conflict in README.md
Automatic merge failed; fix conflicts and then commit the result.


解决冲突:

# git status 查看有冲突的文件
# cat README.md 用cat命令查看该文件


# ThinkGame
Fantastic training program  
start
<<<<<<< HEAD
branch master
=======
branch tst
>>>>>>> tst

手动修改文件再add、commit后合并分支

[删除分支]

#git branch -d tst   删除分支
#git branch -D tst   删除一个没有合并过的分支


[查看分支合并情况]
用带参数的git log查看分支的合并情况

#git log --graph --pretty=oneline --abbrev-commit


–pretty  按指定格式显示日志信息,可选项有:oneline,short,medium,full,fuller,email,raw以及format:<string>,默认为medium,可以通过修改配置文件来指定默认的方式。
–abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。

[当前工作区的保存(不提交)与恢复]

#git stash   不提交,将当前工作区中的内容保存起来,等完成紧要分支任务后再恢复
#git stash list    查看刚才的工作区中的内容存到哪去


#git stash pop  将刚刚工作区中的内容恢复,并将stash内容删除

#git remote -v查看远程库的信息 -v显示详细内容
origin	git@github.com:mazilaile/ThinkGame.git (fetch)
origin	git@github.com:mazilaile/ThinkGame.git (push)
(远程仓库的默认名称是origin)


[向远程库推送分支]
推送分支,本地可以建立许多分支,但是并不是所有的分支都需要推送到远程库中

#git push origin Branch-Name


如果向远程推送分支失败,查看错误信息,如果是和其他人的推送有冲突,则
需要先用git pull将远程分支从origin/Branch-Name抓下来试图合并;

如果git pull提示“no tracking information”,则说明本地分支和远程分支的链接关系没有创建,

#git branch --set-upstream branch-name origin/Branch-Name


如果合并有冲突,则解决冲突,然后再在本地提交

在本地创建和远程分支对应的分支,使用

#git checkout -b branch-name origin/branch-name


本地和远程分支的名称最好一致;

【Git标签】
Git的标签虽然是版本库的快照,但其实它就是指向某个commit的指
针(跟分支很像对不对?但是分支可以移动,标签不能
移动),所以,创建和删除标签都是瞬间完成的。

[建立标签]

#git tag <name>  在当前分支上打一个新标签


查看历史提交的commit id

#git log --pretty=oneline --abbrev-commit


–pretty  按指定格式显示日志信息,可选项有:oneline,short,medium,full,fuller,email,raw以及format:<string>,默认为medium,可以通过修改配置文件来指定默认的
方式。
–abbrev-commit 仅显示 SHA-1 的前几个字符,而非所有的 40 个字符。

[给指定提交打标签]

#git tag Commit_Id     给指定的提交打标签


[查看标签]

#git tag  查看所有标签


#git show <tagname> 查看指定标签信息


还可以创建带有说明的标签,用-a指定标签名,-m指定说明文字:

#git tag -a v0.1 -m "version 0.1 released" 3628164


[删除标签]

#git tag -d v0.1 删除标签


如果标签已经推送到远程
先在本地删除,再在远程删除

#git push origin :refs/tags/<tagname>可以删除一个远程标签


[推送标签到远程]
如果要推送某个标签到远程,使用命令

#git push origin <tagname>


【忽略文件向远程推送】
在Git工作区的根目录下创建一个特殊的.gitignore文件,然后把要忽略的文件名填进去,Git就会自动忽略这些文件。不需要从头写.gitignore文件,GitHub已经为我们准备了各种配置文件,只需要组合一下就可以使用了。所有配置文件可以直接在线浏览:https://github.com/github/gitignore

#git check-ignore命令检查.gitignore中哪个规则写错了

更多内容请移步:麻子来了 |博客 技术分享
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息