11

I am little bit new to Git. I would like to push some files to my Git origin (remote).

What I did:

I had my master, and I created a branch to do some job. After that I merged my branch to my master. During my work, a lot of binary files and project files were changed/added locally. I would like only to add .java files which changed to remote server.

(I believe that I experimented with commits when I worked on my branch, just to check how it work.)

My master is up to date with my origin (that is what I get when I do git pull. Also I did git fetch origin. I always received (when I ran git status):

On branch master Your branch is ahead of origin/master by 12 commits.
  (use "git push" to publish your local commits)

nothing to commit, working directory clean`

I tried to add, commit those files, but running git status wasn't changed. I tried to do add, commit on the new branch:

On branch NewBranch nothing to commit, working directory clean

I tried to reset Head. I didn't find a solution for my problem in the Git tutorial or on Stack Overflow.

Of course I can push all files to remote origin, but I don't think it's a good solution.

Some duplicate questions that I found: How to push a single file, how to push changes made to only certain files?, and How to commit only some files?.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Mike.R
  • 2,824
  • 2
  • 26
  • 34
  • 1
    As the first duplicate you linked to says, you can't push just certain files. What you're pushing are commits, which you can think of as snapshots of your files at a point in time. Without doing some special operations (which you should avoid), you'll have to push all or nothing. – Dave Zych Sep 05 '14 at 15:33
  • What are the binary files that you don't want to commit? The compiled bins? They should be [`.gitignored`](http://git-scm.com/docs/gitignore) – Dave Zych Sep 05 '14 at 15:34
  • not only binary some project files which I changed and I don't want to push.(I add all of them to .gitignore but when I run git diff --stat origin/master I see all of them) so I believe I will also push all of them. – Mike.R Sep 05 '14 at 15:38
  • 2
    You have to `git rm` them from the repository then ignore them. – Dave Zych Sep 05 '14 at 15:39
  • When I used git for windows I could choose which files to push before synchronize so I thought there must be a simple way. Just curious if people commit the whole project to git so how to they work all together? (if everyone change project files every push) – Mike.R Sep 05 '14 at 15:41
  • I thought about git rm but I can not run git status after it... which can be very annoying to understand what going on.... – Mike.R Sep 05 '14 at 15:42

2 Answers2

18
  1. Create a new branch from master
  2. git checkout master
  3. git checkout -b new_branch
  4. Checkout just the file you want from your old branch
  5. git checkout old_branch path/to/some/file
  6. repeat as necessary for additional files
  7. Commit the files to your new branch
  8. git commit -a
  9. Push new branch to origin master branch
  10. git push origin new_branch:master
Mapad
  • 8,407
  • 5
  • 41
  • 40
linuxdan
  • 4,476
  • 4
  • 30
  • 41
  • Tnx linuxdan after step 3. git status will return :On branch NewBranch nothing to commit, working directory clean. so if I push i push everything – Mike.R Sep 05 '14 at 16:47
  • What does your status look like after step 2? – linuxdan Sep 05 '14 at 17:11
  • 3
    Shouldn't the last step be `git push origin new_branch:master`? The way it currently is, it's just pushing the local master branch to the remote. – lapropriu Feb 02 '16 at 09:19
  • This is the correct answer because the other files are not deleted. – yes4me Jan 06 '17 at 02:33
6

I solved my problem: (I was confused with git status response because it wasn't changed when I tried to add/commit files which were already there).Thanks to linuxdan and DaveZych and Temich.

git checkout -b NewBranch

After that I deleted all unnecessary files.

git rm --cache build/web/WEB-INF/classes/doggizz/utils/\*.class

Move back to master

git checkout master

(only disadvantage that those files were deleted when I moved back to master so I copied manually project file , I tried to stash before checkout but it didn't helped)

git merge NewBranch
git push
Mike.R
  • 2,824
  • 2
  • 26
  • 34