git 覆盖本地修改

When you learn to code, sooner or later you'll also learn about Version Control Systems. And while there are many competing tools in this space, one of them is the de facto standard used by almost everyone in the industry. It's so popular that there are companies that use its name in their branding. We're talking about Git, of course.

当您学习编码时,迟早也将了解版本控制系统。 尽管在这个领域有许多竞争性工具,但其中一个就是几乎每个行业内的人都在使用的事实上的标准。 它是如此流行,以至于有些公司在其品牌中使用了它的名字。 当然,我们在谈论Git。

While Git is a powerful tool, its power is well-hidden. There are some essential concepts that you need to understand to become really proficient with Git. The good news is that once you learn them, you'll hardly ever run into trouble you can't escape from.

虽然Git是强大的工具,但它的功能是隐藏的。 要真正精通Git,您需要了解一些基本概念。 好消息是,一旦您学习了它们,就几乎不会遇到无法逃脱的麻烦。

典型工作流程 (The Typical Workflow)

In a typical Git workflow you'll use a local repository, a remote repository, and one or more branches. Repositories store all the information about the project, including its entire history and all the branches. A branch is basically a collection of changes leading from an empty project to the current state.

在典型的Git工作流程中,您将使用本地存储库,远程存储库以及一个或多个分支。 存储库存储有关项目的所有信息,包括项目的整个历史记录和所有分支。 分支基本上是从空项目到当前状态的更改的集合。

After cloning a repository, you work on your local copy and introduce new changes. Until you push local changes to the remote repository, all your work is available only on your machine.

克隆存储库后,您将在本地副本上工作并引入新的更改。 在将本地更改推送到远程存储库之前,所有工作仅在计算机上可用。

When you finish a task, it's time to synchronize with the remote repository. You want to pull the remote changes to keep up with the project's progress, and you want to push the local changes to share your work with others.

完成任务后,就该与远程存储库进行同步了。 您希望提取远程更改以跟上项目的进度,并且希望推送本地更改以与他人共享您的工作。

当地变化 (Local Changes)

All is well when you and the rest of your team are working on totally separate files. Whatever happens, you won't be stepping on each other's feet.

当您和团队的其他成员正在处理完全独立的文件时,一切都很好。 无论发生什么,您都不会踩到对方的脚。

However, there are times when you and your teammates simultaneously introduce changes in the same place. And that's usually where the problems begin.

但是,有时您和您的队友会在同一位置同时进行更改。 这通常是问题开始的地方。

Have you ever executed git pull only to see the dreaded error: Your local changes to the following files would be overwritten by merge:? Sooner or later, everyone runs into that problem.

您是否曾经执行过git pull才能看到可怕的error: Your local changes to the following files would be overwritten by merge: 迟早,每个人都会遇到这个问题。

What's more confusing here is that you don't want to merge anything, just pull, right? Actually, pull is a bit more complicated than you might have thought.

更令人困惑的是,您不想合并任何东西,只是拉,对不对? 实际上,拉动比您想象的要复杂一些。

Git Pull如何工作? (How Exactly does Git Pull Work?)

Pull is not a single operation. It consists of fetching data from the remote server and then merging the changes with the local repository. These two operations can be performed manually if you want:

拉并不是一个单一的操作。 它包括从远程服务器获取数据,然后将更改与本地存储库合并。 如果需要,可以手动执行以下两个操作:

git fetch
git merge origin/$CURRENT_BRANCH

The origin/$CURRENT_BRANCH part means that:

origin/$CURRENT_BRANCH部分的意思是:

  • Git will merge the changes from the remote repository named origin (the one you cloned from)

    Git将合并来自名为origin (您从中克隆的源)的远程存储库中的更改

  • that have been added to the $CURRENT_BRANCH

    已添加到$CURRENT_BRANCH

  • that are not already present in your local checked out branch您的本地已结帐分支中尚不存在的

Since Git only performs merges when there are no uncommitted changes, every time you run git pull with uncommitted changes could get you into trouble. Fortunately, there are ways to get out of trouble in one piece!

由于Git仅在没有未提交的更改时执行合并,因此每次使用未提交的更改运行git pull时都会惹上麻烦。 幸运的是,有多种方法可以一劳永逸!

不同的方法 (Different Approaches)

When you have uncommitted local changes and still want to pull a new version from the remote server, your use case typically falls into one of the following scenarios. Either:

当您尚未提交本地更改并且仍想从远程服务器中提取新版本时,您的用例通常属于以下情况之一。 要么:

  • you don't care about the local changes and want to overwrite them,您不在乎本地更改,而是要覆盖它们,
  • you care about the changes very much and would like to apply them after the remote changes,您非常关心更改,并希望在远程更改后应用它们,
  • you want to download the remote modifications but not apply them yet您要下载远程修改但尚未应用

Each of the approaches requires a different solution.

每种方法都需要不同的解决方案。

您不在乎本地变化 (You Don't Care About the Local Changes)

In this case, you just want to drop all the uncommitted local changes. Perhaps you modified a file to experiment, but you no longer need the modification. All you care about is being up to date with the upstream.

在这种情况下,您只想删除所有未提交的本地更改。 也许您修改了文件进行实验,但是您不再需要修改。 您所关心的只是与上游保持最新。

This means that you add one more step between fetching the remote changes and merging them. This step will reset the branch to its unmodified state, thus allowing git merge to work.

这意味着您在获取远程更改与合并它们之间又增加了一步。 此步骤会将分支重置为其未修改状态,从而允许git merge工作。

git fetch
git reset --hard HEAD
git merge origin/$CURRENT_BRANCH

If you don't want to type the branch name every time you run this command, Git has a nice shortcut pointing to the upstream branch: @{u}. An upstream branch is the branch in the remote repository that you push to and fetch from.

如果您不想每次运行该命令时都输入分支名称,则Git有一个指向上游分支的不错的快捷方式: @{u} 。 上游分支是您在其中推送和获取的远程存储库中的分支。

This is how the above commands would look like with the shortcut:

这是使用快捷方式的上述命令的外观:

git fetch
git reset --hard HEAD
git merge '@{u}'

We are quoting the shortcut in the example to prevent the shell from interpreting it.

我们在示例中引用了快捷方式,以防止shell解释它。

您非常关心本地变化 (You Very Much Care About the Local Changes)

When your uncommitted changes are significant to you, there are two options. You can commit them and then perform git pull, or you can stash them.

当您的未提交更改对您很重要时,有两种选择。 您可以提交它们,然后执行git pull ,也可以隐藏它们。

Stashing means putting the changes away for a moment to bring them back later. To be more precise, git stash creates a commit that is not visible on your current branch, but is still accessible by Git.

存放意味着暂时保留更改,以便稍后将其恢复。 更准确地说, git stash创建一个提交,该提交在当前分支上不可见,但仍可由Git访问。

To bring back the changes saved in the last stash, you use the git stash pop command. After successfully applying the stashed changes, this command also removes the stash commit as it is no longer needed.

要恢复保存在上一个存储中的更改,请使用git stash pop命令。 成功应用隐藏的更改后,此命令还将删除隐藏提交,因为不再需要它。

The workflow could then look like this:

工作流程如下所示:

git fetch
git stash
git merge '@{u}'
git stash pop

By default, the changes from the stash will become staged. If you want to unstage them, use the command git restore --staged (if using Git newer than 2.25.0).

默认情况下,从存储中进行的更改将被暂存。 如果要git restore --staged ,请使用命令git restore --staged (如果使用的是比2.25.0更新的Git)。

您只想下载远程更改 (You Just Want to Download the Remote Changes)

The last scenario is a little different from the previous ones. Let's say that you are in the middle of a very messy refactoring. Neither losing the changes nor stashing them is an option. Yet, you still want to have the remote changes available to run git diff against them.

最后一种情况与先前的情况有些不同。 假设您正处于非常混乱的重构中。 既不丢失更改也不存储它们是一种选择。 但是,您仍然希望可以使用远程更改来对它们运行git diff

As you have probably figured out, downloading the remote changes does not require git pull at all! git fetch is just enough.

您可能已经知道,下载远程更改根本不需要git pullgit fetch就足够了。

One thing to note is that by default, git fetch will only bring you changes from the current branch. To get all the changes from all the branches, use git fetch --all. And if you'd like to clean up some of the branches that no longer exist in the remote repository, git fetch --all --prune will do the cleaning up!

需要注意的一件事是,默认情况下, git fetch只会带来当前分支的更改。 要从所有分支获取所有更改,请使用git fetch --all 。 而且,如果您想清理远程存储库中不再存在的某些分支,则git fetch --all --prune将进行清理!

一些自动化 (Some Automation)

Have you heard of Git Config? It's a file where Git stores all of the user-configured settings. It resides in your home directory: either as ~/.gitconfig or ~/.config/git/config. You can edit it to add some custom aliases that will be understood as Git commands.

您听说过Git Config吗? Git是一个文件,用于存储所有用户配置的设置。 它位于您的主目录中: ~/.gitconfig~/.config/git/config 。 您可以对其进行编辑以添加一些自定义别名,这些别名将被理解为Git命令。

For example, to have a shortcut equivalent to git diff --cached (that shows the difference between the current branch and the staged files), you'd add the following section:

例如,要具有与git diff --cached等效的快捷方式(它显示当前分支和暂存文件之间的差异),请添加以下部分:

[alias]dc = diff --cached

After that, you can run git dc whenever you wish to review the changes. Going this way, we can set up a few aliases related to the previous use cases.

之后,只要您想查看更改,就可以运行git dc 。 这样,我们可以设置一些与先前用例相关的别名。

[alias]pull_force = !"git fetch --all; git reset --hard HEAD; git merge @{u}"pf = pull_forcepull_stash = !"git fetch --all; git stash; git merge @{u}; git stash pop"

This way, running git pull_force will overwrite the local changes, while git pull_stash will preserve them.

这样,运行git pull_force将覆盖本地更改,而git pull_stash将保留它们。

其他Git拉力 (The Other Git Pull Force)

Curious minds may have already discovered that there is such a thing as git pull --force. However, this is a very different beast to what's presented in this article.

好奇的人可能已经发现存在git pull --force类的东西。 但是,这与本文中介绍的野兽完全不同。

It may sound like something that would help us overwrite local changes. Instead, it lets us fetch the changes from one remote branch to a different local branch. git pull --force only modifies the behavior of the fetching part. It is therefore equivalent to git fetch --force.

听起来可能会帮助我们覆盖本地更改。 相反,它使我们能够将更改从一个远程分支转移到另一个本地分支。 git pull --force仅修改获取部分的行为。 因此,它等效于git fetch --force

Like git push, git fetch allows us to specify which local and remote branch do we want to operate on. git fetch origin/feature-1:my-feature will mean that the changes in the feature-1 branch from the remote repository will end up visible on the local branch my-feature. When such an operation modifies the existing history, it is not permitted by Git without an explicit --force parameter.

git push一样, git fetch允许我们指定要在哪个本地和远程分支上进行操作。 git fetch origin/feature-1:my-feature意味着远程存储库中feature-1分支中的更改最终将在本地分支my-feature上可见。 当这样的操作修改了现有的历史记录时,如果没有显式的--force参数,Git将不允许这样做。

Just like git push --force allows overwriting remote branches, git fetch --force (or git pull --force) allows overwriting local branches. It is always used with source and destination branches mentioned as parameters. An alternative approach to overwriting local changes using git --pull force could be git pull --force "@{u}:HEAD".

就像git push --force允许覆盖远程分支一样, git fetch --force (或git pull --force )允许覆盖本地分支。 它始终与作为参数提及的源分支和目标分支一起使用。 使用git --pull force覆盖局部更改的另一种方法是git pull --force "@{u}:HEAD"

结论 (Conclusion)

The world of Git is vast. This article covered only one of the facets of repository maintenance: incorporating remote changes into a local repository. Even this everyday scenario required us to look slightly more in-depth into this version control tool's internal mechanisms.

Git的世界辽阔。 本文仅涉及存储库维护的一个方面:将远程更改合并到本地存储库中。 即使是这种日常情况,也需要我们稍微更深入地了解此版本控制工具的内部机制。

Learning actual use cases helps you better understand how Git works under the hood. This, in turn, will make you feel empowered whenever you get yourself into trouble. We all do that from time to time.

学习实际用例有助于您更好地了解Git的工作原理。 反过来,这会使您在遇到麻烦时感到有力量。 我们每个人都不时这样做。

翻译自: https://www.freecodecamp.org/news/git-pull-force-how-to-overwrite-local-changes-with-git/

git 覆盖本地修改

git 覆盖本地修改_Git拉力–如何使用Git覆盖本地更改相关推荐

  1. checkout 撤销修改_Git的4个阶段的撤销更改

    虽然git诞生距今已有12年之久,网上各种关于git的介绍文章数不胜数,但是依然有很多人(包括我自己在内)对于它的功能不能完全掌握.以下的介绍只是基于我个人对于git的理解,并且可能生编硬造了一些不完 ...

  2. git 代码回滚_git代码版本管理(1)——git版本回滚

    git代码版本管理(1)--git版本回滚 1.问题背景 在利用github.gitlab.Gitee等代码管理器中对代码的管理,我们有时会出现错误提交的情况,此时我们希望能撤销提交操作,让程序回到提 ...

  3. git 快速清理本地分支_Git删除本地多个分支

    由于本人是做QA的,经常需要发布很多分支,所以本地存留了很多的分支,需要批量删除本地分支:找到如下方法,有需要的人可以用到,多谢点赞. 要删除本地,首先要考虑以下三点 1.列出所有本地分支 2.搜索目 ...

  4. git查看分支记录_git原理

    标准用法请参考git-scm.本文记录笔者对git的一些理解,如有错误,欢迎指正. 引用内容已用markdown记号标出.版权所有,转载请注明出处. 文章完成中 这是引用内容 First Editio ...

  5. git 修改本地用户名_git简单介绍

    Git是目前世界上最先进的分布式版本控制系统. Git 如此的优秀是因为,Git 跟踪并管理的不是文件,而是修改. 需要填写用户名和邮箱作为一个标识. git config --global user ...

  6. git reset 怎么还原_git 本地修改被reset后怎么恢复

    reset命令有3种方式: 1:git reset –mixed:此为默认方式,不带任何参数的git reset,即时这种方式,它回退到某个版本,只保留源码,回退commit和index信息 2:gi ...

  7. git 怎么备份本地分支_Git常用个人备份笔记

    一.新建代码库 1.在当前目录下创建一个Git代码库 git init 创建的.git默认是隐藏的,使用命令ls -ah显示出来. 2.新建一个目录,并初始化为Git的代码库 git init [di ...

  8. 本地git存储库关闭_Git 入门:术语基础 | Linux 中国

    想学习 Git?看看这个最重要的术语和命令的快速总结. • 来源:linux.cn • 作者:Matthew Broberg • 译者:Xingyu.Wang • (本文字数:4793,阅读时长大约: ...

  9. git丢弃本地修改的所有文件(新增、删除、修改)

    本地修改了许多文件,其中有些是新增的,因为开发需要这些都不要了,想要丢弃掉,可以使用如下命令: git checkout . #本地所有修改的.没有的提交的,都返回到原来的状态 git stash # ...

最新文章

  1. 深度学习巨头Yoshua Bengio清华演讲: 深度学习通往人类水平人工智能的挑战
  2. TX Pattern Generator功能块
  3. 搭建yum源,配置yum源
  4. 对象存储/编码/解码
  5. arm-linux内核编译过程小结
  6. adapt和adopt的区别_“适应”是“ adapt ”还是“ adopt ”?看完你就会了
  7. ssl1072-砝码称重【dp练习】
  8. QueryDSL中包含通配符的字符串的精确匹配
  9. 了解普通人的心理,在销售中非常重要
  10. 解决org.hibernate.QueryException illegal attempt to dereference collection 异常错误
  11. byte[] 转化为 string 转化为汉字和字母
  12. java计算机毕业设计基于安卓Android的团务智慧管理APP
  13. 转:中文汉字占二个字节还是三个字节长度
  14. REST-assured基本使用
  15. b、blockquote、base、bdo、big、标签
  16. python 惰性属性_python中惰性对象
  17. Caffe新手教程傻瓜系列(9):训练和测试自己的图片
  18. [ctf.show.reverse] 红包六
  19. 开发人员为何应该使用苹果电脑,兼Mac OS X
  20. 0.91英寸OLED初始化程序

热门文章

  1. 【js】common.jsp的使用
  2. iOS SwiftUI篇-3 排版布局layout
  3. a++浏览器_“公司发给我的职业装太小了,我该怎么和领导说?”哈哈哈哈哈|职业装|uc|浏览器...
  4. [MaxCompute MapReduce实践]通过简单瘦身,解决Dataworks 10M文件限制问题
  5. 如何在OS X中打印到PDF文件
  6. 【虚拟化实战】VM设计之一vCPU
  7. 路由器互联端口处于不同网段的路由方法和原理
  8. 基于Idea从零搭建一个最简单的vue项目
  9. clientdataset 用法
  10. slf4j 日志监控