日常开发需要掌握的Git命令

本人待的两家公司,一直都是用的SVN,Git我只是自己私下学习和开发小项目的时候用过,工作一直没有使用过,但还是要学的。。。Git是最好的分布式版本控制系统。

工作流程

SVN和Git的区别

  SVN是集中式版本控制系统,版本库是集中放在中央服务器的,在工作的时候你首先要从中央服务器哪里得到最新的版本,然后在写完代码之后,把这些内容提交到服务器。

前提必须联网才能工作,一般都是公司自己的局域网。

  Git是分布式版本控制系统,没有中央服务器的,每个人的电脑就是一个完整的版本库,工作的时候就不需要联网了,因为版本都是在自己的电脑上。

多人如何协同工作:

  比如说自己在电脑上改了文件A,其他人也在电脑上改了文件A,这时,你们两之间只需把各自的修改推送给对方,就可以互相看到对方的修改了。

工作区和暂存区

工作区:

  就是你在电脑上看到的目录,比如目录下testgit里的文件(.git隐藏目录版本库除外)。或者以后需要再新建的目录文件等等都属于工作区范畴。

版本库(Repository):

  工作区有一个隐藏目录.git,这个不属于工作区,这是版本库。其中版本库里面存了很多东西,其中最重要的就是stage(暂存区),还有Git

为我们自动创建了第一个分支master,以及指向master的一个指针HEAD。

git add就是把文件添加到stage,然后通过git commit将暂存区的所有内容提交到当前分支

分支操作

git branch 创建分支

git branch -b 创建并切换到新建的分支上

git checkout 切换分支

git branch 查看分支列表

git branch -v 查看所有分支的最后一次操作

git branch -vv 查看当前分支

git brabch -b 分支名 origin/分支名 创建远程分支到本地

git branch --merged 查看别的分支和当前分支合并过的分支

git branch --no-merged 查看未与当前分支合并的分支

git branch -d 分支名 删除本地分支

git branch -D 分支名 强行删除分支

git branch origin :分支名 删除远处仓库分支

git merge 分支名 合并分支到当前分支上

  通常合并分支时,git一般使用”Fast forward”模式,在这种模式下,删除分支后,会丢掉分支信息,现在我们来使用带参数 –no-ff来

禁用”Fast forward”模式。

暂存操作

git stash 暂存当前修改

git stash apply 恢复最近的一次暂存

git stash pop 恢复暂存并删除暂存记录

git stash list 查看暂存列表

git stash drop 暂存名(例:stash@{0}) 移除某次暂存

git stash clear 清除暂存

回退操作

git reset --hard HEAD^ 回退到上一个版本

git reset --hard ahdhs1(commit_id) 回退到某个版本

git checkout -- file撤销修改的文件(如果文件加入到了暂存区,则回退到暂存区的,如果文件加入到了版本库,则还原至加入版本库之后的状态)

git reset HEAD file 撤回暂存区的文件修改到工作区

标签操作

git tag 标签名 添加标签(默认对当前版本)

git tag 标签名 commit_id 对某一提交记录打标签

git tag -a 标签名 -m '描述' 创建新标签并增加备注

git tag 列出所有标签列表

git show 标签名 查看标签信息

git tag -d 标签名 删除本地标签

git push origin 标签名 推送标签到远程仓库

git push origin --tags 推送所有标签到远程仓库

git push origin :refs/tags/标签名 从远程仓库中删除标签

常规操作

git push origin test 推送本地分支到远程仓库

git rm -r --cached 文件/文件夹名字 取消文件被版本控制

git reflog 获取执行过的命令

git log --graph 查看分支合并图

git merge --no-ff -m '合并描述' 分支名 不使用Fast forward方式合并,采用这种方式合并可以看到合并记录

git check-ignore -v 文件名 查看忽略规则

git add -f 文件名 强制将文件提交

git clone http://username:password@链接,例如http://zhangsan:123456@gitee.com/abc/def.git clone远程仓库到本地

git创建项目仓库

git init 初始化

git remote add origin url 关联远程仓库

git pull

