本文介绍了GIT-如何使文件在所有分支机构中都相同的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我在分支"master"中有一个文件"ChangeLog".我想在此文件的任何分支中记录有关所有更改的信息(比提交消息中更详细的信息以及其他描述性信息).

Suppose I have a file "ChangeLog" in branch "master". I want to record information about all changes in any branch in this file (in more detail than in a commit message and with other descriptive information).

I git checkout -b revA,执行编辑,更新ChangeLog和git commit.

I git checkout -b revA, perform edits, update ChangeLogand git commit.

然后是git checkout -b master.此结帐会将ChangeLog替换为分支"master"中的版本.

I then git checkout -b master. This checkout will replace ChangeLog with the version in branch "master".

我希望ChangeLog自动成为我签出了哪个分支的最新修改版本 .我不想将ChangeLog从其他某个分支手动合并(或者很可能忘记合并)到我当前的分支中.

What I would like is for ChangeLog to automagically be the most recently modified version irrespective of which branch I've checked out. I don't want to manually merge (or, most likely, forget to merge) ChangeLog from some other branch into my current branch.

我还没有发现任何可能允许这样做的东西.可以这样做吗?

I haven't found anything which seems to allow this. Is it possible to do this?

推荐答案

您可以使用结帐后挂钩:

You could use a post-checkout hook:

#!/bin/sh

newref="$2"
isbranch="$3"

# ignore file checkouts
if test $isbranch -eq 0; then
  exit 0
fi

path=ChangeLog

git for-each-ref --sort=-committerdate \
                 --format='%(objectname) %(refname:short)' \
                 refs/heads/\* |
while read sha ref; do
  if git rev-parse --verify --quiet "$sha:$path" >/dev/null
  then
    if test "$newref" != "$sha"; then
      echo Checking out $path from $ref
      git checkout $sha -- "$path"
    fi
    break
  fi
done

在适当的情况下,仍然可以自行决定在分支上添加和提交ChangeLog.

It will still be up to you to add and commit the ChangeLog on your branch if appropriate.

此挂钩按其头部各自的提交日期对分支进行排序,这可能会出现问题.假设您创建了一个主题分支,其根源于母版的历史记录.即使它的ChangeLog快照在日历时间上较旧,上面的钩子也将其视为最新快照,因为它是由最近创建的提交引用的,因此注意不要在未暂存对ChangeLog的更改时,通过切换分支来意外地丢失工作.

This hook sorts branches by their heads' respective commit dates, which could be problematic. Say you create a topic branch rooted at a commit in master's history. Even though its snapshot of ChangeLog is older in the sense of calendar time, the hook above will treat it as the newest because it is referenced by a commit that was created more recently, so be careful that you don't accidentally lose work by switching branches when you have unstaged changes to ChangeLog.

这篇关于GIT-如何使文件在所有分支机构中都相同的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 13:31