简体   繁体   中英

git push origin new_local_branch pushes all files

I cloned a distant git repository on my local machine.

I created a branch with

git checkout -b new_local_branch

I did some changes.

Now I want to push my local branch to the remote repository with

git push origin new_local_branch

I am quite surprised that git is sending all files to the remote repository (not only the modified one) Is it normal? How can I avoid that?

Thanks in advance

You must have misunderstood something. Git push sends only the objects that are new. For example, have a look at this simplistic sequence where I update one file (README.txt) and push that to a new remote branch.

$ git checkout -b new_branch
Branch new_branch set up to track local branch master.
Switched to a new branch 'new_branch'

$ echo "New readme file content" > README.txt 

$ g commit -a -m "Commit only on new branch"
[new_branch 8d8f56d] Commit only on new branch
 1 file changed, 1 insertion(+), 3 deletions(-)

$ git log --oneline --decorate
8d8f56d (HEAD, new_branch) Commit only on new branch
d101a0e (origin/master, master) Small update
2d02c36 Initial commit

$ git push origin new_branch
Counting objects: 3, done.
Writing objects: 100% (3/3), 283 bytes | 0 bytes/s, done.
Total 3 (delta 0), reused 0 (delta 0)
To /tmp/myrepo.git/
 * [new branch]      new_branch -> new_branch

Git push sent only 3 objects. Those are:

  1. The new blob object containing the text: "New readme file content"
  2. The new tree object representing the repo root directory (which points to the new blob object)
  3. The new commit object 8d8f56d which we could see in the git log output

Note that the old commit objects 2d02c36, d101a0e and trees/blobs related to those commits were not sent, because the remote repo already had those commits. Note that if you had a deep directory structure inside your repo, even if you update only one file, you may need to send quite many new tree objects, one for each parent directory of the changed file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM