Search code examples
gitversion-controlgit-branchgit-pushgit-commit

How did my collaborators' commits find their way into my branch?


I am new to Git and still learning about it. I want to push my local branch commit to remote. I have

  1. created a local branch Task1004 and done some work,
  2. committed all the changes,
  3. run get fetch and git pull origin development
  4. run git rebase development.

Now I want to push my changes but, when I use git log, I see all other commits which have been pushed by other developers on branch development; for instance, one of my own commit and 3 commits by my collaborators.

When I run

git push origin Task1004

all 4 commits are pushed to Task1004, which will be later merged with development.

What is the reason of this and what should I do to push only my commits?


Solution

  • That's because you rebased Task1004 to development. If you rebase your Task1004 branch to development (or merge it with development), your collaborators' commits will become part of your branch's history; there's no way around that (see my detailed explanation below). If you don't want that to happen, simply do not rebase to or merge with development.

    On the other hand, why wouldn't you? Keeping up to date with a remote branch that's more stable than the one you're currently working on is good practice.


    Here is a reconstruction of what happened. I'll call the development branch "dev", for conciseness. Let's assume that, at the beginning, your repo's history looked like that:

    A [HEAD,dev,origin/dev]
    

    After creating and checking out the branch called Task1004, you got

    A [HEAD,Task1004,dev,origin/dev]
    

    After you did some work, staged the changes and committed, your repo's history looked as follows:

    A [dev,origin/dev]
     \ 
      B [HEAD,Task1004]
    

    You then pulled (fetch + merge) the dev branch, on which your collaborators had created a few more commits in the meantime. Your repo then looked something like that:

    A -- C -- D -- E [dev,origin/dev]
     \ 
      B [HEAD,Task1004]
    

    You then rebased Task1004 to dev, and ended up with

    A -- C -- D -- E [dev,origin/dev]
                    \ 
                     B' [HEAD,Task1004]
    

    So now, because of that git rebase, the commits of your collaborators (C, D, and E) became part of branch Task1004's ancestry. Therefore, it's normal that those commits show up in the log of that branch.

    To finish, you pushed your Task1004 branch to remote, and ended up with

    A -- C -- D -- E [dev,origin/dev]
                    \ 
                     B' [HEAD,Task1004,origin/Task1004]