How can I copy files between localhost and server using git? - linux

I have server on Ubuntu, where i host many projects. Files visible to world in /var/www
After working on my localhost with project "projectName" I open FTP client and send files to server. I want to make a remote repo on my server and I want to have newest files in folder /var/www/projectName
Is there any simple way to do that? I have read GIT manual, but they always write about having only repo on server, not to store newest files in specific directory.
I'm the only person who will be pushing files to the server.

That depends, if you have ssh access to the server, yes, just create the git repo on the server then pull it locally and then git push, you will have to create a git hook on the server to auto "checkout" the latest changes.
server :
cd /var/www/site.com
git init
git add -A
git commit -a -m 'initial'
cd .git
echo "#!/bin/sh" > hooks/post-receive
echo "git checkout -f" >> hooks/post-receive
chmod +x hooks/post-receive
local :
git clone user#host:/var/www/site.com
cd site.com
--edit stuff--
git commit
git push

Related

CentOS: Git: "fatal: could not read from the remote repository"

( I have already read through this, and several other posts, thoroughly git: fatal: Could not read from remote repository )
I'm using my own server as a git server. I set it up according to several guides. Everything is fine except any operation that read or writes to the remote git repository.
Problem:
When I try to do anything that interacts with the remote server that I have set up, I get:
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository
exists.
in windows shell. In Cygwin it just hangs.
Quick Background:
I'm using CentOS 7 as the server and Windows 10 as the client.
On the server:
I made a new user 'git'
mkdir /home/git/myproject.git
git init --bare
opened up necessary ports 9418, 22, & 443 using:
"firewall-cmd --permanent --add-port=22/tcp" and
"firewall-cmd --reload"
On the client:
created a folder, created a text file with some text, ran 'git init', 'git add .'
setup rsa keys according to several guides
What Works:
I can ssh into the server fine with windows shell, cygwin and puTTy. The folder /home/git/mproject.git exists. git has been working fine locally using either windows shell or Cygwin.
Attempted solutions:
I've tried:
chmod 600 pyproject.git
chmod 700 pyproject.git
chmod 777 pyproject.git
git remote add origin git#my-site.com:/home/git/myproject.git
git remote add origin git#my-site:/home/git/myproject.git
git remote add origin ssh://git#my-site.com/home/git/myproject.git
git remote add origin ssh://git#my-site.com/repo-<wbr< a="">>/home/git/myproject.git..git
git remote add origin ssh://git#my-site.com/repo-<wbr< a="">>/home/git/myproject.git
git remote add origin git#my-site.com:/home/git/myproject.git
git clone git#my-site.com:/home/git/myproject.git
git remote add origin https://git#my-site.com/home/git/myproject.git
git clone ssh://git#my-site.com/home/git/myproject.git
git clone ssh://git#my-site.com/myproject.git
git clone https://git#my-site.com:myproject.git
git clone ssh://git#my-site.com/home/git/myproject.git
git clone git#my-site.com/home/git/myproject.git
git clone git#my-site.com:/home/git/myproject.git
git clone git#my-site.com/myproject.git
I can log into the server with ssh git#my-site.com just fine. I can also navigate to the /home/git/myproject.git folder. The ports are open. What else could be wrong?
So the problem seems to have been that the client and the server were using versions of git that were very different. The client was using 1.9 I believe, and the server was using 2.6. Not only that, the versions of git differed depending on the whether I was using windows terminal or Cygwin.
Now, after removing old versions and updating git, commands like:
git remote add origin git#mysite.com:/home/git/myproject.git
git push origin master
work correctly.

git over ssh second user can't clone

