How do I commit only some files?
Solution 1:
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
Solution 2:
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
Solution 3:
You can commit some updated files, like this:
git commit file1 file2 file5 -m "commit message"
Solution 4:
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
Solution 5:
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