Git pull failed to my web server - python

Locally (Cloud9), I have commited my changes to my repo (https://github.com/edward408/my-first-blog), however when i perform a git pull on my deployment server (PythonAnywhere) i get the following error:
Updating aef1181..5d68bfa
error: Your local changes to the following files would be overwritten by merge:
db.sqlite3
Please, commit your changes or stash them before you can merge.
Aborting
I have pushed the changes from my local console (Cloud9) and made sure by veryfing git status afterwards. It seems that the best path long-term is to updatede git on PythonAnywhere while updating it from my local git at the same time. However, how would I implement that without starting a new repo again? Id prefer not to change anything that I already have on PythonAnywhere.
Anywho, what would the best feasible solution at this point in time?
EDITED: Its not completely the same as the suggested link. I already have my local dev environment pushing updates to the project repo. To use git stash on PythonAnywhere, i would have to push from there as well. Unless this means executing stash locally?

All that means is db.sqlite3 has been modified on your production server. Because of this, git isn't sure whether it is ok to overwrite it with your new code (because that file is present in your repo). Really, you should just add that file to your .gitignore, but you can get around it with (on your production server) running:
git stash
# do your pull
git stash pop
Basically, stash "saves" modified files that haven't been committed and allows you to later restore it by popping it off again, after the merge has overwritten it. https://git-scm.com/book/en/v1/Git-Tools-Stashing

Related

Deploying Flask application to Heroku gives an ImportError from psycopg2

Recently I'm learning Flask by the book "Flask Web Development". When I completed the code and deployed it to Heroku, the following error happened:
ImportError: /app/.heroku/python/lib/python3.6/
sitepackages/psycopg2/.libs/libresolv-2-c4c53def.5.so:
symbol __res_maybe_init version GLIBC_PRIVATE not defined
in file libc.so.6 with link time reference
However, this works fine locally. I have searched relevant questions about psycopg2, and I have adjusted the version of psycopg2 but the same error still happens. Please, how can I solve the problem?
I got the same problem. I solved it by forcing heroku to clean up the python virtual environment, and reinstall the requirements.txt file using psycopg2>=2.7,<2.8 --no-binary psycopg2. I must admit it felt a little like magic, but for what its worth here are the steps I took:
Using Git Bash for Windows, I logged in to heroku with heroku login.
Next, I navigated to the root folder of the git/project folder I was working on.
There I made sure that my requirements.txt was updated by running venv/Scripts/pip freeze > requirements.txt. You will probably have to find your own python/virtual environment path.
Next I changed the line/entry for psycopg2 to psycopg2>=2.7,<2.8 --no-binary psycopg2.
Then saved all the content of the requirements.txt file to my desktop, making sure the one in my project folder was empty. Not sure if this step was necessary. But that's what I did, so I'm just gonna mention it here.
Now in the Git Bash terminal I ran heroku apps:info -a blogglistene to find the version of the my heroku stack which for me was heroku-18.
Next I look online for the supported versions of python for heroku-18. It said the default was python-3.6.6 (which I then assumed is the one I was running), and some others.
I then picked python-2.7.15 arbitrarily from the list, and added that text to a runtime.txt file which I placed in the root of my project folder.
Next I committed and pushed this to my heroko git repo, and it was deployed. This will temporarily break the build. From what I understand, the magic behind this is that heroku now fully destroys the python interpreted and environment, so no lingering cached files or anything. Clean slate.
Then I reverted the content of runtime.txt to the default of python-3.6.6 and added back the content of the requirements.txt file, making sure the changes I made are there.
Finally, I committed and pushed this, and my application was back up and running.
I had the exact same problem when I was following "Flask Web Development". I spent a day plus trying to solve it and eventually succeeded. It's not as complicated as André C. Andersen's method.
Make sure you are on master branch. Not other branches.
The book is second edition of Flask Web Development by Miguel Grinberg. Chapter 17. Deployment. When you work all the way till Deploying with git push, after git push heroku master to upload the application to the heroku remote. The application is now deployed and running, but it is not going to work correctly because the deploy command that initializes the database tables has not been executed yet. You need to use
heroku run flask deploy
to create database on heroku. That's where the problem appears.
This is because at this point your git repo HEAD is at 17c. It's not the master branch yet. If you commit any change now, it won't change heroku environment. Meaning when you do
git branch
You will see:
(HEAD detached from 17c)
master
But this is not what you want. Not head attached to 17c. You want to see only master, which is achieved with
git checkout master
Then git branch will show only master branch. Therefore you can make adjustments on the project and heroku will update. Doing this because you will eventually git push to master branch. I think you can git push to other branches in order for heroku to work, but I don't know how to do that.
Change psycopg2==2.7.3 to the newest version, e.g. psycopg2==2.8.4. Another workaround is to change it to psycopg2-binary==2.8.3.
psycopg2 2.7.3 will give you GLIBC_PRIVATE error. (You can check it by running heroku run bash, then starting python and importing psycopg2. If heroku dependency is 2.7.3, then you will see the same error.) The official website notes that version 2.7.3.1 drops libresolv which causes the issue, therefore using this version or later should do the trick.
Now, change your requirements/heroku.txt (heroku environment dependency) line psycopg2==2.7.3 to psycopg2--2.8.4 or psycopg2-binary==2.8.3. (For some reason binary version of psycopg2 will work without issue. Don't know why, but I saw A LOT of discussion on stackover.) Then,
git add .
git commit -m "notes"
As usual.
git push heroku master
Push to heroku master branch. If you do not do step 1, then git will show Everything up-to-date. Therefore, it won't make changes because you are committing not to master. Now, you can see git is downloading the appropriate package.
You can double check if the packages are correct by opening heroku bash then pip list. You will see psycopg2 (and psycopg2-binary if you chose the workaround). Now, when you start Python, you can import psycopg2 with no problem.
Finally. heroku run flask deploy. Voila!
I was stuck on step 1, since whatever change I made with the project, heroku just didn't update.

How to test pre/post commit scripts in git?

I've created a git post-commit hook in python and made a file executable and placed it into .git/post-commit.
It works when I run it via Python. Now I want test it locally in git. How can I do that? I can't make a commit to git locally from my computer in an easy way, right?
Should I copy the hook file to my server and make test commits to test it? Is there another way?
I can't make a commit to git locally from my computer in an easy way, right?
You need to differentiate between pushing a branch to a server, which may require some configuration, and simply creating a commit, which can be as easy as running a command:
$ git commit --allow-empty -m "Testing my hook"
pre-commit and post-commit hooks are run locally on commit (see https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks ), not on push (push is what transfers your local commits to the server).
To test a hook I would create a test branch, and make several test commits there with whatever content you want. After the hook is ready, you can delete that branch. Until you do a push, everything is local.
Also if you output something from the hook script, that will be visible in the terminal when you do a commit. This is useful for debugging.
For executing the code on the server you should use "Server-Side Hooks" (see at the bottom of this page, also documented here).

How to check if a git repo was updated without using a git command

TL;DR
I would like to be able to check if a git repo (located on a shared network) was updated without using a git command. I was thinking checking one of the files located in the .git folder to do so, but I can't find the best file to check. Anyone have a suggestion on how to achieve this?
Why:
The reason why I need to do this is because I have many git repos located on a shared drive. From a python application I built, I synchronize the content of some of these git repo on a local drive on a lot of workstation and render nodes.
I don't want to use git because the git server is not powerful enough to support the amount of requests of all the computers in the studio would need to perform constantly.
This is why I ended up with the solution of putting the repos on the network server and syncing the repo content on a local cache on each computer using rsync
That works fine, but the as time goes by, the repos are getting larger and the rsync is taking too much time to perform. So I would like to be have to (ideally) check one file that would tell me if the local copy is out of sync with the network copy and perform the rsync only when they are out of sync.
Thanks
Check the .git/FETCH_HEAD for the time stamp and the content.
Every time you fetch content its updating the content and the modification time of the file.

How to use a pull request in my local copy of a Git branch?

I'm not familiar with Git, I only know how to download a master or dev branch of a python software project from Github and install into my system (I do not really use Git locally).
Now there is a "pull request" by a GitHub user which contains a feature I find useful but not in the main or dev branch. How can I use that in my local copy?
And if I manage to include it into my local copy, would it be lost if I later updated to the latest maaster/dev branch?
see: Git fetch remote branch
git fetch
git checkout <the name of the remote branch>
You can revert to any commit anytime you want, those changes are kept in the repository so you can visit different versions of it during the development on all branches. You need to do a merge command, which if, there's not any conflict with the patch contained in the pull request, will combine the code of the patch with the code of the repository. If there's conflicting code, like shared files for example, you will have to do the proper refactors and select which changes get inside or not.
Here you have basic information about merging different branches:
http://git-scm.com/book/en/v2/Git-Branching-Basic-Branching-and-Merging
Updating will only do a merge with the changes and your master. If you break something you can stash the changes and get to the original state.
Also I would suggest to checkout a fresh branch and apply that feature on the freshly created branch. If every thing works fine merge it to your master otherwise delete and forget it :P
if you dont know the branch name
use git fetch first to know the branch name
and then git checkout
Also maybe you can find this link useful

crontab automated git pull

I have a server deployed. I am writing a crontab task. It uses a python script that need to check if there is new push into the repository. If it finds a new push, it will pull and update the server code and should restart the server.
My problem is how to make the python script know if there is new commit into the repository?
I know you can use
git rev-list deployment..origin/deployment
to check if there is any commit is available on the remote server.
But how to implement in the python script and make it decide to know that it need to pull?
Thanks
Nick
You will need to contact the server in any case, so you might as well pull.
If you are on the current branch when you pull, and want to detect whether the current branch has changed, you can always do (note: written in shell)
BEFORE=$(git rev-parse HEAD)
# git pull here
AFTER=$(git rev-parse HEAD)
# Changes if $BEFORE is different from $AFTER
fge is correct. You cannot see if someone else pushed without first either doing a git pull origin or a git fetch origin. Your command would have worked with a git fetch origin first.
git fetch origin
git rev-list deployment..origin/deployment

Categories

Resources