git常用命令总结

1.克隆一个仓库

$ git clone 仓库地址

2.把一个文件夹变成git可以管理的仓库

$ git init

3.把文件添加到仓库(在执行commit之前)

$ git add <file>     //把文件file添加到仓库

4.把文件提交到仓库 (在执行add后)

$ git commit -m <message>     //message为本次提交的说明

5.查看当前工作区的状态

$ git status

6.查看具体修改了什么内容

$ git diff <filename>     //filename为要查看的具体文件

7.查看之前提交的历史记录

$ git log
or
$ git log --pretty=oneline

8.版本回退

$ git reset --hard commit_id    //commit_id为要回退到指定提交时的id

9.查看历史命令,用来确定要回到哪个版本

$ git reflog    //向后回退
$ git log //向前回退

10.想要丢弃工作区的修改(注意有"--",没有这个则是切换分支命令)

$ git checkout -- file    //想直接丢弃工作区的修改
or
$ git reset HEAD <file>     //当你修改了某个文件比将其添加到了暂存区像将其丢弃则分两步首先用 git reset HEAD <file>然后再用 git checkout -- file

  

11.删除一个文件

$ git rm <file>
$ git commit -m "删除说明"

12.将本地仓库与远程仓库关联(在已经配置号SSH key的前提下)

$ git remote add origin 你的仓库地址

13.将本地仓库的所有内容推送到远程仓库(如果远程仓库是空的或者第一次推送则要加“-u”,之后就可不用使用)

$ git push p -u origin master

14. 创建分支命令的使用

$ git checkout -b newbranch   //创建分支newbranch并切换到newbranch分支

上面的命令相当于两条命令

$ git branch newbranch
$ git checkout newbranch

  

15.查看当前分支(当前分支前面会有一个 * 号)

$ git branch

16.合并分支的使用

$ git merge <name>

17.删除分支命令

$ git branch -d <name>

18.查看分支合并情况

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

19. 保存当前工作现场,等以后恢复现场

$ git stash    //保存现场
$ git stash list //查看保护现场信息

20.恢复现场

$ git stash pop     //恢复现场的同时删除stash内容
or
$ git stash apply     //恢复现场但不删除stash内容

21.强行销毁分支(在没有合并之前适用)

$ git branch -D <name>

22.查看远程仓库信息

$ git remote 
or
$ git remote -v    //查看详细信息

23.将本地push的分叉整理成直线(便于查看)

$ git rebase

24.添加标签

$ git tag <name>
or
$ git tag <name> commit_id   //给指定提交添加标签
$ git tag    //查看标签

25.查看标签信息

$ git show <tagname>

26.创建带有说明的标签

$ git tag -a <tag> -m "标签说明"

27.删除标签

$ git tag -d <tagname>

28.推送标签到远程

$ git push origin <tagname>   //推送一个标签

$ git push origin --tags      //推送全部未推送过的本地标签

  

在GitHub上我们可以任意的Fork别人的开源仓库;Fork别人的仓库之后我们就拥有了读写权限,之后我们就可以通过推送pull request来与原仓库的主人一起参与项目的开发与贡献。

仓库之间的关系图(以bootstrap仓库为例):

 参考连接:

https://www.liaoxuefeng.com/wiki/0013739516305929606dd18361248578c67b8067c8c017b000

原文地址:https://www.cnblogs.com/yuanchao-blog/p/10555729.html