Howto pull latest revision of a git repository on system startup - linux

I'm trying to update a git repository on system startup.
I called a Script from /etc/rc.local which is executed.
In that script I do the following steps:
1) Enter the folder where repo was cloned to
2) Do a git pull in that folder in two different ways
1st way simple git pull > /home/user/result.txt (doesn't work)
2nd way git --git-dir=/home/pi/gitrepo/.git pull origin master > /home/user/result.txt
What do I need to do to get this working?
Also tried to test if internet connection is already available with a simple wget which was successful
Thanks in advance

I expect this to be a problem with public key authentication.Note that commands during startup are executed as root, but you've likely registered a regular user's key at github.
Solution: register a machine key at github and use that key for the pull, like described here

Related

GIT Pull using Ubuntu 16.04.2 LTS

I want to be able to pull down my source code from GitHub automatically, but currently doing it manual using the process below.
Currently i have to do this by going
$ sudo -i
Then I CD to my directory, once in I run the following command
$ git pull origin master
the command then asks for me to enter my key password
Enter passphrase for key '/root/.ssh/id_rsa':
This then pulls the latest code down from GitHub.
A beginning note: does your repo really need to be pulled as root? Why? It's probably not a good idea to be doing that.
Github supplies https pull links that anyone can use to pull without needing a key. So, we can add another remote, used specifically for this purpose, that pulls using the https link.
git remote add autopull https://github.com/<user>/<repo>.git
Now you can change your pull command to:
git pull autopull master
From there, you can put it in .profile, .bash_profile, .bashrc, or maybe even a cron script. You haven't specified how you want to automate it, though, so I can't give any specific examples.
It should be noted that this can only work for public repositories. If this is a private repository, you should create another keypair that has no passphrase required. If it isn't your default keypair you can make use of a trick.
ssh-agent bash -c 'ssh-add /path/to/yourkey; git pull autopull master'
Or another answer to the same question:
GIT_SSH_COMMAND='ssh -i /path/to/yourkey' git pull autopull master

Execute a script after every git push

There is a server running and has a git instance on it. I want a script to run everytime a user does git push to the server. I want my script to be executed then git push to continue.
Any work arounds?
You've tagged this GitHub so I'm assuming that you are referring to public GitHub and not GitHub enterprise.
You cannot run a script "server-side" on GitHub's servers because that would obviously be a massive vulnerability but you can set up a web hook to trigger a script on another server.
Basically whenever someone does a push, a specific URL will be sent data about the push. You can then trigger a script from this. For more information on web hooks, see the GitHub API docs.
I am not sure If you want a scipt to run prior to push or after. So here is my answer for pre-push. But if you want post-push (i.e after push) you have to change the pre-push hooks accordingly to check if pushed successfully and then you can do post push thing.
As suggested by #Travis, git hooks is the one that you are looking for. So to execute a script pre-push, you have to do is to create a pre-push file in the .git/hooks. So in this case put your bunch of code in the pre-post script file .git/hooks/pre-push and save it. Then make it executable by chmod +x .git/hooks/pre-push. After you done with this successfully you will be able to see the script gets executed each time you do run push command.
PS: Please note that I haven't tested this whole but expected to work in this way.
In Short, assuming you(Linux user) are in the project directory
vim .git/hooks/pre-push # then add your code and save the file
# Also put the shebang on top to identify the interpreter
chmod +x .git/hooks/pre-push # make it executable
You should look into git hooks:
8.3 Customizing Git - Git Hooks
and, another site regarding this technology:
githooks.com

Automatically Sync Local Github Repository with Directory on Server

I have been attempting to use hooks to automatically pull the updated repo from github to my remote web server. This is the code on the post-receive hook:
cd /home/[my username]/document_root/[github repo name]/
git pull origin master
I expect this to run when there is a new commit from my development machine that syncs with the one on github, but it doesn't update the files inside of that repo directory. Any help is appreciated, but I am new to using git and github in general.
EDIT: I tried to follow this tutorial but it still doesn't work.
http://ryanflorence.com/deploying-websites-with-a-tiny-git-hook/
You can push git post-hook to Github. Instead, you can use Github's Webhooks
you can customize a schedule with crontab.
for example, crontab -e , and input
5 * * * snyc_git.sh
Then it will snyc your respository.

List what's inside a remote git repository

I know there is git branch -a that lists the branches in the repository but how do I see the files inside this branch when the repository is remote (can't access it from the github website).
You can also use git ls-files.
Short of using the GitHub API or another hack, it's not really possible to do this without pulling down the repository first. However, you can if you do pull from the remote.
In order to fetch all branches from remote, do git fetch --all. Then you can view all your branches via git branch and checkout to them via git checkout -b [branch]. Then you can simply view the files as you please from your remote using standard command line using ls, cd, cat, etc. If you're really rebelious you can try Midnight Commander.
I recognise it might be slow pulling down large repositories over limited bandwidth (try pulling it through a VPS you SSH into?), but unfortunately there isn't much choice.

Git - Syncing a Github repo with a local one?

First off, forgive me if this is a duplicate question. I don't know anything but the basic terminology, and it's difficult to find an answer just using laymen's terms.
I made a project, and I made a repository on Github. I've been able to work with that and upload stuff to it for some time, on Windows. The Github Windows application is nice, but I wish there was a GUI for the Linux git.
I want to be able to download the source for this project, and be able to edit it on my Linux machine, and be able to do git commit -m 'durrhurr' and have it upload it to the master repository.
Forgive me if you've already done most of this:
The first step is to set up your ssh keys if you are trying to go through ssh, if you are going through https you can skip this step. Detailed instructions are provided at https://help.github.com/articles/generating-ssh-keys
The next step is to make a local clone of the repository. Using the command line it will be git clone <url> The url you should be able to find on your github page.
After that you should be able to commit and push over the command line using git commit -am "commit message" and git push
You can use SmartGit for a GUI for git on Linux: http://www.syntevo.com/smartgit/index.html
But learning git first on the command line is generally a good idea:
Below are some basic examples assuming you are only working from the master branch:
Example for starting a local repo based on what you have from github:
git clone https://github.com/sampson-chen/sack.git
To see the status of the repo, do:
git status
Example for syncing your local repo to more recent changes on github:
git pull
Example for adding new or modified files to a "stage" for commit
git add /path/file1 /path/file2
Think of the stage as the files that you explicitly tell git to keep track of for revision control. git will see the all the files in the repo (and changes to tracked files), but it will only do work on the files that you add to a stage to be committed.
Example for committing the files in your "stage"
git commit
Example for pushing your local repo (whatever you have committed to your local repo) to github
git push
What you need to do is clone your git repository. From terminal cd to the directory you want the project in and do
git clone https://github.com/[username]/[repository].git
Remember not to use sudo as you will mess up the remote permissions.
You then need to commit any changes locally, i.e your git commit -m and then you can do.
git push
This will update the remote repository.
Lastly if you need to update your local project cd to the required directory and then:
git pull
To start working on the project in linux, clone the repo to linux machine. Add the ssh public key to github. Add your username and email to git-config.
For GUI you can use gitg.
PS : Get used to git cli, It is worth to spend time on it.

Resources