git fetch 获取远程仓库中所有的分支到本地

忽略已加入到版本库中的文件

git update-index --assume-unchanged file 忽略单个文件

git rm -r --cached 文件/文件夹名字 (. 忽略全部文件)

取消忽略文件

git update-index --no-assume-unchanged file

拉取、上传免密码

git config --global credential.helper stor

Git使用实践:

1、首先按照Git

2、创建一个文件夹,用来创建Git的版本库

3、进入文件夹,通过下面的命令创建本地仓库,从而实现对版本库的管理

git init

  发现创建了一个.git文件夹,不要随便改动里面的文件

4、创建文件a.txt之后

5、通过git status查看git状态

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        a.txt

nothing added to commit but untracked files present (use "git add" to track)

6、添加文件到本地仓库

$ git add a.txt

  如果成功了,就没有任何提示。俗话说:没有消息,就是最好的消息。。。

此时查看状态:

$ git status
On branch master
Your branch is up to date with 'origin/master'.

Changes to be committed:
  (use "git reset HEAD <file>..." to unstage)

        new file:   a.txt

Git发现一个新文件a.txt

7、提交文件

$ git commit -m "create new file a.txt"
[master a96f8bb] create new file a.txt
 1 file changed, 1 insertion(+)
 create mode 100644 a.txt

  -m为此次提交说明,提交到master分支,一个文件修改,插入一行数据,创建模式

git commit只是提交暂存区的修改,也就是只有先git add之后,才能commit成功

$ git commit -m "xiajibagaide"
On branch master
Your branch is ahead of 'origin/master' by 3 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
        modified:   a.txt

no changes added to commit

修改了a.txt,但是提交发现没有任何改变

  PS:可以add多个文件,然后统一提交。

8、git diff

  此时我们修改a.txt,通过git status只能查看到文件被修改,如果想要查看修改什么内容,可以通过git diff a.txt查看

$ git diff a.txt
diff --git a/a.txt b/a.txt
index 2fd702e..db7ec96 100644
--- a/a.txt
+++ b/a.txt
@@ -1 +1,2 @@
-git test
\ No newline at end of file
+git test
+second line
\ No newline at end of file

9、git提交日志

$ git log
commit 6bab7bd9dfef4504b5a51e6c5b53c53272741ce0 (HEAD -> master)
Author: liguohui <17012345815@163.com>
Date:   Fri Jul 19 14:27:47 2019 +0800

    add three line

commit 93ed7195a312e6d65ed50399bfed2569fd28feb7
Author: liguohui <17012345815@163.com>
Date:   Fri Jul 19 14:22:30 2019 +0800

    modify a.txt

commit a96f8bbabb60185d8d2f12a7fd5c36c3d6f1c695
Author: liguohui <17012345815@163.com>
Date:   Fri Jul 19 14:09:31 2019 +0800

    create new file a.txt

查看到提交三次,包括每次提交生成的加密ID,时间,提交说明

也可以将每次提交日志显示在一行,进行格式化美化:

$ git log --pretty=oneline
6bab7bd9dfef4504b5a51e6c5b53c53272741ce0 (HEAD -> master) add three line
93ed7195a312e6d65ed50399bfed2569fd28feb7 modify a.txt
a96f8bbabb60185d8d2f12a7fd5c36c3d6f1c695 create new file a.txt

10、版本回退

如果我们想要回退版本,可以使用git reset命令,Git用HEAD表示当前版本,HEAD^表示上一个版本,如果是前100个版本呢,可以使用HEAD~100

回到上个版本:

$ git reset --hard HEAD^
HEAD is now at 93ed719 modify a.txt

回到任意一个版本,需要知道提交的ID

$ git reset --hard 6bab7bd9dfef4504b5a51e6c5b53c53272741ce0
HEAD is now at 6bab7bd add three line

11、Git所有操作日志:

与git log不同的是,git reflog可以显示对git的所有操作日志

