I connected 2 repositories.

git remote add origin https://github.com/мой_аккаунт/репозиторий_1.git -> здесь много файлов git remote add index https://github.com/мой_аккаунт/репозиторий_2.git -> тут пусто 

After that, I made the changes and decided to commit them to the first repository:

 git add index.php git commit -m "Comit" git push origin master 

All OK. Then I want to commit the same changes to the second repository:

 git push index master 

and all files were uploaded to the empty repository, not just index.php. Why?

  • The answer to the question "Why?" - because this is how Git works. - korytoff
  • one
    You forgot to write what you wanted to do, but now only a statement of the facts of the correct operation of Git - korytoff
  • Is it possible to do that only index.php would be flooded? that is, only one file - Diefair

3 answers 3

In a git it is possible to commit not only individual files, but even just certain lines.

To do this, go to the graphical interface ( git gui& ) and there in the window on the left - the current changes from above, the left - from the bottom - changes prepared for the commit. And you can remove-add files one by one by clicking on the file icon.

Also for lines: select "stage lines" in the main window, and in the menu, drop-down when you press the right mouse button, select "stage lines" (prepare for a commit) or "unstage lines" (remove from a commit)

Then click commit and only selected files or strings will get into it.

The push command applies all updated commits completely to the remote repository, here without options. That is, if one commit has changed with respect to the first repository, only it will be updated; if there are changes with respect to the second, for several commits, they will all be updated. The push command does not actually "upload files" but updates the remote repository to the local state, sending missing objects.

    As Mira correctly writes, in Git you can index (and, therefore, save to a commit) separate files and even strings (for binary ones, this will not work). But after that, Git only operates on commits entirely. You cannot upload a part of a commit to a remote repository - only the whole.

    Each commit contains more than one file — it contains a “snapshot” of the entire project workspace. That is, when you made a commit with index.php, it actually contains:

    • New index.php
    • All that was in the previous (parent) commit.

    The parent, respectively, contains changes that are stored in it, plus everything from its ancestor. And so on until the very first commit in the history of the project. That's why every file is uploaded to the repository every time.

    Read more here: How does git save the changed line at commit?

    If you need to upload only one file, then you can think about the implementation, but first you need a clear statement of the problem that you are trying to solve.

      Because the branch is the same.

      A branch consists of the "tops" and all its ancestors (the commit tree).
      push and pull load the entire tree until the commits recursively cease to have ancestors. That is, the state of the repository on the top of the branch will be accurately reproduced.

      If you want to separate a part of the files, store them in a separate branch (I told you yesterday how ) and make the merge only from this branch, and not in it, if necessary, make changes to these files. Then the commits that change these files will never be referenced to other project files and will not be in the branch.

      In branch b , the git merge a command will force branch b to reference branch a , but not vice versa . Branch a does not know anything about this merdzhe, she alone.

      Consider "utilities" and "framework" as different projects.