Let's say I have a large Git repository stored in a server so multiple people can work with it.
I want to be able to work with individual files from this repository, i.e. read, edit or insert new ones, without having to fetch all its contents, since it's a considerably big repository.
I know that I can get files from a specific branch/commit by using the
archive command and uncompressing the results, like this:
git archive --remote=git:<URL> <tree-ish> <file_name> | tar -x
What about the other way around? Is there a way I can edit, insert or delete new files in a given branch, or even push new branches to a remote Git repository without having to download all its contents?
Best How To :
To the best of my knowledge there is no way to do what you want. When you try to push some commit to a remote repository Git will detect whether your current branch is behind its remote counterpart and it that case it will prompt an error:
! [rejected] master -> master (non-fast-forward)
Consider as an example the following scenario:
A - B - C - D (remote master)
- E (local master)
If you try to push under this circumstances Git will stop you and suggest:
Updates were rejected because the tip of your current branch is behind its remote counterpart. Merge the remote changes (e.g. 'git pull') before pushing again.
That is, you must fetch the remote branch, merge the changes and then push the result of the merge. Git prevents you from pushing changes that potentially conflict with the remote branch and requires you to locally merge changes (and possibly resolve conflicts) before pushing.
As a side note you can push non-fast-forward changes in Git (that is, avoid fetching the remote) with
git push --force. Forcing a push should be avoided whenever possible as it may have some unpleasant consequences as making unreachable some commit that someone may have already pulled. In the previous scenario forcing a push will result in:
A - B - E (remote/local master)
As you can see this will cause some troubles to people that pulled the remote in the previous state. For a thorough explanation of the consequences of
git push --force see this stack overflow question.
What you can also do in Git is fetch a remote and check out only some files. You can achieve this with:
git checkout FETCH_HEAD -- <file-path>
This however does not solve your problem as it still fetches the remote and does not allow you to push without a merge.