$ git reflog
6bab7bd (HEAD -> master) HEAD@{0}: reset: moving to 6bab7bd9dfef4504b5a51e6c5b53c53272741ce0
93ed719 HEAD@{1}: reset: moving to HEAD^
6bab7bd (HEAD -> master) HEAD@{2}: commit: add three line
93ed719 HEAD@{3}: commit: modify a.txt
a96f8bb HEAD@{4}: commit: create new file a.txt
5e9a9b2 (origin/master) HEAD@{5}: commit (initial): Red Black Tree Commit

我们可以看到不仅仅有commit,还有reset的操作等

12、撤销修改/删除

git checkout命令把工作区的修改撤销,这里分为两种情况:

1).a.txt修改过后还没add到暂存区,撤销之后和版本库保持一致。

2).a.txt修改后已经add到暂存区了,撤销之后就是加入暂存区的状态。

这种情况下的解决方案:注意--和a.txt之间有空格

$ git reset HEAD a.txt    #首先回到当前版本,就可以把暂存区的修改撤销,然后就属于第一种情况了
Unstaged changes after reset:
M       a.txt

$ git checkout -- a.txt    #撤销工作区的修改

PS:git checkout就是用版本库里的版本替换工作区的版本,工作区删除也是可以还原

13、删除文件

工作区删除了a.txt,通过git status可以查看到

如果想要删除文件:

$ git rm a.txt
rm 'a.txt'
$ git commit -m "delete a.txt" [master a708588] delete a.txt 1 file changed, 3 deletions(-) delete mode 100644 a.txt

如果是误删:

$ git checkout -- a.tx

14、远程仓库:

个人使用远程仓库可以选择GitHub、Gitee,本人公司使用Gitlab

这里忽略GitHub上一些相关配置,绑定用户名密码等操作,因为我本身之前已经建立远程仓库了。

与远程仓库关联:远程仓库名称默认origin

git remote add origin https://github.com/huigelaile1234/Huige.git

本地库的内容推送到远程:

$ git push -u origin master
Enumerating objects: 17, done.
Counting objects: 100% (17/17), done.
Delta compression using up to 8 threads.
Compressing objects: 100% (11/11), done.
Writing objects: 100% (16/16), 1.41 KiB | 240.00 KiB/s, done.
Total 16 (delta 0), reused 0 (delta 0)
To https://github.com/huigelaile1234/Huige.git
   5e9a9b2..6e8c255  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.

PS:

  加上了 –u参数,Git不但会把本地的master分支内容推送的远程新的master分支,还会把本地的master分支和远程的master分支关联起来,

在以后的推送或者拉取时就可以简化命令。

  后面只要本地作了提交,就可以通过如下命令把本地master分支的最新修改推送到github上了,现在你就拥有了真正的分布式版本库了。

git push origin master

远程仓库克隆:

$ git clone https://github.com/huigelaile1234/Huige.git
Cloning into 'Huige'...
remote: Enumerating objects: 31, done.
remote: Counting objects: 100% (31/31), done.
remote: Compressing objects: 100% (21/21), done.
remote: Total 31 (delta 5), reused 30 (delta 4), pack-reused 0
Unpacking objects: 100% (31/31), done.

创建分支:

$ git checkout -b dev                #创建一个新的分支,-b相当于下面两个命令
Switched to a new branch 'dev'
D       a.txt

$ git branch dev
$ git checkout dev
Switched to branch 'dev'

$ git branch    #查看分支,*表示当前分支为dev
* dev
  master

  我们在dev分支的操作,对于master是不可见的,例如我们修改了miaosha.txt,切换到master后,发现修改的内容没了,所以需要合并分支,

然后删除分支

$ git merge dev            #将dev分支的内容合并到当前分支
Updating d806f1c..f2c990e
Fast-forward
 miaosha.txt | 1 +
 1 file changed, 1 insertion(+)

$ git branch -d dev        #删除分支
Deleted branch dev (was f2c990e).

解决冲突:

我们在master分支对a.txt的第二行,添加一个词:master,在dev分支对a.txt的第二行,添加一个词:dev,分别进行add和commit

$ git merge dev        #发生冲突
Auto-merging a.txt
CONFLICT (content): Merge conflict in a.txt
Automatic merge failed; fix conflicts and then commit the result.

