Reputation: 89
I've two branches in one repo : branch_1 and branch_2.
somebody added a file in branch_1 let's say first_file.txt. To get this file in branch_2 - I'll simply raise a pull request and once the PR is merged I'll have the file in branch_2.
somebody added a file in branch_1 let's say first_file.txt. Now to get this file, I do below on terminal :
(on branch branch_1) $ git pull
(on branch branch_1) $ git checkout branch_2
(on branch branch_2) $ git checkout branch_1 first_file.txt
and then performs git add,commit and push.
In both above scenarios I get the file in my branch (branch_2). but the issue is when I see git log (or history on git portal) they are completely different.
For Scenario 1 : it shows that file came from somebody.
For Scenario 2 : it shows that file is checked-in by me.
Is there a solution to copy files from one branch to another using command line, keeping history safe?
Thanks
Upvotes: 2
Views: 867
Reputation: 488183
The only history in a Git repository is the set of commits in the repository.
Each commit contains a complete snapshot of every file—or rather, of every file that is in that commit. That is, suppose we start with a simple repository with one commit containing one file:
A <-- master (HEAD)
(A
stands in for some raw hash ID). Commit A
contains a README.md
file, and nothing else.
Now you make a new commit in which you add a new file, such as main.py
:
A--B <-- master (HEAD)
New commit B
contains two files: README.md
—which is the same as the copy in commit A
—and main.py
.
The history in this repository is that commit B
leads back to commit A
. That's the only history there is.
In your case, you have a more complex starting setup. Simplified, it probably looks about like this:
I--J <-- branch_1
/
...--G--H
\
K <-- branch_2
Commit H
is the first commit where the two branches join, in terms of history: history from J
backwards goes J
, I
, H
, G
, and so on. History from K
goes K
, H
, G
, and so on.
In commits up through I
, and your commits up through K
, there is no file named first_file.txt
, but in commit J
, someone else added this new file. Therefore commit J
has all the files that I
has, plus this one new file. Viewing the history from J
backwards, the difference between I
and J
is that the file is added.
If you now make a new commit L
that comes after K
that has the file in it, the difference between commit K
and your new commit L
is that this file is added. So the history as viewed from L
backwards shows the file added.
That's simply how Git works. There is no file history, there is only commit history. You can ask Git to produce a subset of commit history that traces which commits had activity in specific files—but since the commit history will be that the file newly appears in L
as compared to K
, that is what you will see.
Upvotes: 1