Git指令大全[通俗易懂]

Git指令大全[通俗易懂]转自:https://mp.weixin.qq.com/s/sF-cx2ss4haO74K55Sjf9g配置首先是配置帐号信息ssh-Tgit@github.com#登陆github修改项目中的个人信息gitconfig–globaluser.name“githelper”gitconfig–globaluser.emailgithelper@gmail.c

大家好,又见面了,我是你们的朋友全栈君。

转自:https://mp.weixin.qq.com/s/sF-cx2ss4haO74K55Sjf9g

配置

首先是配置帐号信息

ssh -T git@github.com#登陆github

修改项目中的个人信息

git config –global user.name “githelper”
git config –global user.email githelper@gmail.com

config

git config –global user.name JSLite#设置提交用户名
git config –global user.email JSLite@yeah.net#设置提交邮箱
git config –list#查看配置的信息
git remote remove origin#删除该远程路径
git remote add origin git@jslite.github.com:JSLite/JSLite.git#添加远程路径

help

git help config#获取帮助信息

配置自动换行(自动转换坑太大)

git config –global core.autocrlf input#提交到git是自动将换行符转换为lf

配置密钥

ssh-keygen -t rsa -C JSLite@yeah.net#生成密钥
ssh -T git@github.com#测试是否成功

多账号ssh配置

1.生成指定名字的密钥

ssh-keygen -t rsa -C “邮箱地址” -f ~/.ssh/github_jslite
会生成github_jslite和github_jslite.pub这两个文件
2.密钥复制到托管平台上
vim ~/.ssh/github_jslite.pub
打开公钥文件github_jslite.pub,并把内容复制至代码托管平台上
3.修改config文件
vim ~/.ssh/config#修改config文件,如果没有创建config
Host jslite.github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_jslite
Host abc.github.com
HostName github.com
User git
IdentityFile ~/.ssh/github_abc
4.测试
ssh -T git@jslite.github.com#@后面跟上定义的Host

push

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

缩写 -f
全写–force
注:强制推送文件没有了哦

pull

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

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 就会在本地删除远程已经删除的分支。

$ git pull -p

等同于下面的命令

gitfetchpruneorigin g i t f e t c h – p r u n e o r i g i n git fetch -p

新建仓库

init

git init#初始化

status

git status#获取状态

add

git add file#.或*代表全部添加
git rm –cached 在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

git push -u origin master#push同事设置默认跟踪分支
git push origin master

从现有仓库克隆

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 <版本库的网址> <本地目录名>

gitclonehttp[s]://example.com/path/to/repo.git/ g i t c l o n e h t t p [ s ] : / / e x a m p l e . c o m / p a t h / t o / r e p o . g i t / git clone ssh://example.com/path/to/repo.git/
gitclonegit://example.com/path/to/repo.git/ g i t c l o n e g i t : / / e x a m p l e . c o m / p a t h / t o / r e p o . g i t / git clone /opt/git/project.git
gitclonefile:///opt/git/project.git g i t c l o n e f i l e : / / / o p t / g i t / p r o j e c t . g i t git clone ftp[s]://example.com/path/to/repo.git/
$ git clone rsync://example.com/path/to/repo.git/

submodule

git submodule add –force 仓库地址 路径

其中,仓库地址是指子模块仓库地址,路径指将子模块放置在当前工程下的路径。
注意:路径不能以 / 结尾(会造成修改不生效)、不能是现有工程已有的目录(不能順利 Clone)

git submodule init初始化submodule
git submodule update更新submodule(必须在根目录执行命令)

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

git submodule update –init –recursive下载的工程带有submodule
git submodule foreach git pullsubmodule 里有其他的 submodule 一次更新
git submodule foreach git pull origin mastersubmodule更新
git submodule foreach –recursive git submodule init
git submodule foreach –recursive git submodule update

本地

add

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

rm

rm &git rm #移除文件

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

commit

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\n1. ′ \n \n ″ 1. 详 细 描 述 ′ ’\n”2.详细描述’#提交简要描述和详细描述

reset

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

revert

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

checkout

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

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

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

merge