查看冲突:both modified:a.txt

$ git status
On branch master
Your branch is ahead of 'origin/master' by 6 commits.
  (use "git push" to publish your local commits)

You have unmerged paths.
  (fix conflicts and run "git commit")
  (use "git merge --abort" to abort the merge)

Unmerged paths:
  (use "git add <file>..." to mark resolution)

        both modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")

git log查看分支的合并情况:

$ git log --graph --pretty=oneline --abbrev-commit
*   bf37303 (HEAD -> master) resolve conflict
|\
| * 21e732b (dev) dev commit
* | a3d4dc7 master commit
|/
* b2c0a33 dev commit
* f2c990e aaa
*   bf37303 (HEAD -> master) resolve conflict
|\
| * 21e732b (dev) dev commit
* | a3d4dc7 master commit
|/
* b2c0a33 dev commit
* f2c990e aaa
* d806f1c a
* 3b0963a commit
* 82f5ce0 create new file miaosha.txt

需要先解决冲突,然后在进行提交,最后删除分支。

禁用Fast Forward模式:

  合并分支时,--no-ff参数就可以用普通模式合并,合并后的历史有分支,能看出来曾经做过合并,而fast forward合并就看不出来曾经做过

合并。

Bug分支:

  现在在master分支开发一个需求,突然来了一个bug,只能创建一个bug分支,去解决这个bug,你需要先把master的代码提交才能切换到bug

分支。但是你还没有开发完毕,不想提交,这时候就需要git stash把这部分代码藏起来。

$ git status    #修改文件,查看状态
On branch master
Your branch is ahead of 'origin/master' by 12 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   a.txt
        modified:   b.txt

no changes added to commit (use "git add" and/or "git commit -a")

$ git stash    #把修改隐藏
Saved working directory and index state WIP on master: 9109e8d meger bug fix 001

$ git status    #重新查看,发现没有update了
On branch master
Your branch is ahead of 'origin/master' by 12 commits.
  (use "git push" to publish your local commits)

nothing to commit, working tree clean
$ git checkout -b issue-001            #在需要分支上创建bug分支,并切换到bug分支
Switched to a new branch 'issue-001'

$ git add b.txt

$ git commit -m "fix bug"        #解决bug
[issue-001 d23090d] fix bug
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

$ git checkout master        #切换到master分支
Switched to branch 'master'
Your branch is ahead of 'origin/master' by 10 commits.
  (use "git push" to publish your local commits)

$ git merge --no-ff -m "meger bug fix 001" issue-001    #合并代码
Merge made by the 'recursive' strategy.
 b.txt | 1 +
 1 file changed, 1 insertion(+)
 create mode 100644 b.txt

$ git stash list    #查看stash list
stash@{0}: WIP on master: df1b58c a

$ git stash pop    #恢复stash之前状态
On branch master
Your branch is ahead of 'origin/master' by 12 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")
Dropped refs/stash@{0} (b15629eb0747dc4e2cc1e9ff44e685f20d07d91b)

$ git status    #此时状态已经恢复
On branch master
Your branch is ahead of 'origin/master' by 12 commits.
  (use "git push" to publish your local commits)

Changes not staged for commit:
  (use "git add <file>..." to update what will be committed)
  (use "git checkout -- <file>..." to discard changes in working directory)

        modified:   a.txt

no changes added to commit (use "git add" and/or "git commit -a")

stash恢复的方式有两种:

1、git stash apply:但是恢复后,stash内容并不删除,你需要用git stash drop来删除

2、git stash pop:恢复的同时把stash内容也删了

Feature分支:

  如果你创建一个分支,开发完成之后,切换回原来的分支,还没来得及合并,需要强制删除和其修改的代码,可以强行删除

$ git branch -D feature-vulcan
Deleted branch feature-vulcan (was 287773e).

多人协作:

1

 

内容参考:https://www.cxiansheng.cn/daily/490

https://www.liaoxuefeng.com/wiki/896043488029600

posted @ 2019-05-23 17:50  Diamond-Shine  阅读(606)  评论(0编辑  收藏  举报