作为程序员一定要保持良好的睡眠,才能好编程

git常用的命令汇总

发布时间:2020-01-26

jgit.png

git config core.autocrlf false



hard:撤销并删除相应的更新


soft:撤销相应的更新,并把这些更新的内容放到 Stage 中




五、Delete --- 删除操作

1、rm:只在 WorkSpace 删除相应文件,如果想提交更新,以后得自己 add 到 Stage


2、git rm:在删除 WorkSpace 相应文件的同时,将这些删除更新到 Stage 中






查看系统config

git config --system --list

1

查看当前用户(global)配置

git config --global  --list

1

查看当前仓库配置信息

git config --local  --list




修改git提交时的名字

git config --global user.name "your name"


查看当前的user.name 的名字

git config --global user.name


git config --global core.autocrlf false



$ git config --list

core.symlinks=false

core.autocrlf=true

core.fscache=true

color.diff=auto

color.status=auto

color.branch=auto

color.interactive=true

help.format=html

diff.astextplain.textconv=astextplain

rebase.autosquash=true

http.sslbackend=openssl

http.sslcainfo=C:/Program Files/Git/mingw64/ssl/certs/ca-bundle.crt

credential.helper=manager

user.name=songyongzhan

user.email=songyz@guahao.com

core.repositoryformatversion=0

core.filemode=false

core.bare=false

core.logallrefupdates=true

core.symlinks=false

core.ignorecase=true

remote.origin.url=git@git.guahao-inc.com:anhao/PHP/business-service-client.git

