git commit master and branch

2 views
Skip to first unread message

L. V. Lammert

unread,
Sep 23, 2016, 6:01:39 PM9/23/16
to St. Louis Perl Mongers Google Group
Slightly OT, but I know a lot of git eggspert hang out here, ..

Can't seem to find any hints - is there a resonable way to commit the same
file to both master and a branch (or, I guess, multiple branches)?

Thanks!

Lee

Anthony Brummett

unread,
Sep 23, 2016, 7:17:12 PM9/23/16
to stl...@googlegroups.com

You could make the commit on one branch, switch to the other branch and cherry-pick that same commit to apply it to the other.  But, you'll likely get a conflict if you try to merge the branches later since they both make a change to the same lines.

If it's possible to make a third branch that starts before the second and make the change there, then that thirds branch could be merged to both the other branches and won't conflict later when they're merged.

-- Tony

L. V. Lammert

unread,
Sep 24, 2016, 12:37:02 PM9/24/16
to stl...@googlegroups.com
On Fri, 23 Sep 2016, Anthony Brummett wrote:

> If it's possible to make a third branch that starts before the second and
> make the change there, then that thirds branch could be merged to both the
> other branches and won't conflict later when they're merged.
>
How about amending an earlier commit to the 2nd branch? In this case, I am
just adding three files that need to be in master and the 2nd branch.

Thanks!

Lee

James E Keenan

unread,
Sep 24, 2016, 12:57:53 PM9/24/16
to stl...@googlegroups.com
At least in the strict sense of 'git commit --amend', you can only
"amend" the tip of a branch -- not any "earlier" commit.

I have to admit that I get uneasy when I hear someone say that they want
to add commits to multiple branches. It suggests to me that they aren't
following the principle that branches should be short-lived.

It sounds as if in this case you want your branch to be long-lived. Can
you paint a fuller picture of what you're trying to accomplish? That
might enable us to provide better suggestions.

Thank you very much.
Jim Keenan

L. V. Lammert

unread,
Sep 24, 2016, 1:30:07 PM9/24/16
to stl...@googlegroups.com
On Sat, 24 Sep 2016, James E Keenan wrote:

> I have to admit that I get uneasy when I hear someone say that they want
> to add commits to multiple branches. It suggests to me that they aren't
> following the principle that branches should be short-lived.
>
Even if that commit is checked out?

> It sounds as if in this case you want your branch to be long-lived. Can
> you paint a fuller picture of what you're trying to accomplish? That
> might enable us to provide better suggestions.
>
We abandoned work on the next version in favor of a total rewrite, .. anI
was going to commit some additional files to clean up.

Thanks!-

Lee

Scott Smith

unread,
Sep 24, 2016, 3:18:10 PM9/24/16
to stl...@googlegroups.com

Usually if I want to add something to multiple branches it looks the same as when I want to pull a piece of one branch onto another:

git checkout branch1
git add myfile
git commit -m 'add myfile'

git checkout branch2           # this changes branches
git checkout branch1 myfile # this doesn't change branches, it stages one file from another branch
git status                              # see how that file is staged
git commit -m 'add myfile'

You can replace myfile with a list of files or directories.

L. V. Lammert

unread,
Sep 24, 2016, 9:55:46 PM9/24/16
to stl...@googlegroups.com
On Sat, 24 Sep 2016, Scott Smith wrote:

> Usually if I want to add something to multiple branches it looks the same
> as when I want to pull a piece of one branch onto another:
>
> git checkout branch1
> git add myfile
> git commit -m 'add myfile'
>
> git checkout branch2 # this changes branches
> git checkout branch1 myfile # this doesn't change branches, it stages one
> file from another branch
> git status # see how that file is staged
> git commit -m 'add myfile'
>
> You can replace myfile with a list of files or directories.
>
Bingo - actually makes sense to me - thanks!!

Lee
Reply all
Reply to author
Forward
0 new messages