50

I have changed a single file in a subdirectory of my repository and I want to push just that file to Github.

I've made a small change to one file, and I don't want to re-upload the entire repository.

It seems like all of the instructions that I've seen so far require me to merge locally with the master and then push my local master to the remote origin.

How can I push just that one file?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
moon star
  • 511
  • 1
  • 4
  • 6
  • 2
    Just add that one file to staging and commit. The only change that will be pushed is your file. – chwi Sep 28 '14 at 10:50

7 Answers7

45

When you do a push, git only takes the changes that you have committed.

Remember when you do a git status it shows you the files you changed since the last push?

Once you commit those changes and do a push they are the only files that get pushed so you don't have to worry about thinking that the entire master gets pushed because in reality it does not.

How to push a single file:

git commit yourfile.js
git status
git push origin master
Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
krilovich
  • 3,475
  • 1
  • 23
  • 33
23

Very simple. Just follow these procedure:
1. git status
2. git add {File_Name} //the file name you haven been changed
3. git status
4. git commit -m '{your_message}'
5. git push origin master

Salman Srabon
  • 247
  • 2
  • 3
13

Let me start by saying that the way git works is you are not pushing/fetching files; well, at least not directly.

You are pushing/fetching refs, that point to commits. Then a commit in git is a reference to a tree of objects (where files are represented as objects, among other objects).

So, when you are pushing a commit, what git does it pushes a set of references like in this picture:

git commits

If you didn't push your master branch yet, the whole history of the branch will get pushed.

So, in your example, when you commit and push your file, the whole master branch will be pushed, if it was not pushed before.

To do what you asked for, you need to create a clean branch with no history, like in this answer.

Community
  • 1
  • 1
Adam Adamaszek
  • 3,914
  • 1
  • 19
  • 24
  • 1
    Just one point of clarification - you don't really push commits, either. What you push are refs (branches and tags, primarily) that happen to point to commits. But +1 for the spiffy graphics and the detailed description. – twalberg Nov 20 '12 at 19:40
  • @AdamAdamaszek where is this diagram from? – Wakan Tanka Nov 21 '16 at 12:03
  • 1
    AFAIR it comes from the 'Pro git' book. – Adam Adamaszek Nov 23 '16 at 13:31
  • Please highlight the link to `--orphan` branch solution as it is a conforming answer if you don't want your other files to appear in history. Unfortunately the way I put my question into google in the first place didn't lead me to that link. – simno Apr 22 '20 at 14:14
8

If you commit one file and push your revision, it will not transfer the whole repository, it will push changes.

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
  • The reason I thought it was pushing the whole repository is that it's taking a long time to upload and I'm getting this message: "Counting objects: 26, done. Writing objects: 19% (5/26), 12.41 MiB | 119 KiB/s" The files I changed are just little text files, much less than a megabyte in size. – moon star Nov 22 '12 at 03:37
  • Well, I can't tell you what are your changes, but suppose for instance you have directory with 100,000 files and you change one. Then the changes are: your file contents, directory, containing the list of 100,000 files (not their contents), directory containing the directory, etc. and finally commit metadata. And you may, of course, have more than one commit. Or you may have some other problem and push something completely different. – Michael Krelin - hacker Nov 22 '12 at 06:52
6

Push only single file

git commit -m "Message goes here" filename

Push only two files.

git commit -m "Message goes here" file1 file2
itro
  • 7,006
  • 27
  • 78
  • 121
2

It will only push the new commits. It won't push the whole "master" branch. That is part of the benefit of working with a Distributed Version Control System. Git figures out what is actually needed and only pushes those pieces. If the branch you are on has been changed and pushed by someone else you'll need to pull first. Then push your commits.

Andrew T Finnell
  • 13,417
  • 3
  • 33
  • 49
2

git status #then file which you need to push git add example.FileExtension

git commit "message is example"

git push -u origin(or whatever name you used) master(or name of some branch where you want to push it)