Last active
November 3, 2017 21:28
Star
You must be signed in to star a gist
Pushing your local Git repository to a Remote server — Linux & Mac OS X
This file contains 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 characters
Pushing from Linux or Mac OS X | |
On your local computer, navigate to your user's ~/.ssh directory: | |
[local ~]$ cd ~/.ssh | |
If the directory doesn't exist, create it: | |
[local ~]$ mkdir ~/.ssh | |
Set up SSH keys by running the following command: | |
[local ~]$ ssh-keygen -t rsa -b 4096 -C "Remote Server Git repo" | |
Enter a name for the file when prompted, such as 'Remote-git-key'. | |
When prompted to enter a password, click 'Enter' twice to skip. | |
Two new files are created in your user's .ssh directory: | |
[local ~]$ cd ~/.ssh | |
[local ~]$ ls -la | |
Remote-git-key | |
Remote-git-key.pub | |
Use the following instructions to copy your public key into your Remote server's 'authorized_keys' file. | |
To run the following commands, you need the name of your Remote server and the user/pass your website is under. View the following articles to locate this information: | |
Locating your servername | |
Locating your username | |
Resetting your user password (if necessary) | |
If you're using Linux: | |
[local ~]$ ssh-copy-id -i ~/.ssh/Remote-git-key.pub user@server.Remote.com | |
If you're using Mac OS X (also works with Linux): | |
[local ~]$ cat ~/.ssh/Remote-git-key.pub | ssh user@server.Remote.com "mkdir ~/.ssh; cat >> ~/.ssh/authorized_keys" | |
You may receive the following error when running this command: | |
mkdir: cannot create directory `/home/user/.ssh': File exists | |
This simply means the /.ssh directory already exists on the web server. If you see this, remove the portion of the command that creates the directory and run again. For example: | |
[local ~]$ cat ~/.ssh/Remote-git-key.pub | ssh user@server.Remote.com "cat >> ~/.ssh/authorized_keys" | |
Log into your Remote server via SSH: | |
[local ~]$ ssh user@server.Remote.com | |
If you receive an error when attempting to log in, you may need to use ssh-agent. See the following article for more information: | |
Using ssh-agent to log in | |
Confirm your key has been added to the Remote server by running the following (this should output your key): | |
[local ~]$ cat ~/.ssh/authorized_keys | |
Create a new directory for your new remote repository on your Remote server. If this code is meant to replace your existing website, you could name it the same as your website ending in .git. | |
Since this is a remote repository and not a working repository, the directory name you create must end with .git. You can then checkout this repository in the future to work on it. | |
[server]$ cd ~ | |
[server]$ mkdir example.com.git | |
[server]$ cd ~/example.com.git | |
[server]$ git init --bare | |
[server]$ exit | |
Navigate to your git repository on your local computer, and then push to the remote repository on your Remote server. Make sure to change 'user' and 'server' to your actual Remote username and servername: | |
[local ~]$ git remote add Remote ssh://user@server.Remote.com/~/example.com.git | |
[local ~]$ git push -u Remote master | |
You should see the following response: | |
Counting objects: 3, done. | |
Delta compression using up to 2 threads. | |
Compressing objects: 100% (2/2), done. | |
Writing objects: 100% (3/3), 200 bytes | 0 bytes/s, done. | |
Total 3 (delta 0), reused 0 (delta 0) | |
To ssh://username@server.Remote.com/~/example.com.git | |
* [new branch] master -> master | |
How can I confirm my repository was correctly pushed to my Remote server? | |
If you run 'git status' in the ~/example.com.git directory, the following error appears: | |
[server]$ git status | |
fatal: This operation must be run in a work tree | |
This is because it's a 'bare' repository, not a working directory you can use. You could clone this remote repository into a working Git repository. This allows you to view the files that were pushed to the server. | |
Log into your Remote server via SSH. | |
Clone the repo to a new location by running the following in your user's directory: | |
[server]$ mkdir ~/cloned.example.com.git | |
[server]$ git clone ~/example.com.git ~/cloned.example.com.git | |
[server]$ cd ~/cloned.example.com.git | |
[server]$ ls -la | |
This cloned directory now contains all the files you pushed from your local machine. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment