-
-
Save asabedia/f4616accc26a0666585a67b5070896dc to your computer and use it in GitHub Desktop.
Revisions
-
wizioo renamed this gist
Jan 28, 2016 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
wizioo created this gist
Jan 27, 2016 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ # How to have specific .gitignore for each git branch ## Objective My objective is to have some production files ignored on specific branches. Git doesn't allow to do it. ## Solution My solution is to make a general `.gitignore` file and add `.gitignore.branch_name` files for the branches I want to add specific file exclusion. I'll use post-checkout hook to copy those `.gitignore.branch_name in place` of `.git/info/exclude` each time I go to the branch with `git checkout branch_name`. ## Steps 1. Create new `.gitignore` files for each branch and name it like this : `.gitignore.branch_name` 2. In your git repo, go to `.git/hooks/` 3. Edit or create `post-checkout` file and copy the content found in this gist. 4. Don't forget to make it executable with `chmod 755 post-checkout` 5. Just go to the branch you want and type `git status`: TADAAA ! This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,28 @@ #!/bin/sh old_ref=$1 new_ref=$2 branch_switched=$3 if [[ $branch_switched != '1' ]] then exit 0 fi echo "---- POST CHECKOUT ----" current_branch=$(git rev-parse --abbrev-ref HEAD) hook_dir=$(dirname $0) root_dir="$(pwd -P)" info_dir="$root_dir/.git/info" exclude_target='.gitignore' if [[ -f "$root_dir/$exclude_target.$current_branch" ]] then echo "Prepare to use .gitignore.$current_branch as exclude file" exclude_target=.gitignore.$current_branch fi cd "$info_dir" rm exclude #ln -s $exclude_target exclude echo "Copy .gitignore.$current_branch file in place of exclude" cp "$root_dir/$exclude_target" exclude echo "--- POST CHECKOUT END ---"