Reputation: 49
Every time when I try push single file to new branch, git all files from folder.
When I delete all files from repo using git rm --cached -r .
and upload this single file one more time it's fine. But when i try upload some files to another new branch, it's push new files + files from last push.
I used this comands:
git init
git remote add origin master
git branch test
git checkout test
git add <file>
git commit -m "blahblah"
git push origin test
Of course in the meantime I used git status
command to check current files, but it's show only <file> that I want. I don't know why it's push all files, not only <file>.
Upvotes: 0
Views: 6660
Reputation: 488203
Git does not push files. Git pushes whole commits.
A commit is a snapshot of the entire source tree: all the files. That's what git push
transfers: one or more complete snapshots.
When you use git checkout branch
, Git translates the branch
name to a specific commit, and then extracts that entire commit—all of its files—into your work-tree. This is kind of the point of commits: they represent a set of files that all go together. Use file-set-A to do thing A on branch A; use different file-set-B to do thing B on branch B; and so on. If there is a bug in the set-of-files that is the current tip of some branch B, you extract the complete set, fix the bug, add the fixes, and run git commit
to say: Now branch B means this new commit I just made—using all of these files. The new commit represents the new snapshot that does thing B.
Hence, git push
pushes all the new snapshots that you made, that the other Git does not have already. They add the new snapshots to their collection of "all snapshots ever made".
Note that the opposite of push
is fetch
, not pull
! This wrong-verb thing is due to a small mistake Git made early on, that we all have to live with now. Using git pull
means run git fetch
, then run git merge
. That's an extra step that git push
does not (and cannot) do. We fetch, then merge, then push; then we fetch, merge, and push again as necessary. The pull
verb combines the two, but it turns out there's often something to do between those two actions, or instead of using merge
. When there isn't—when it's OK to fetch-and-immediately-merge—then git pull
is more convenient. But I advise learning them separately at first.
For much more about this, once you're ready, see What does GIT PUSH do exactly?
Upvotes: 1