ChatGPT解决这个技术问题 Extra ChatGPT

How do I commit only some files?

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?

Are those two projects connect to the same git repository?
Yes, it is the same repository, but i don't want put my branch to server
So why don't you merge your new branch to master(or other official branch)

w
wjandrea

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

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.
A
Abram

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"

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

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.
A
Adriano Rivolli

You can commit some updated files, like this:

git commit file1 file2 file5 -m "commit message"

T
Tom Stickel

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


K
KamalDeep

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


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
k
kaiser

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.


M
MilanDemoniack

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)


J
James

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).