414

I have two projects. One is the "official" project and the second is a light modification (some files added). I created new branch and I put new files to them. But in during development some files common to both branches is changed.

How do I commit only these files?

4
  • Are those two projects connect to the same git repository? Commented Aug 30, 2011 at 6:20
  • Yes, it is the same repository, but i don't want put my branch to server Commented Aug 30, 2011 at 6:25
  • So why don't you merge your new branch to master(or other official branch) Commented Aug 30, 2011 at 6:28
  • 1
    Answers to these questions shall help you: stackoverflow.com/questions/7161860/… stackoverflow.com/questions/7175869/… Commented Aug 30, 2011 at 12:32

9 Answers 9

400

I suppose you want to commit the changes to one branch and then make those changes visible in the other branch. In git you should have no changes on top of HEAD when changing branches.

You commit only the changed files by:

git commit [some files] 

Or if you are sure that you have a clean staging area you can

git add [some files] # add [some files] to staging area git add [some more files] # add [some more files] to staging area git commit # commit [some files] and [some more files] 

If you want to make that commit available on both branches you do

git stash # remove all changes from HEAD and save them somewhere else git checkout <other-project> # change branches git cherry-pick <commit-id> # pick a commit from ANY branch and apply it to the current git checkout <first-project> # change to the other branch git stash pop # restore all changes again 
Sign up to request clarification or add additional context in comments.

4 Comments

To literally commit only those files, even if other changes have been staged, the second example (git commit [some files], which implies the --only switch) should be used. The first example (git add [some files] followed by git commit) will also commit any other changes which had been staged.
I would be helpful to provide examples for git commit [some files]. like how should you replace [some files], comma, white space?
@claudiu usually shell stuff is space delimited. And if You dive deep enough You can change that To anything
If you can add some examples, this this answer will be great.
228

Get a list of files you want to commit

$ git status Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: file1 modified: file2 modified: file3 modified: file4 

Add the files to staging

$ git add file1 file2 

Check to see what you are committing

$ git status Changes to be committed: (use "git reset HEAD <file>..." to unstage) modified: file1 modified: file2 Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git checkout -- <file>..." to discard changes in working directory) modified: file3 modified: file4 

Commit the files with a commit message

$ git commit -m "Fixed files 1 and 2" -- file1 file2 

If you accidentally commit the wrong files

$ git reset --soft HEAD~1 

If you want to unstage the files and start over

$ git reset Unstaged changes after reset: M file1 M file2 M file3 M file4 

5 Comments

This is wrong. I did git add file1 file2 and then git commit -m "Fixed files 1 and 2" and it committed ALL the files.
@BinarWeb Because you had all those files staged (added). But it is worth mentioning in the answer to be absolutely clear. In your case what would've worked: git commit file1 file2 -m "Fixed files 1 and 2"
@UncaughtTypeError Don't get why this answer got so many upvotes while the question clearly says "common to both branches" which means it is already staged. As Binar Web mentioned this answer is wrong given the question asked.
I corrected this answer but not sure which moderator has not approved it and left the invalid answer as an accepted answer. As per @UncaughtTypeError the command to commit selected files is git commit file1 file2 -m "Fixed files 1 and 2"
This command just worked well: git commit -m "Fixed files 1 and 2" file1 file2
151

You can commit some updated files, like this:

git commit file1 file2 file5 -m "commit message" 

1 Comment

You can also specify directories and filename patterns in addition to files, git will commit all the staged files in the directories and the ones matching the patterns. Particularly handy if you have a large number of files to commit.
53

Suppose you made changes to multiple files, like:

  • File1
  • File2
  • File3
  • File4
  • File5

But you want to commit only changes of File1 and File3.

There are two ways for doing this:

1.Stage only these two files, using:

git add file1 file3 

then, commit

git commit -m "your message" 

then push,

git push 

2.Direct commit

git commit file1 file3 -m "your message" 

then push,

git push 

Actually first method is useful in case if we are modifying files regularly and staging them --> Large Projects, generally Live projects.
But if we are modifying files and not staging them then we can do direct commit --> Small projects

1 Comment

When not specifying the file name as you second examples suggests, git assumes the command flag for these arguments is --only. Then, it would be the same command as git commit --only file1 --only file3 -m "my message" or using a shortcut as git commit -o file1 -o file3 -m "my message" Reference: git-scm.com/docs/git-commit
44

Some of this seems "incomplete"

Groups of people are NOT going to know if they should use quotes etc..

Add 1 specific file showing the location paths as well

git add JobManager/Controllers/APIs/ProfileApiController.cs 

Commit (remember, commit is local only, it is not affecting any other system)

git commit -m "your message" 

Push to remote repo

git push (this is after the commit and this attempts to Merge INTO the remote location you have instructed it to merge into) 

Other answer(s) show the stash etc. which you sometimes will want to do

Comments

13

If you have already staged files, simply unstage them:

git reset HEAD [file-name-A.ext] [file-name-B.ext] 

Then add them bit by bit back in.

Comments

5

I think you may also use the command line :

git add -p 

This allows you to review all your uncommited files, one by one and choose if you want to commit them or not.

Then you have some options that will come up for each modification: I use the "y" for "yes I want to add this file" and the "n" for "no, I will commit this one later".

Stage this hunk [y,n,q,a,d,K,g,/,e,?]? 

As for the other options which are ( q,a,d,K,g,/,e,? ), I'm not sure what they do, but I guess the "?" might help you out if you need to go deeper into details.

The great thing about this is that you can then push your work, and create a new branch after and all the uncommited work will follow you on that new branch. Very useful if you have coded many different things and that you actually want to reorganise your work on github before pushing it.

Hope this helps, I have not seen it said previously (if it was mentionned, my bad)

1 Comment

This is a nice function, thanks for sharing! Indeed, "?" prints out help: y - stage this hunk n - do not stage this hunk q - quit; do not stage this hunk or any of the remaining ones a - stage this hunk and all later hunks in the file d - do not stage this hunk or any of the later hunks in the file s - split the current hunk into smaller hunks e - manually edit the current hunk ? - print help
1

This is a simple approach if you don't have much code changes:

1. git stash 2. git stash apply 3. remove the files/code you don't want to commit 4. commit the remaining files/code you do want 

Then if you want the code you removed (bits you didn't commit) in a separate commit or another branch, then while still on this branch do:

5. git stash apply 6. git stash 

With step 5 as you already applied the stash and committed the code you did want in step 4, the diff and untracked in the newly applied stash is just the code you removed in step 3 before you committed in step 4.

As such step 6 is a stash of the code you didn't [want to] commit, as you probably don't really want to lose those changes right? So the new stash from step 6 can now be committed to this or any other branch by doing git stash apply on the correct branch and committing.

Obviously this presumes you do the steps in one flow, if you stash at any other point in these steps you'll need to note the stash ref for each step above (rather than just basic stash and apply the most recent stash).

Comments

0

it is: git add full path of your file with file name

suppose I want to add a file MainActivity.kt then If i do git add MainActivity.kt it will throw error sayingfatal: pathspec 'MainActivity.kt did not match any files

so to avoid that:

git add app/src/main/java/com/androminor/firstcompose/MainActivity.kt 

I checked all of the answers. To my surprise they where half baked reply giving wrong output.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.