git命令行

常用

git clone

git checkout

 

git status -s

git diff

git log

git reset HEAD^

 

git add

git commit

git push

git help 和 git stash或xxx命令 --help

 (1)

PS D:\> git --help
usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [-
           [-p | --paginate | -P | --no-pager] [--no-replace-ob
           [--git-dir=<path>] [--work-tree=<path>] [--namespace
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the i

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introdu
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout    Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signe

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository
   push       Update remote refs along with associated objects

 (2)

cmd:git stash --help 来查看help:

浏览器显示 file:///D:/Git/mingw64/share/doc/git-doc/git-stash.html

查看仓库地址:git remote -v

git fetch 和git pull 的差别

1、git fetch 相当于是从远程获取最新到本地,不会自动merge,如下指令:

 git fetch orgin master //将远程仓库的master分支下载到本地当前branch中

 git log -p master  ..origin/master //比较本地的master分支和origin/master分支的差别

 git merge origin/master //进行合并

也可以用以下指令:

git fetch origin master:tmp //从远程仓库master分支获取最新,在本地建立tmp分支

git diff tmp //將當前分支和tmp進行對比

git merge tmp //合并tmp分支到当前分支

2. git pull:相当于是从远程获取最新版本并merge到本地

git pull origin master

git pull 相当于从远程获取最新版本并merge到本地

在实际使用中,git fetch更安全一些

git stash/ git stash pop

将改了的内容先放到内存,以免pull的时候冲突

other stash cmd:

    1) stash列表 git stash list查看

    2) 删除stash git stash drop <stash@{id}> 如果不加stash编号,默认的就是删除最新的,也就是编号为0的那个,加编号就是删除指定编号的stash。git stash clear 是清除所有stash

    3)更多:file:///D:/Git/mingw64/share/doc/git-doc/git-stash.html 如下:

    git stash list [<options>]
    git stash show [<options>] [<stash>]
    git stash drop [-q|--quiet] [<stash>]
    git stash ( pop | apply ) [--index] [-q|--quiet] [<stash>]
    git stash branch <branchname> [<stash>]
    git stash [push [-p|--patch] [-k|--[no-]keep-index] [-q|--quiet]
             [-u|--include-untracked] [-a|--all] [-m|--message <message>]
             [--] [<pathspec>…​]]
    git stash clear
    git stash create [<message>]
    git stash store [-m|--message <message>] [-q|--quiet] <commit>

git reset HEAD

(详见git回退:https://blog.csdn.net/chushoufengli/article/details/101683839

拉取最近一次提交到版本库的文件到暂存区 

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

拉取项目覆盖本地

(详见git回退:https://blog.csdn.net/chushoufengli/article/details/101683839

git fetch --all

git reset --hard origin/master

git pull

撤销commit

git add . 

git commit -m ""

之后想撤回commit:git reset --soft HEAD^

分支管理:


1. 创建分支:git branch (branchname)
//没有参数时,git branch 会列出你在本地的分支。

2. 分支信息:git branch
$ git branch
* master  //*当前分支
  newtest

3. 切换分支:git checkout (branchname) 
git checkout -b (branchname) 命令来创建新分支并立即切换到该分支下

git clone下来的有全部的分支,显示的分支只是默认显示分支

4. 删除分支:git branch -d (branchname)

5. 合并分支:git merge  //将任何分支合并到当前分支中去
$ git branch
* master
  newtest
$ git merge newtest

5-1. 合并产生冲突,需要手动去修改
$ git merge change_site
Auto-merging runoob.php
CONFLICT (content): Merge conflict in runoob.php
Automatic merge failed; fix conflicts and then commit the result.

5-2. 用 git add 要告诉 Git 文件冲突已经解决
$ git add runoob.php

5-3. 提交:
$ git commit

5-4. 注:没有冲突的合并,git status是没有东西的,直接push即可

两种方式来合并 详细https://blog.csdn.net/chushoufengli/article/details/102524506

合并到master:
  切换到master
  pull master
  git merge
  solve conflit
  git add .
  git commit
  git push

 feat分支和dev分支有冲突,因为dev上有改动,在feat分支上git pull origin dev,本地解决冲突,然后提交

git checkout 和 git checkout -b

当使用 git checkout -b sprint-2 切换到 sprint-2分支时 报 pathspec 'sprint-2' did not match any file(s) known to git 错误

使用 git checkout -b sprint-2 创建并切换 sprint-2 分支

使用 git pull origin sprint-2 从远程的 sprint-2 分支pull到本地的 sprint-2 (因为本地和远程不是一个源,所以pull需要加上origin sprint-2)

同理 git push --set-upstream origin sprint-2

使用git在本地新建一个分支后,需要做远程分支关联。如果没有关联,git会在下面的操作中提示你显示的添加关联。

关联目的是在执行git pull, git push操作时就不需要指定对应的远程分支,你只要没有显示指定,git pull的时候,就会提示你。

解决方法就是按照提示添加:

git branch --set-upstream-to=origin/remote_branch  your_branch

其中,origin/remote_branch是你本地分支对应的远程分支;your_branch是你当前的本地分支。

git checkout -b dev 与 git checkout -b dev master 有什么区别?

前者:创建了一个名称为dev的分支并切换到dev分支上去
后者:从master分支分化一个新分支名为dev,并切换到dev分支上去

merge 和 rebase 和 cherry-pick 的详细

见:《搞清:merge、–no-ff选项、merge –squash、rebase、cherry-pick》https://blog.csdn.net/chushoufengli/article/details/104819544

git remote set-url

git remote set-url origin http://gitlab.haibaodianjing.com/maoqiudao/server.git

取消add commit

取消add git reset HEAD
取消commit git reset HEAD^

检出某次提交(远程/本地)
git reflog
git checkout 5a009c7

git reset (–mixed) HEAD~1(待定)
回退一个版本,且会将暂存区的内容和本地已提交的内容全部恢复到未暂存的状态,不影响原来本地文件(未提交的也
不受影响)
git reset –soft HEAD~1
回退一个版本,不清空暂存区,将已提交的内容恢复到暂存区,不影响原来本地的文件(未提交的也不受影响)
git reset –hard HEAD~1
回退一个版本,清空暂存区,将已提交的内容的版本恢复到本地,本地的文件也将被恢复的版本替换

git log

git log 命令可以显示所有提交过的版本信息
git reflog 可以查看所有分支的所有操作记录(包括已经被删除的 commit 记录和 reset 的操作)

+ git checkout

.gitIngore内容:

# Created by .ignore support plugin (hsz.mobi)
.idea/
go.sum

git checkout 788258e49531eb24bfd347a600d69a16f966c495

删除git仓库

即删除仓库文件夹下隐藏的 .git 文件夹
$ ls -a
$ rm -rf .git
$ ls -a

查看某文件的历史提交

git log -p user.go
-p 选项展开显示每次提交的内容差异
git文档(https://git-scm.com/book/zh/v1/Git-%E5%9F%BA%E7%A1%80-%E6%9F%A5%E7%9C%8B%E6%8F%90%E4%BA%A4%E5%8E%86%E5%8F%B2)

git blame 追溯一个指定文件的历史修改记录

显示格式:commit ID  (代码提交作者  提交时间  代码位于文件中的行数)  实际代码

在这里插入图片描述

fork

(查看本地仓库 git remote -v  (remote:远程

本地添加远程库:git remote add wxx https://git.xxx.net/xx.wang/xxx

指定库和分支push:git push wxx b1

git rm 、git rm --cached的区别

【git rm】
当我们需要删除暂存区或分支上的文件, 同时工作区也不需要这个文件了, 可以使用git rm
git rm file = rm file+ git add file  (rm file删除本地文件,git add file 提交删除的步骤同步到git仓库

【git rm --cached file】
当我们需要删除暂存区或分支上的文件, 但本地又需要使用, 只是不希望这个文件被版本控制, 可以使用 git rm --cached
git rm --cached 会从index里面删除该文件,下次commit的时候会修改git仓库,但是本地的文件还是保留

【显示Untracked files】
Untrack and stop tracking files in git
有一些文件我们不想提交到git上去了,但是又被检测到有变化,显示Untracked files,我们可以采用
git rm --cached file来停止跟踪这些文件

git切换分支报错

error: pathspec '分支名' did not match any file(s) known to git.

执行一下:git fetch

 

 更多详细:

基本操作:   https://www.runoob.com/git/git-basic-operations.html

分支管理:  https://www.runoob.com/git/git-branch.html

git 详细文档:https://git-scm.com/book/zh/v2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值