Hi
I have a question about GIT.
Suppose I have a project on MASTER branch with 100s of php files. To do a bug fixing in the project, i create a separate branch
>git checkout -b bugfix
Then after fixing the bug in 3 files (for eg index.php, register.php and login.php), i merge it in the master branch
>git checkout master
>git merge bugfix
The above code will merge all the 3 files i made changes, but is there anyway that i can force GIT to merge only 2 files, say login.php and register.php only?
Thanks
When you make the commit, you can use the
git add --patch
to specify only some portions of each file in the commit.
You also, of course, have the opportunity to specify which files are included in each-and-every commit.
In your case; “after the fact” I am not sure there is a simply (direct) way to handle this. Of course, you can always
- Checkout the bugfix branch
- ‘touch’ the files you want to [ultimately] merge into your master branch
- Create a new commit of only those files
Then you simply merge that latest commit into master.
Hi
Thans for the reply.
But GIT does not allow you to switch to another branch unless u commit all changed files. So in that case how do I merge without switching to another branch?
Thanks
You can use the stash
command to ‘put aside’ all your current work.
git stash
will bring you to a clean state.
After you are finished with your changes use
git pop
to restore your working files.
You can also simply commit what you have in a new branch. Branches are cheap and easy to use. I often make a branch called “WorkInProgress” to file stuff away while I jump elsewhere to manage some other parts of the project.
You could do it like this:
# On branch master
git merge --no-commit bugfix
This will merge the files, but will not commit them, allowing you to remove the one you don’t want (index.php in your example) like this:
git reset HEAD index.php
git checkout -- index.php
# index.php is now reverted to the master branch version
and then you can commit the changes as normal.