zoukankan      html  css  js  c++  java
  • skills_git

    skills_git

    1 git diff命令输出结果详解

    注意这个:@@ -1,7 +1,7 @@
    "-1,7″分成三个部分:减号表示第一个文件(即f1),"1″表示第1行,“7″表示连续7行。
    合在一起,就表示下面是第一个文件从第1行开始的连续7行。
    同样的,”+1,7″表示变动后,成为第二个文件从第1行开始的连续7行。
    reference: https://blog.csdn.net/CSDN___LYY/article/details/102555882

    2 本地仓库同时关联多个远程仓库github、gitee

    reference: https://blog.csdn.net/xiecheng1995/article/details/106570059

    3 克隆远程仓库到本地,指定自定义名称(默认是origin),指定分支(默认是master)

    git clone -o gitee -b main git@gitee.com:mediocrep/shells.git
    

    4 设置git push的默认分支为当前分支

    git config --global push.default "current"
    

    5 git 删除错误提交的文件

    reference: https://www.cnblogs.com/cposture/p/git.html
    reference: https://www.runoob.com/git/git-rm.html

    6 Github/Gitee仓库中彻底删除历史提交记录的方法

    git reset --hard HEAD^  # 这里的参数也可以设置为 --soft 或 --mixed,这样暂存区或工作区还会保留错误提交的文件,以便于修改后直接重新提交
    git push --force  # 必须加上参数 --force ,强制提交覆盖远程仓库的代码,不管是否有冲突
    

    好像是不能从版本库中单独彻底地删除某个文件。

    7 ubuntu18,升级git到最新版

    ubuntu18上执行 sudo apt-get install git 命令,默认安装的是旧版本2.17,因为它追求稳定。
    以下是升级到最新稳定版的方法:

    sudo add-apt-repository ppa:git-core/ppa
    sudo apt-get update
    sudo apt-get install git
    

    8 git修改分支名称

    reference: https://blog.csdn.net/y_s_jun/article/details/117433339#t1

    9 git对比暂存区和版本库HEAD的内容

    git中的文件可能会处于3个区域:工作区、暂存区(Stage)、版本库。

    # 对比工作区和版本库HEAD的内容
    git diff src/main/java/com/mediocre/entity/PostHalo.java
    # 对比暂存区和版本库HEAD的内容(添加了--staged 参数)
    git diff --staged src/main/java/com/mediocre/entity/PostHalo.java
    

    10 git如何放弃文件的修改

    1. 未使用 git add 缓存代码时:
      // 放弃单个文件修改,注意不要忘记中间的"--",不写就成了检出分支了!
    git checkout -- filepathname  
    // 放弃所有的文件修改  
    git checkout .    
    

    此命令用来放弃掉所有还没有加入到缓存区(就是 git add 命令)的修改:内容修改与整个文件删除。但是此命令不会删除掉刚新建的文件。因为刚新建的文件还没已有加入到 git 的管理系统中。所以对于git是未知的。自己手动删除就好了。

    1. 已经使用了 git add 缓存了代码:
      可以使用 git reset HEAD filepathname (比如: git reset HEAD readme.md)来放弃指定文件的缓存,放弃所有的缓存可以使用 git reset HEAD . 命令。
      此命令用来清除 git 对于文件修改的缓存。相当于撤销 git add 命令所在的工作。在使用本命令后,本地的修改并不会消失,而是回到了如(一)所示的状态。继续用(一)中的操作,就可以放弃本地的修改。

    2. 已经用 git commit 提交了代码:
      可以使用 git reset --hard HEAD^ 来回退到上一次commit的状态。
      此命令可以用来回退到任意版本:**git reset --hard commitid **
      --hard,根据需要可以换成--soft,或 --mixed,含义如下:

    git reset [<mode>] [<commit>]
               This form resets the current branch head to <commit> and possibly updates the index (resetting it to the tree of <commit>) and the working tree depending on <mode>. If <mode> is omitted,
               defaults to --mixed. The <mode> must be one of the following:
    
               --soft
                   Does not touch the index file or the working tree at all (but resets the head to <commit>, just like all modes do). This leaves all your changed files "Changes to be committed", as git
                   status would put it.
    
               --mixed
                   Resets the index but not the working tree (i.e., the changed files are preserved but not marked for commit) and reports what has not been updated. This is the default action.
    
                   If -N is specified, removed paths are marked as intent-to-add (see git-add(1)).
    
               --hard
                   Resets the index and working tree. Any changes to tracked files in the working tree since <commit> are discarded.
    
               --merge
                   Resets the index and updates the files in the working tree that are different between <commit> and HEAD, but keeps those which are different between the index and working tree (i.e. which
                   have changes which have not been added). If a file that is different between <commit> and the index has unstaged changes, reset is aborted.
    
                   In other words, --merge does something like a git read-tree -u -m <commit>, but carries forward unmerged index entries.
    
               --keep
                   Resets index entries and updates files in the working tree that are different between <commit> and HEAD. If a file that is different between <commit> and HEAD has local changes, reset is
                   aborted.
    
               --[no-]recurse-submodules
                   When the working tree is updated, using --recurse-submodules will also recursively reset the working tree of all active submodules according to the commit recorded in the superproject, also
                   setting the submodules' HEAD to be detached at that commit.
    

    你可以使用 **git log **命令来查看git的提交历史。git log 的输出如下,之一这里可以看到第一行就是 commitid。
    reference: https://www.jianshu.com/p/37f3a7e4a193

    11 查看历史记录(包括文件列表和详细的文件内容)

    git log --name-only  #  --name-only 仅显示每次修改的文件名称列表
    git log --stat -2  # 查看最近2次提交记录的文件列表。 --stat 查看文件列表; -2 查看最近2次提交记录
    git log --name-status  # --name-status 查看每次修改的文件列表, 显示状态M、A等
    git whatchanged -2  # 查看最近2次修改的文件列表
    git whatchanged --stat   # --stat 查看每次修改的文件列表, 及文件修改的统计
    git show   # 显示最后一次的文件改变的具体内容
    git show -2   # 显示最后 2 次的文件改变的具体内容
    git show commitid   # 显示某个 commitid 改变的具体内容
    

    12 git在本地新建分支并拉取远程的相同名称的分支到本地

    # 假设本地仓库关联的远程名称为origin,本地分支为master,要拉取远程分支dev到本地
    git fetch origin dev  # 把远程分支拉到本地
    git switch -c dev  # 在本地创建分支dev并切换到该分支,如果执行了第1步git fetch,则不需要加参数 -c
    git pull origin dev  # 把某个分支上的内容都拉取到本地,执行了以上2步,这一步不用执行了
    # note:如果本地分支和远程分支都有内容,可能会冲突,导致无法拉取远程分支到本地,报错如下:
    # fatal: 拒绝合并无关的历史
    # 此时加上该参数即可 --allow-unrelated-histories
    git pull gitee develop --allow-unrelated-histories
    

    14 git合并一个分支dev的提交到另一个分支main

    git switch main
    git merge dev
    

    也可以使用 git cherry-pick合并dev中特定的commit到main,不过遇到一些问题,详情参考:http://www.ruanyifeng.com/blog/2020/04/git-cherry-pick.html

    15

  • 相关阅读:
    SPOJ distinct subtrings
    OI题目类型总结整理
    emacs 考场配置
    SDOI2013 费用流
    HAOI2011 problem a
    BZOJ3438 小M的作物(和拓展)
    APIO2014 连珠线
    NOI2009 管道取珠
    HNOI2013 切糕
    NOI2009 植物大战僵尸
  • 原文地址:https://www.cnblogs.com/mediocreWorld/p/15145761.html
Copyright © 2011-2022 走看看