简体   繁体   中英

Git- understanding the output of diff command

I have recently started working on a Python/ Django project that uses Git as its version control.

I am relatively new to Git, having not used it much before. I currently have a number of branches of development- and want to merge some of the changes on my current branch with the master, but don't want to merge all of the changes from this branch.

My thought was to compare the particular files on my local branch where the changes that I want to merge are, with the same files on the master branch, so that I could see which changes I want to keep, and which ones I want to discard. I was then planning on creating a new branch from master , and manually copying over the few changes that I want to keep from my current local branch.

I ran the following command from my local branch:

git diff budgetsReports3 master -- costing/views.py

in order to see the differences between the views.py file in the costing app on my local budgetsReports3 branch and the master branch.

This command has produced the following output:

 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)

I can see that the differences between the versions of the file on each of the branches are highlighted in red, but I'm not sure which branch is showing which 'difference'- presumably all of the differences shown by the diff command are where the file on my local branch is different to the file on my master branch? So I would just need to look through these, and see which ones I want to keep/ merge with my master branch? Or does it show the differences the other way round?

git diff budgetsReports3 master can be read as "What changes need to be made to budgetsReports3 to make it look like master "

A nice resource for understanding diffs is https://www.git-tower.com/learn/git/ebook/en/command-line/advanced-topics/diffs

I currently have a number of branches of development- and want to merge some of the changes on my current branch with the master, but don't want to merge all of the changes from this branch.

As long as you want to merge only some changes (and not others) from one branch to another, merge by itself is not the tool to use (in git anyway). In git, a merge means everything in this branch has been merged into the other branch.

There are a few ways to proceed:

  1. A merge based workflow: Use git rebase -i to rewrite the history of your budgetsReports3 branch so that the changes you want to merge are self contained in one or more commits before all the other commits. Then merge just those commits to master. And probably merge master into budgetsReports3.

  2. A rebase based workflow: Rewrite budgetsReports3 (as in 1) but then rebase budgetsReports3 on master. Then merge the self contained commits to master. This will be a fast forward merge.

  3. A diff based workflow: Take the git diff (in the other direction), use git apply to apply it to master, use git add -p to add only some of the diffs, commit, and rebase budgetReports3 on top of master. For example:

     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 

Personally, I prefer rebase based workflows, but that's up to you. Also note that if you have already shared (you have pushed or someone else has pulled) your budgetsReports3 branch to another repo, rebase is not advised (unless you really know what you're doing).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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