remote.origin.fetch=+refs/heads/*:refs/remotes/origin/*

branch.master.remote=origin

branch.master.merge=refs/heads/master

branch.feature/20190719_songyz_sms.remote=origin

branch.feature/20190719_songyz_sms.merge=refs/heads/feature/20190719_songyz_sms

branch.feature/20190806_songyz_smsService.remote=origin

branch.feature/20190806_songyz_smsService.merge=refs/heads/feature/20190806_songyz_smsService


版本回退:

git reflog 显示回退的id

git reset --hard commit-id 回退到你想要的版本

git reset --hard HEAD^^ 向上回退两个版本

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

git reset HEAD~5 撤销过去5个commit的命令

git reset --hard bebb929a38af


git reset --hard


git reset application/controllers/Base.php




总结下git在开发中常用的命令。有不对的地方,欢迎大家指正。


git init:初始化当前目录为git仓库
git clone 仓库地址:下载一份git仓库到本地(clone下来的仓库的默认名为origin,而且下载的是主分支,主分支一般是master)

$ git clone <版本库的网址> <本地目录名>

git status:检查本地是否有更新。
git status -s:和git status一样,显示的内容更简单
git add 文件或目录:添加文件到缓冲区
git commit -m '提交说明':将缓冲区的文件提交到本地git仓库(本地生成一份版本快照)
git push 本地仓库名 分支名:将本地最新的git快照推送到远程git仓库





git diff:将当前本地(非缓冲区,即未add的文件)和本地最新的快照进行比较

             显示两个文件的不同

            11.jpg

git diff --cached:将当前本地(已add但未commit的)和本地最新的快照进行比较
git diff HEAD:相当于git diff和git diff --cached的合并语句,比较本地(不管有没有add)和本地最新的快照
git reset HEAD -- 文件名:取消缓存某个文件(比如你add了两个文件,但是想分两次提交,这时可以reset其中一个文件,没有reset的那个文件还在缓冲区,reset的文件则"撤销了add")
git rm 文件名:将文件从缓冲区移除,并物理删除该文件。这个命令其实并不常用,如果你直接物理删除了不想要的文件,使用git add和git commit后,git会自动把物理删除的文件从仓库中删除。


暂存当前分支:

git stash 隐藏当前分支 相当于暂存

git stash list  列出所有stash列表

git stash apply 恢复暂存区

git stash drop 删除暂存区

git branch:列出本地所有的分支(带*号的是主分支,进行git init的时候,默认情况下git会创建mater分支)
git branch 分支名:创建新分支(新分支将和本地的最新快照保持一致)
git checkout 分支名:切换(本地)分支
git checkout -b 分支名:相当于git branch 新分支A  和git checkout 新分支A的命令的集合(创建新分支,并立即切换到它)
git branch -d 分支名:删除(本地)分支

git branch -m oldname newname 更换分支名称 更改分支名称  分支更名

追踪关系 设置本地分支与远程分支关联关系
git branch --set-upstream master origin/feature-songyz-0515


git merge 分支名:此命令将会把选择的分支和主分支进行合并




git branch  用来查看本地分支的

$ git branch
* master

git branch -r 用来查看远程分支的

$ git branch -r
  origin/HEAD -> origin/master
  origin/master

git branch -a 选项查看所有分支

$ git branch -a
* master
  remotes/origin/HEAD -> origin/master
  remotes/origin/master

上面命令表示 ,本地主机当前分支是master ,远程分支是origin/master


取回远程主机的更新疑惑,可以在它的基础上 使用git checkout 命令创建一个新的分支

git checkout  -b newBranch origin/master

从远程服务器获取数(在origin/master的基础上)据并建立一个新的分支取名 newBranch。


git merge 命令合并分支 在当地分支 合并远程分支

git merge origin/master



git remote:列出远程项目的仓库列表(显示的是仓库名)
git remote -v:列出远程仓库列表,并显示仓库的URL
git remote add 仓库名 xx@xx.com:用户名/项目名.git:给远程项目添加新的仓库
git remote rm 仓库名:删除远端仓库


解释说明:

git remove  git要求每个远程主机都必须指定一个主机名, 用于管理主机名 不带选项的时候,git remove 命令列出所有远程主机


$ git remote -v
origin  https://yiqieyuanyuni.visualstudio.com/_git/Myds_PHP_FRAME (fetch)
origin  https://yiqieyuanyuni.visualstudio.com/_git/Myds_PHP_FRAME (push)
上面命令表示当前只有一台远程主机,叫做origin 以及他的网址

克隆版本库的时候,所使用的远程主机自动被git命名为origin ,如果想用其他主机名,需要用

git clone 命令的 -o 选项指定

例如:

$ git clone -o jQuery https://github.com/jquery/jquery.git
$ git remote
jQuery


$ git remote show origin  查看该主机详细信息
* remote origin
  Fetch URL: https://yiqieyuanyuni.visualstudio.com/_git/Mybs_PHP_FRAME
  Push  URL: https://yiqieyuanyuni.visualstudio.com/_git/Mybs_PHP_FRAME
  HEAD branch: master
  Remote branch:
    master tracked
  Local branch configured for 'git pull':
    master merges with remote master
  Local ref configured for 'git push':
    master pushes to master (up to date)


git remote add

git remote rm

git remote rename


远程主机一旦有了版本更新,需要将这些数据更新取回本地,这时就需要用到

git fetch <远程名>

例如  git fetch origin     #所使用的远程主机自动被git命名为origin

此命令将远程主机的更新全部取回本地

但是取回来并没有显示,需要再次执行 git merge  然后才能合并在一起。


git fetch 通常用来查看其他人的进程,因为他取回的代码对本地的开发代码没有影响。

默认情况下,git fetch 取回所有分支branch的更新。如果只想取回特定分支的更新,,可以采用

git fetch <origin远程主机名> <branch分支名>

比如取回:git fetch origin master  解释下:取回主机origin 下的master 分支的更新


git fetch 远端仓库名 远端分支名:tmp:下载远程仓库到本地,取名tmp(该分支不允许切换,但是可以diff和merge)

git merge tmp:把tmp分支和主分支合并


git log:查看版本记录(详细)
git log --stat:查看版本记录(精简)
git log --pretty=oneline:查看所有提交的版本记录,每行显示一条


git reset --hard HEAD~3 将文件最近的三次提交回退。

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

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

git reset --hard 版本号:回退到指定版本
$ git reset --hard aa47270e882f9cbac
HEAD is now at aa47270 Merge branch 'master' of https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME

版本回退,不需要输入完整的版本号,只需要输入前几位即可,git会自动检索。输入的版本号建议在6位以上。



pull或merge更新冲突时(本地文件可能被合并和产生冲突):
git stash:将本地的文件“暂时存储”,以便之后“弹出”
git pull 远端仓库名 分支名:更新远端的仓库分支到本地
git stash pop:将本地暂时存储的文件“弹出”,此时可能产生冲突,请注意修改冲突再进行add和commit和push



经验之谈:

每次使用git的时候,先git pull  一下  获取到最新的资源。



这是命令的等价公式

git pull   <==> git  fetch origin   + git merge


当提交本地数据到远程服务器时,本地与服务器的代码有不同,那么会出现下列错误:

$ git push -u origin master
To https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME
 ! [rejected]        master -> master (fetch first)
error: failed to push some refs to 'https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME'
hint: Updates were rejected because the remote contains work that you do
hint: not have locally. This is usually caused by another repository pushing
hint: to the same ref. You may want to first integrate the remote changes
hint: (e.g., 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

Updates were rejected because the remote contains work that you do not have locally
更新被拒绝,因为远程包含了本地没有的工作。

This is usually caused by another repository pushing to the same ref.
这通常是由另一个存储库推到同一个参考文件引起的。
You may want to first integrate the remote changes before pushing again.
您可能希望在再次推之前首先集成远程更改。
See the 'Note about fast-forwards' in 'git push --help' for details.
在“Git推-帮助”中查看关于“快进”的详细信息。


这是提交成功了
$ git push -u origin master
Counting objects: 3, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 337 bytes | 337.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Analyzing objects... (3/3) (97 ms)
remote: Storing packfile... done (61 ms)
remote: Storing index... done (31 ms)
To https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME
   dc4c6da..9115592  master -> master
Branch 'master' set up to track remote branch 'master' from 'origin'.




情况说明: 
客户端2:提交了一些修改文件,并新建立了一个b.php 提交到了服务器
客户端1:客户端2修改的文件,本地没有,且本地也对客户端2修改的文件做了修改,本地没有b.php
那么执行 git fetch origin 会发生什么事情?

$ git fetch origin
remote: Microsoft (R) Visual Studio (R) Team Services
remote: Found 9 objects to send. (15 ms)
Unpacking objects: 100% (9/9), done.
From https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME
   1abd61d..71f3e40  master     -> origin/master

到这里看来是执行成功了。

并没有看到变化,需要再次执行一句代码   git merge  才能完全合并

这样再次查看就能看到变化了。





情况说明2:
客户端2:修改了b.php 并通过push 提交到了远程服务器
客户端1:b.php还是原来的(在客户端2修改之前的),现在呢客户端2也修改b.php文件,
那么客户端1 执行git push -u origin master 会发生什么事情?
git push -u origin master

$ git push -u origin master
To https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME
 ! [rejected]        master -> master (non-fast-forward)
error: failed to push some refs to 'https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME'
hint: Updates were rejected because the tip of your current branch is behind
hint: its remote counterpart. Integrate the remote changes (e.g.
hint: 'git pull ...') before pushing again.
hint: See the 'Note about fast-forwards' in 'git push --help' for details.

出现以上问题,怎么办呢?


情景:提交时本地代码与服务器代码不一致,怎么办?
请执行 git pull
$git pull
Auto-merging b.php    #这里进行了提醒  自动合并了文件
Merge made by the 'recursive' strategy. #合并采用了 “递归策略”合并
 b.php | 5 +++++
 1 file changed, 5 insertions(+)

然后我们再次 添加 、commit  、push 提交到服务器

git add b.php

git commit -m "//合并后的b.php"

git push -u origin master

提交到服务器




情景说明: 同样客户端1 和客户端2 都对文件b.php 进行了修改 

客户端2 进行了push提交到服务器,

然后客户端1提交的时候就会报错:

git push -u origin master

error: Your local changes to the following files would be overwritten by merge:
        b.php
Please commit your changes or stash them before you merge.
Aborting
Updating aa47270..2f5cbe9


错误:您对以下文件的本地更改将被合并覆盖:
b.php
请在合并前提交更改或隐藏更改。
中止
更新2f5cbe9 aa47270 ..


那么出现这个情况 就是 两个人对同一个文件的一行代码进行了修改,或同一个块进行修改,

git不能自动的判断,整合成一个文件,因此需要人为的去整合这个文件。

那么整合这个文件有两种方式:

第一种:把服务器上最新的代码下载下来,与本地文件进行比对,然后进行调整,保存再次上传

那么看看怎么操作:

$ git stash
Saved working directory and index state WIP on master: aa47270 Merge branch 'master' of https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME
保存了当前本地修改的一个版本库


$git pull 获取服务器上最新的版本



$git stash pop  恢复到本地,自动合并一些文件

Auto-merging b.php
CONFLICT (content): Merge conflict in b.php

自动合并了 b.php
合并冲突(conflict in b.php content):


通过 git diff  -w  b.php 查看两个文件的不同之处

$ git diff -w b.php
diff --cc b.php
index fe62f86,bc5e0a7..0000000
--- a/b.php
+++ b/b.php
@@@ -1,7 -1,7 +1,11 @@@
  b.php

++<<<<<<< Updated upstream
 +是否新增了连接mysql数据库的功能?
++=======
+ 是否yubushi新增了连接数据库的功能?
++>>>>>>> Stashed changes


  iiifkfkfk

20.png


以上蓝色快就是代码冲突的位置,

然后通过软件打开b.php 进行代码的修复后  重新上传

+ 是否yubushi新增了连接mysql数据库的功能?


我修复成了 上面的这句代码,然后通过

git add b.php

git commit -m "//合并了b.php 的冲突"

git push -u origin master

这样本地就得到了最新的数据了。


第二种:把本地的git版本库倒退到上一个版本,然后进行pull 到服务器最新,再把本地文件  git add  .  git comit  .  git push  上传到服务器。

git log

查看到上一个版本的日志。

返回到上一个版本

git reset --hard  cc8ad85391071ea5b2dcb492

git pull  获取到服务器最新数据,修改完毕后再次提交。

然后 获取到最新b.php 进行代码的修改后,重新提交。

22.png

退到上一个版本,重新编辑后再次上传即可。


保存当前

$ git stash
Saved working directory and index state WIP on master: aa47270 Merge branch 'master' of https://yiqieyuanyuni.visualstudio.com/_git/My_PHP_FRAME


git1.jpg

git2.jpg


实践练习:

http://blog.csdn.net/menxu_work/article/details/19113157

http://blog.csdn.net/u012575819/article/details/50553501


   删除远程版本  git push origin :br-1.0.0 

  1. 删除远程分支  

  2. git branch -r -d origin/branch-name  

  3. git push origin :branch-name   



图形git使用说明:

https://marklodato.github.io/visual-git-guide/index-zh-cn.html#rebase