I had covered my first adventures with Git in a previous post. That article focused on creating a local repository; you could save your versions to the local hard drive. That approach works well enough for local work...you can roll back changes, keep track of changes, get frustrated with Git on your local system.
But what if you want to have some redundancy in the case of hard disk failure? Or if you want to work on multiple machines?
In that case you might want to create a simple Git server, a system on which to save your progress and transfer them to other machines. The process isn't all that difficult.
In this example I'm going to turn a Raspberry Pi with a USB-connected hard drive into a network Git server.
Create the Git User on the Server
First, on the Pi, I add a git user.
sudo adduser git
su git
cd
mkdir .ssh
chmod 700 ./.ssh
Raspian on the Pi had an implementation of adduser that asked for the password by default. If yours didn't, you'd have to run "sudo passwd git" after creating the user so you can su into the account. Plus you generally want a password on any account you create on a networked machine...
Enable Passwordless Connection with SSH
Now I'm going to get a public key from the developer (client) machine.
If you have a .id_rsa.pub in your ~/.ssh directory, you're already set. If not, you can usually create one with a variation of:
ssh-keygen -t rsa -C "your_email@address.com"
Then transfer the public key to the git server. ONLY transfer the .pub file!
scp ~/.ssh/id_rsa.pub git@gitserveraddress:~/
Now, back on the Git server:
mv ~/id_rsa.pub /home/git/.ssh
cd /home/git/.ssh
Make sure the .pub file is owned by the git user. If there are no other files in .ssh (which there aren't, if this is the first time you're setting this up...just rename the .pub file to authorized_keys. If the file were already there, I'd append the new .pub file contents to authorized_keys.
mv id_rsa.pub authorized_keys
Now, if you're on the client computer and run "ssh git@gitmachinesaddress, it should automatically log in.
Time to Create a Workspace to Save Projects
Next step is to create your git workspace. On the Pi, I'm going to put it into my USB-connected drive instead of the SSD card. If you're familiar with Linux, it's not hard to suss out what I'm doing and what you'll have to modify for your own installation. I'm going to exit back to my default user (who already has sudo ability, which git doesn't...) to do the following.
cd /mnt/mydrive
sudo mkdir gitrepo
sudo chown git:git gitrepo
Create a Directory For Your Project (Each Time)
Now create a directory, as git, for your project. In this case, I'm going to use my loopcheck.go test application. As git, create the loopcheck project directory.
su git
cd /mnt/mydrive/gitrepo
mkdir loopcheck.git
cd loopcheck.git
git --bare init
Push the Project to the Server
Almost there! On the development computer, add the remote repo and push it to the server. You did already have the project git-staged, yes?
From the project's source code directory (where you already have git versioning...)
git remote add origin git@gitserveraddress:/mnt/mydrive/gitrepo/loopcheck.git
git push origin master
There you go! Now the loopcheck project is on the local system and a remote git server!
Pull the Project to Another Client
If you want to duplicate your project on another system, you can use the clone command. Most Go projects live under /src. So I would connect to the system I want to work on and (for me)
cd ./go/src
git clone git@gitserveraddress:/path/to/repo/on/server/loopcheck.git
...at that point I should have the loopcheck project available, and can cd into the project, edit files, and use
git add .
git commit -m "message for the commit"
git push
...to update the repo! Be aware that if you didn't install the public key from the new workstation, it will prompt you for the git user's password in order to download the project.
There is no tracking information for the current branch...
When I tried pulling the changes from client 2 to client 1, I had an error that began, "There is no tracking information for the current branch..."
A quick fix is to use
git branch --set-upstream-to=origin/master master
...then it let me use git pull to pull the changes.
No comments:
Post a Comment