Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9162619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:15:14+00:00 2026-06-17T14:15:14+00:00

I’m struggling to understand how the following behavior is a good thing in git.

  • 0

I’m struggling to understand how the following behavior is a good thing in git. See below for an example I put together to help illustrate my problem. Many times my team and myself are getting changes/commits going into branches that we did not want to go there.

> git init sandbox && cd sandbox
> echo "data a" > a.txt && echo "data b" > b.txt
> git add -A && git commit -a -m "initial population"
[master (root-commit) d7eb6af] initial population
 2 files changed, 2 insertions(+)
 create mode 100644 a.txt
 create mode 100644 b.txt
> git branch branch1
> echo "more data a" >> a.txt && git commit -a -m "changed a.txt on master"
[master 11eb82a] changed a.txt on master
 1 file changed, 1 insertion(+)
> git branch branch2 && git checkout branch2
Switched to branch 'branch2'
> echo "more data b" >> b.txt && git commit -a -m "changed b.txt on branch2"
[branch2 25b38db] changed b.txt on branch2
 1 file changed, 1 insertion(+)
> git checkout branch1
Switched to branch 'branch1'
> git merge branch2
Updating d7eb6af..25b38db
Fast-forward
 a.txt | 1 +
 b.txt | 1 +
 2 files changed, 2 insertions(+)

Notice in the above, a.txt is updated in the merge, even though it was not touched/modified on branch2. In the above scenario I would expect git to be intelligent to recognize that a.txt was not changed on branch2 and therefore when applying updates to branch1, not make those changes.

Is there something I’m doing wrong? Yes, I could cherry pick and that would would for this simplistic example where I know what I changed, but is not realistic under real circumstances where the changes are much larger and you don’t know what might have been affected.

To be clear, I do not want this behavior from git.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T14:15:15+00:00Added an answer on June 17, 2026 at 2:15 pm

    ‘branch1’ and ‘branch2’ are nothing but commit pointers. They are states of the commit history at certain moments in time. As such, when merging ‘branch2’ into ‘branch1’, git does little more than establish a common ancestor and attempt to apply changes from both trees, together.

    Take a simple diagram:

     branch1       E <- branch2
        |         /
        v        /
    A - B - C - D <- master
    

    In the example above, ‘branch1’ points at commit B and ‘branch2’ points at commit E. This describes, more or less, the order of operations you entered above. Were you to merge ‘branch2’ into ‘branch1’, git would find a common ancestor in B then apply all the history that exists between B and E to ‘branch1’, specifically commits C, D, and E.

    What you want, however, is just E. One (bad) solution would be cherry-picking, as you’ve already identified. A much better solution is rebasing ‘branch2’ onto ‘branch1’, thereby rewriting ‘branch2’s history to include only commit E past ‘branch1’:

    git rebase --onto branch1 master branch2
    

    That results in exactly what you seek, and reads as ‘rebase branch2, which was originally based on master, onto branch1’. Note, I’ve left the ‘branch1’ pointer out of this diagram for simplicity, and E became E' because its commit hash changed (as is a common convention with these diagrams):

           E' <- branch2
          /
         /
    A - B - C - D <- master
    

    You could get a similar effect with git checkout branch2 && git rebase -i B, then remove commits C and D from the interactive rebase session.

    At my last job we routinely faced this problem with isolated feature branches. Cut at different moments in time from the same production branch, they would pull along unwanted changes if merged without rebasing. As an integration manager, I routinely rewrote their histories to a common point in the past (the last production release), thereby allowing clean merges all the way through. It’s one of many possible workflows. The best answer depends heavily on how your team moves code around. In a CI environment, for example, it’s sometimes less important that C and D get pulled along with merges like the one you describe.

    Finally, note that if E depends on any code in C or D, this solution will wreak havoc on your history when merging ‘branch1’ (now containing the E' change set) back into ‘master’. If your workflow is incremental, and ‘branch1’ and ‘branch2’ meddle in similar functions and files, merge conflicts will arise as a matter of course. In that case, a closer look at your team’s workflow is probably warranted.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
Does anyone know how can I replace this 2 symbol below from the string
i got an object with contents of html markup in it, for example: string
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I am trying to loop through a bunch of documents I have to put

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.