繁体   English   中英

git-了解diff命令的输出

[英]Git- understanding the output of diff command

我最近开始研究使用Git作为其版本控制的Python / Django项目。

我对Git还是比较陌生的,之前没有使用过。 我目前有多个开发分支,并且希望将当前分支中的某些更改与主服务器合并,但不想合并该分支中的所有更改。

我的想法是将我要合并的更改所在的本地分支上的特定文件与master分支上的相同文件进行比较,以便可以看到要保留的更改和要丢弃的更改。 。 然后,我打算从master创建一个新分支,并手动复制要保留在当前本地分支中的一些更改。

我从本地分支运行了以下命令:

git diff budgetsReports3 master -- costing/views.py

为了查看我的本地budgetsReports3分支和master分支上的costing应用程序中views.py文件之间的差异。

此命令产生了以下输出:

 diff --git a/costing/views.py b/costing/views.py
index 452b082..f8a3f77 100644
--- a/costing/views.py
+++ b/costing/views.py
@@ -1324,12 +1324,6 @@ def report_overview(request, project_id):
    project = Project.objects.get(id=project_id)

        budget = get_current_budget(project_id)
-       #(01/12/2016 @ 1410) Add the missing code that's used in report_ccis(...) to display the individual CCI items on this page
-       cci_total_exc_final = budget.cci_total_exc_vat_final
-       print("cci_total_exc_final value in report_overview: ", cci_total_exc_final)
-       cci_grouped_items = budget.cci_items.all().order_by('project_room', 'name')
-       print("cci)grouped_items value in report_overview: ", cci_grouped_items)
-       #(01/12/2016 @ 1410) Added missing code...
    if not budget and not project.budget_versions.filter(current_marker=1):
            Budget.objects.create(project=project, current_marker=1)

 @@ -1343,9 +1337,6 @@ def report_overview(request, project_id):
            'project': project,
            'budget': budget,
            'cci_total_exc': cci_total_exc,
-               #(01/12/2016 @ 1410) Add the missing code that's used in report_ccis(...) to display the individual CCI items on this page
-               'cci_grouped_items': cci_grouped_items,
-               #ERF(01/12/2016 @ 1410) Added missing code...
            'item_total_exc': item_total_exc,
            'total_exc': total_exc,
            'total_exc_2': total_exc_2,
@@ -1460,15 +1451,11 @@ def report_by_class(request, project_id):

 def report_ccis(request, project_id):
    """ CCI items styled for pdf """
-       print ("report_ccis called from costing/views.py (line 1463) ")
    project = Project.objects.get(id=project_id)
    budget = get_current_budget(project_id)
-       #(06/12/2016 @ 1450) Create a boolean to determine whether or not to display 'Latest Sum'

    cci_total_exc = budget.cci_total_exc_vat_final
    cci_grouped_items = budget.cci_items.all().order_by('project_room', 'name')
-       print ("Value of cci_total_exc in costing/views.py (line 1469): ", cci_total_exc)
-       print ("Value of cci_grouped_items in costing/views.py (line 1470): ", cci_grouped_items)

我可以看到每个分支上的文件版本之间的差异以红色突出显示,但是我不确定哪个分支显示了哪个“差异”-大概diff命令显示的所有diff都在哪里我的本地分支上的文件与我的主分支上的文件不同吗? 所以我只需要仔细阅读这些内容,看看我想保留/合并到我的master分支中的哪个? 还是反过来显示差异?

git diff budgetsReports3 master可以理解为“需要对budgetsReports3进行哪些更改才能使其看起来像master

https://www.git-tower.com/learn/git/ebook/zh-CN/command-line/advanced-topics/diffs是了解差异的好资源

我目前有多个开发分支,并且希望将当前分支中的某些更改与主服务器合并,但不想合并该分支中的所有更改。

只要您只想合并一个分支到另一个分支的某些更改(而不合并其他更改),合并本身就不是使用的工具(无论如何,在git中)。 在git中,合并意味着该分支中的所有内容都已合并到另一个分支中。

有几种方法可以进行:

  1. 基于合并的工作流程:使用git rebase -i重写budgetsReports3分支的历史记录,以便要合并的更改在一个或多个提交中包含在所有其他提交之前。 然后合并那些提交到母版。 并可能将master合并到budgetsReports3。

  2. 一个基于基准的工作流程:重写budgetsReports3(与1相同),然后在主数据库上重新基准budgetsReports3。 然后将自包含的提交合并到master。 这将是快速向前合并。

  3. 基于diff的工作流程:采取git diff(在另一个方向上),使用git apply将其应用于master,使用git add -p仅在master之上添加一些差异,提交和重新调整budgetReports3。 例如:

     git checkout budgetsReports3 # if not already there git diff master budgetsReports3 -- costing/views.py > views.diff git checkout master git apply views.diff git add -p costing/views.py # follow the prompts to choose which patch hunks you want to apply git commit -m "your message" git reset HEAD costing/views.py git checkout costing/views.py git checkout budgetsReports3 git rebase master 

就我个人而言,我更喜欢基于基准的工作流,但这取决于您。 还要注意,如果您已经将budgetsReports3分支共享(推送或其他人拉回了)到另一个存储库,则不建议重新设置基准(除非您真的知道自己在做什么)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM