git checkout staging # go to staging branch git checkout dev . # this checkout dev file changes into staging git reset HEAD build # this remove added file in build folder git clean -f # this drops untracked files we just reseted git checkout -- .
You can create a . gitignore file in your repository's root directory to tell Git which files and directories to ignore when you make a commit. To share the ignore rules with other users who clone the repository, commit the . gitignore file in to your repository.
If you want to ignore a file that you've committed in the past, you'll need to delete the file from your repository and then add a . gitignore rule for it. Using the --cached option with git rm means that the file will be deleted from your repository, but will remain in your working directory as an ignored file.
I got over this issue by using git merge command with the --no-commit
option and then explicitly removed the staged file and ignore the changes to the file.
E.g.: say I want to ignore any changes to myfile.txt
I proceed as follows:
git merge --no-ff --no-commit <merge-branch>
git reset HEAD myfile.txt
git checkout -- myfile.txt
git commit -m "merged <merge-branch>"
You can put statements 2 & 3 in a for loop, if you have a list of files to skip.
I ended up finding git attributes
. Trying it. Working so far. Did not check all scenarios yet. But it should be the solution.
Merge Strategies - Git attributes
.gitattributes
- is a root-level file of your repository that defines the attributes for a subdirectory or subset of files.
You can specify the attribute to tell Git to use different merge strategies for a specific file. Here, we want to preserve the existing config.xml
for our branch.
We need to set the merge=foo
to config.xml
in .gitattributes
file.
merge=foo
tell git to use our(current branch) file, if a merge conflict occurs.
Add a .gitattributes
file at the root level of the repository
You can set up an attribute for confix.xml in the .gitattributes
file
<pattern> merge=foo
Let's take an example for config.xml
config.xml merge=foo
And then define a dummy foo
merge strategy with:
$ git config --global merge.foo.driver true
If you merge the stag
form dev
branch, instead of having the merge conflicts with the config.xml
file, the stag branch's config.xml preserves at whatever version you originally had.
for more reference: merge_strategies
You could start by using git merge --no-commit
, and then edit the merge however you like i.e. by unstaging config.xml
or any other file, then commit. I suspect you'd want to automate it further after that using hooks, but I think it'd be worth going through manually at least once.
Example:
master
, develop
develop
branch and want to ignore it while mergingCode:
git config --global merge.ours.driver true
git checkout master
echo "path/file_to_ignore merge=ours" >> .gitattributes
git merge develop
You can also ignore files with same extension
for example all files with .txt
extension:
echo "*.txt merge=ours" >> .gitattributes
You could use .gitignore
to keep the config.xml
out of the repository, and then use a post commit hook to upload the appropriate config.xml
file to the server.
As commented by many it should be noted that the accepted answer and the copies of it (using a custom merge strategy for a specific file or files) only works in cases where there would be actual merge conflicts.
2 simple cases that would not result in merge conflicts and therefore commits getting merged:
config.xml
file in a feature branch and not in master. You want to exclude it during mergeconfig.xml
file in both branches are based off the same commit. New code is added to the file in feature.Either use merge --no-commit
as demonstrated by unmesh-gurjar or simply cherry-pick invidiual commits into master. The latter being rather cumbersome of course and coming with a couple of other pitfalls (new SHA1 IDs etc.)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With