We are two developers working on a project and I set a git repository on a server (ArchLinux) we both have access over ssh. The git repository was created the following way :
mkdir project
touch project/readme.txt
git init project
cd projet
git add .
git commit -m 'initial commit'
git push origin master
cd ..
git clone --bare project.git
cd project.git
git config core.sharedRepository true
cd ..
setfacl -R -m g:<group name>:rwX project.git
find gitrepo -type d | xargs setfacl -R -m d:g:<group name>:rwX
our group name is dev and both users are in the group (checked in /etc/groups)
On a second machine (can be mine or the second developer's), we clone the repository this way.
git clone user1#ip:/path/project.git (over ssh)
password: <enter user1 password>
Everything is put inside a folder project. This user can now do add, push, pull etc.
Then we try the second user
git clone user2#ip:/path/project.git (over ssh)
password: <enter user2 password>
and we get this
fatal: '/path/project.git' does not appear to be a git repository
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
We can connect the second user with SSH with no problem so it's not a bad password. Do you have any idea why we encounter this problem?
Looks like you did not set up git deamon
Read the whole page (its a short one) and follow the steps.
Don't skip the part where you define which projects can be shared.

I want to add remote branch of my local repo?

I want to put my local git project in production server (Linux multi hosting account of go-daddy).
Is it possible to add my production server as my remote branch ?
So, having logged in via SSH:
$ mkdir mywebsite.git
$ cd mywebsite.git
$ git init --bare
on local git repo
$ git remote add production git#myserver.com:mywebsite.git
$ git push production +master:refs/heads/master
I got my solution,but thnx all of you !! :-)
You can add a new remote:
git remote add prod /url/to/empty/bare/repo
And then push your master branch to it:
git push -u prod master
But that supposes you initiated a bare repo on that production server first.
If git is installed on your server and you have cloned a copy of the repository there, you can add it as a remote using git remote.
git remote add [-t <branch>] [-m <master>] [-f] [--[no-]tags] [--mirror=<fetch|push>] <name> <url>
So:
git remote add production URL
You'd need to push from local and pull on the remote though. So you'd still have to SSH into your server to deploy.
If your end goal is simplified deployment you may wish to look into tools like Jenkins, which you can run locally and can be configured to connect to your server and deploy after running tests, etc.
That might be your best option if your hosting provider won't let you install Git.
Yes possible. You need the following command:
git remote add (remote-name)

Deploying git repo to "production" server

I have a git repo where a group of developers and I are collaborating on project code. occasionally, we'd like to copy the git repo to a VPS (setup as Linux server) that has an external IP address (w.x.y.z).
I can go into an SFTP client and navigate up the folder hierarchy to the server root and then navigate down to /var/www/ (server web root) to drop my files in but I'd like to deploy to the server through the command line instead.
My goal is to configure the Linux server as a remote git directory but don't know how to go about navigating up the file hierarchy to have git recognize that the remote branch repo needs to go into /var/www/.
Brief search has uncovered git remote add origin username#w.x.y.z then use git push origin.
It seems that connecting this way to w.x.y.z will land me at the home folder of 'username', not the root web directory (/var/www/) when accessed via the browser.
Does anyone have insight into how I should go about setting up a remote directory for deploying a git repo to a "production" server?
You appear to be doing this a rather non-obvious way. I think what you want to do is copy the git repo to somewhere else (the vps server). The standard way to achieve this is git clone.
In your /var/www/ directory or an appropriate subdirectory thereof, do:
git clone [URL-FROM-GITHUB]
That will clone the git repository to your VPS. You can then update it with
git pull
could script this with
ssh my.vps.server 'cd /var/www/whatever && git pull'
However, normally you don't want the entire project in '/var/www/...' because that would also put stuff you did not mean to deploy there, e.g. the .git directory. Hence perhaps better to clone the repo within your home directory, and make a small script to rsync the appropriate /var/www/ directory against your repo, using --exclude to remove files you don't want, or just rsync-ing a subdirectory of the repo.

Dummy questions about setting up git on amazon cloud ec2

first of all, apologize for dummy questions that I might throw here. It would be nice if you could point the directions where should I go from here.
I'm totally new to version control(as well as git) and cloud system. However, it came to the point that I have to develop php web based application on AWS EC2 instance and make codes contributable for future developers.
I did successfully create EC2 instance that run PHP/MySQL and map the domain with Elastic IP. So the website is now publicly accessible via port 80.
I also installed git using $sudo yum install git and configed user.name and user.email
I then, go to root folder of the website (e.g. public_html) and run ‘git init’ which create the fold “.git” and I then add file using “git add .” and commit “git commit -m ‘initial upload’”
Is that the right way to go? Would it be ok to have the project folder sitting on /public_html (where accessible from anyone).
If above is ok, then where should I go from here?
I would like to have git server running on EC2 that allow developers to connect from their local machines (e.g. Eclipse) while being able to keep the backup and compare the different between codes.
What detail do I suppose to give developers so that they can connect to git server and working on project?
I quick direction or few keywords to do more research would help.
look here for more information on setting up git on amazon ec2
to allow developers to use you git, you just need to give them the git server url.
Direct quote from the site i'm linking to.
"First and foremost, you need to add your EC2 identity to the ssh
authentication agent. This prevents problems with git later, namely
getting the error “Permission denied (publickey).” when trying to do a
git push to the EC2 repository.
ssh-add path/to/privateEC2key.pem
Now you can go ahead and create the git repository on the EC2
instance.
ssh username#hostname.com
mkdir the_project.git
cd the_project.git
git init --bare
So not much going on here, all we do is create an empty repository and
then leave. Now, on the local machine, you do something like the
following:
cd the_project
git init
git add .
git commit -m "Initial git commit message"
git remote add origin username#hostname.com:the_project.git
git config --global remote.origin.receivepack "git receive-pack"
git push origin master
The ‘git config’ command is a fix that I found necessary to be able to
push to the EC2 repository."
The mentioned link by Alex gives a good starting point to setup git on ec2. But I followed a little different approach as mentioned here. link. Direct Quotes from the page:
"Connecting with SSH without a PEM key" : So either you add the ec2 private key and add it as a entity in your ssh authentication agent or create a new ssh key for your user and use that. Steps to be followed are:
Create SSH Key
First up you will need to navigate to your .ssh folder on your local machine :
cd
cd .ssh
if this folder doesn’t exist use mkdir to make it.
Once in your ssh folder on your local machine which should be in /Users/yourusername/.ssh generate your key by executing the following.
ssh-keygen -t rsa -b 1024
When prompted enter the file name to save the key enter id_rsa_aws, when prompted to enter a password leave blank.
In your .ssh directory execute the following command and copy the output to paste later.
cat id_rsa_aws.pub
Now connect to you AWS instance using you PEM key
ssh -i path/to/yourkeyname.pem ubuntu#xx.xxx.xxx.xxx
Once in
echo 'the key you copied from id_rsa_aws.pub' >> .ssh/authorized_keys
chmod 640 .ssh/authorized_keys
chmod 750 .ssh
Now you go to your machine and type
cd desired directory
git clone ubuntu#xx.xxx.xxx.xxx:<path_to_your_just_created_git_server>
If you did all the above mentioned steps correct, the only warning you might get is
warning: You appear to have cloned an empty repository.
That's ok. Now you can copy all your code into the clone directory, and follow the following steps:
git add .
git commit -m "Initial commit"
git push origin master // If working on master branch
i created a GitHub gist with all the details hope it helps
https://gist.github.com/eslam-mahmoud/35777e4382599438023abefc9786a382
//add your EC2 .pem file to ssh kys
ssh-add ~/aws/mypemfile.pem
//create bare repo on AWS EC2 webserver and deploy on demand
mkdir ~/git/the_project
cd ~/git/the_project
git init --bare
//create local repo and track remote one
cd ~/git/the_project
git init
git add .
git commit -m "Initial git commit message"
git remote add aws ubuntu#1.1.1.1:~/git/the_project
git config --global remote.origin.receivepack "git receive-pack"
git push aws master
//create tag
git tag -a v0.1 -m "my version 0.1"
//push tags
git push aws --tags
//Or you have one so you push your updates
git remote add aws ubuntu#1.1.1.1:~/git/the_project
git config --global remote.origin.receivepack "git receive-pack"
git push aws master
//create tag
git tag -a v0.1 -m "my version 0.1"
//push tags
git push aws --tags
//on server create another local repo that track the bare one to deploy
git clone ~/git/the_project
cd ./the_project
//checkout tag
git checkout v0.1
//install clear cache ...
npm install

Resources