Search code examples
gitgitignore

Keep Downstream Branch Updated - Keeps pulling unwanted files


I'm new to git and I'm going in circles. I'm not sure this is a normal use case, but I have lets say a branch called dev and then I have two downstream branches html and xml. Each has "whitelist" .gitignore. They are for cloning into server directories and I want only the files needed in the branch. (WebServer and Network Share for sideloading Office App)

I'm trying to make it so when I push to dev, the "downstream" branches html and xml pull/get pushed only the updates files whitelisted in there respective .gitignore files.

First off here is my setup so as not to avoid the X-Y question problem as I may be doing this incorrectly..

I have githooks setup that when I push to dev, it runs a script that goes into the gitrepo dir and then runs the following:

git checkout html
git pull #probably not needed as it should be updated, but no harm
git pull --no-edit origin dev
git add .
git commit -m "pulled from dev"
git push

Same for the xml.

Here is the HTML whitelist/.gitignore:

/*
!/Commands/
!/Content/
!/Images/
!/Functions/
!/Scripts/
!*.css
!*.html
!*.js
!.gitignore

!/ExcelWebAddIn1
/ExcelWebAddIn1/*

!/ExcelWebAddIn1/ExcelWebAddIn1Web/
/ExcelWebAddIn1/ExcelWebAddIn1Web/*

!ExcelWebAddIn1/ExcelWebAddIn1Web/Commands/
!ExcelWebAddIn1/ExcelWebAddIn1Web/Content/
!ExcelWebAddIn1/ExcelWebAddIn1Web/Images/
!ExcelWebAddIn1/ExcelWebAddIn1Web/Functions/
!ExcelWebAddIn1/ExcelWebAddIn1Web/Scripts/
!ExcelWebAddIn1/ExcelWebAddIn1Web/*.js
!ExcelWebAddIn1/ExcelWebAddIn1Web/*.html
!ExcelWebAddIn1/ExcelWebAddIn1Web/*.css

Here is the message I keep getting and the "extra files" pulled which were modified as part of my test, but shouldn't be in this branch at all. I initialized the branch without them and have used git rm --cached ExcelWebAddIn1/.. repeatedly and removed and re-cloned my working git directory many times.

...\git\vsdev>git pull --no-edit origin dev
remote: Enumerating objects: 17, done.
remote: Counting objects: 100% (17/17), done.
remote: Compressing objects: 100% (9/9), done.
remote: Total 9 (delta 6), reused 0 (delta 0), pack-reused 0
Unpacking objects: 100% (9/9), 724 bytes | 3.00 KiB/s, done.
From ssh://git.freesoftwareservers.com:10022/freesoftwareservers/vsdev
 * branch            dev        -> FETCH_HEAD
   47109fd..5fb82df  dev        -> origin/dev
CONFLICT (modify/delete): ExcelWebAddIn1/ExcelWebAddIn1/ExcelWebAddIn1Manifest/vsdev.localhost.xml deleted in HEAD and modified in 5fb82df36c84c4cc84536a60ecb243088a58d47b.  Version 5fb82df36c84c4cc84536a60ecb243088a58d47b of ExcelWebAddIn1/ExcelWebAddIn1/ExcelWebAddIn1Manifest/vsdev.localhost.xml left in tree.
CONFLICT (modify/delete): ExcelWebAddIn1/ExcelWebAddIn1/ExcelWebAddIn1Manifest/vsdev.prod.xml deleted in HEAD and modified in 5fb82df36c84c4cc84536a60ecb243088a58d47b.  Version 5fb82df36c84c4cc84536a60ecb243088a58d47b of ExcelWebAddIn1/ExcelWebAddIn1/ExcelWebAddIn1Manifest/vsdev.prod.xml left in tree.
Automatic merge failed; fix conflicts and then commit the result.

So in essence, what am I doing incorrectly? Is it a misconfiguration of .gitignore as I have been adjusting that thinking it was the issue. Or perhaps the pull command can be tailored?

As well, is there a way to run the pull/push commands without working on a local copy? IE: I'd like my githook script to just push dev to html and then cd to the webserver and run pull. Without having the need for another "clone" directory just sitting for the script to utilize.


Solution

  • Ok, so based on my suspicions and @toreks comments this is not a normal GIT usage. Add that to the fact I was new a git and it took me a while to figure out. Correct me if I'm wrong, but I think what threw me off was the idea that a file wasn't tracked if it wasn't commit in that branch, but I believe when I pull a file from master, even if its not committed in that branch its still considered "tracked".

    Anyway, these two lines helped me figure things out a lot:

    View files ignored:

    nano -c .gitignore ;  find . -not -path './.git/*' | git check-ignore -v --stdin
    

    View Files Tracked:

    git ls-files **/*
    

    And finally I realized the method I should use for this scenario is strictly checkout. I made .gitignore files for the html/xml repos, as I had already figured them out, but at this point, they should only ever be checking out the correct files, that is my "whitelist".

    Here is how I restarted and setup the two repos:

    git checkout dev
    git push origin --delete localhostxml
    git branch -d localhostxml
    git checkout -b localhostxml
    rm * -R
    

    cat <<'EOF'>.gitignore
    /*
    !.gitignore
    !update_branch.sh
    !/ExcelWebAddIn1
    /ExcelWebAddIn1/*
    !/ExcelWebAddIn1/ExcelWebAddIn1
    /ExcelWebAddIn1/ExcelWebAddIn1/*
    !ExcelWebAddIn1/ExcelWebAddIn1/ExcelWebAddIn1Manifest
    /ExcelWebAddIn1/ExcelWebAddIn1/ExcelWebAddIn1Manifest/*
    !ExcelWebAddIn1/ExcelWebAddIn1/ExcelWebAddIn1Manifes/*localhost.xml
    EOF
    

    cat <<'EOF'>update_branch.sh
    !#/bin/bash
    
    git checkout origin/dev -- ExcelWebAddIn1/ExcelWebAddIn1/ExcelWebAddIn1Manifest/*.localhost.xml
    git add . 
    git commit -m "pulled from dev" 
    git push --set-upstream origin localhostxml 
    git push origin localhost
    EOF
    chmod +x update_branch.sh
    ./update_branch.sh
    

    git checkout dev
    git push origin --delete html
    git branch -d html
    git checkout -b html
    rm * -R
    

    cat <<'EOF'>.gitignore
    /*
    !.gitignore
    !update_branch.sh
    !/ExcelWebAddIn1
    /ExcelWebAddIn1/*
    !/ExcelWebAddIn1/ExcelWebAddIn1Web
    /ExcelWebAddIn1/ExcelWebAddIn1Web/*
    !/ExcelWebAddIn1/ExcelWebAddIn1Web/*.html
    !/ExcelWebAddIn1/ExcelWebAddIn1Web/*.js
    !/ExcelWebAddIn1/ExcelWebAddIn1Web/*.css
    !/ExcelWebAddIn1/ExcelWebAddIn1Web/Content/
    !/ExcelWebAddIn1/ExcelWebAddIn1Web/Images/
    !/ExcelWebAddIn1/ExcelWebAddIn1Web/Functions/
    !/ExcelWebAddIn1/ExcelWebAddIn1Web/Scripts/
    
    EOF
    

    cat <<'EOF'>update_branch.sh
    !#/bin/bash
    
    git checkout origin/dev -- ExcelWebAddIn1/ExcelWebAddIn1Web/*.html
    git checkout origin/dev -- ExcelWebAddIn1/ExcelWebAddIn1Web/*.css
    git checkout origin/dev -- ExcelWebAddIn1/ExcelWebAddIn1Web/*.js
    git checkout origin/dev -- ExcelWebAddIn1/ExcelWebAddIn1Web/Content/
    git checkout origin/dev -- ExcelWebAddIn1/ExcelWebAddIn1Web/Images/
    git checkout origin/dev -- ExcelWebAddIn1/ExcelWebAddIn1Web/Functions/
    git checkout origin/dev -- ExcelWebAddIn1/ExcelWebAddIn1Web/Scripts/
    
    git add . 
    git commit -m "pulled from dev" 
    git push --set-upstream origin html
    git push origin html
    EOF
    chmod +x update_branch.sh
    ./update_branch.sh
    

    Finally my hook script:

    #!/bin/bash
    
    home=/opt/git/local/vsdev/
    cd $home
    git pull
    
    git checkout html
    ./update_branch.sh
    
    git checkout localhostxml
    ./update_branch.sh
    
    git checkout prodxml
    ./update_branch.sh
    
    html=/opt/odev/html/vsdev/
    cd $html
    git checkout html
    git pull origin html
    
    xml=/mnt/zfs/raid-z/odev/vsdev/
    cd $xml
    git pull
    
    cd $home
    
    touch /tmp/hookin