git merge –squash test##合并压缩,将test上的commit压缩为一条
cherry-pick
git cherry-pick commit#拣选合并,将commit合并到当前分支
git cherry-pick -n commit#拣选多个提交,合并完后可以继续拣选下一个提交

rebase

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

删除

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 push -u origin branchName#提交分支到远程origin主机中

拉取

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

分支合并

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

重命名

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

查看

git branch#列出本地分支
git branch -r#列出远端分支
git branch -a#列出所有分支
git branch -v#查看各个分支最后一个提交对象的信息
git branch –merge#查看已经合并到当前分支的分支
git branch –no-merge#查看为合并到当前分支的分支

新建

git branch test#新建test分支
git checkout -b newBrach origin/master#取回远程主机的更新以后,在它的基础上创建一个新的分支

连接

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

分支切换

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

远端

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分支[同时设置跟踪]

撤销远程记录

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

忽略文件

echo node_modules/ >> .gitignore

删除文件

git rm -rf node_modules/

源remote

git是一个分布式代码管理工具,所以可以支持多个仓库,在git里,服务器上的仓库在本地称之为remote。

个人开发时,多源用的可能不多,但多源其实非常有用。

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#查看指定源的全部信息

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请联系我们举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/139036.html原文链接:https://javaforall.net

(0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • ICTCLAS用的字Lucene4.9捆绑

    ICTCLAS用的字Lucene4.9捆绑

    2022年1月1日
    46
  • java 测试程序代码运行时间过长_Java测试

    java 测试程序代码运行时间过长_Java测试突然想准确的测试一下Java代码的执行时间,在网上找了一会。发现基本有以下两种方法:第一种是以毫秒为单位计算的。Java代码//伪代码  long startTime=System.currentTimeMillis();   //获取开始时间  doSomeThing();  //测试的代码段  long endTime=System.currentTime

    2022年10月18日
    0
  • 反掩码的作用是什么?通配符掩码的作用是什么?—Vecloud[通俗易懂]

    反掩码的作用是什么?通配符掩码的作用是什么?—Vecloud[通俗易懂]反掩码即路由器使用的通配符掩码与源或目标地址一起来分辨匹配的地址范围,跟子网掩码刚好相反。它像子网掩码告诉路由器IP地址的哪一位属于网络号一样,通配符掩码告诉路由器为了判断出匹配,它需要检查IP地址中的多少位。这个地址掩码对使我们可以只使用两个32位的号码来确定IP地址的范围。这是十分方便的,因为如果没有掩码的话,你不得不对每个匹配的IP客户地址加入一个单独的访问列表语句。这将造成很多额外的输入和路由器大量额外的处理过程。所以地址掩码对相当有用。在子网掩码中,将掩码的一位设成1表示IP地址对应的位.

    2022年7月19日
    16
  • springboot eclipse 热部署

    springboot eclipse 热部署springboot热部署配置很简单,并且配置好后开发变的非常舒服,修改东西后再也不用重新启动服务了!一、pom.xml添加依赖       org.springframework.boot        spring-boot-devtools        true                  org.springframework

    2022年5月20日
    27
  • scrollWidth,clientWidth,offsetWidth的区别

    scrollWidth,clientWidth,offsetWidth的区别

    【from:http://hi.baidu.com/zgq666/blog/item/54ee392a3cc1b7325243c103.html】
     
    网页可见区域宽:document.body.clientWidth;   
    网页可见区域高:document.body.clientHeight;   
    网页可见区域高:document.body.offsetWeight:   
    网页可见区域高:document.body.offse

    2022年7月22日
    5
  • mybatis逆向生成java代码_mybatis生成

    mybatis逆向生成java代码_mybatis生成前言有时候,我们创建实体类需要跟数据库表里面的字段对应起来。假如一张表有数百个字段,那么手动去写实体类的话就比较麻烦,而且容易出错。解决方案其实解决这个问题的方式有很多,本文介绍其中一种解决方案,通过mybatis的逆向工程生成实体类。本文使用的数据库是Oracle,MySQL只需要修改jar包以及generator.properties配置即可。可以从公众号【程序员高手之路】回复“逆向工程”获取源码!Step1修改p…

    2022年8月21日
    4

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号