Working with git

From Wildsong
Revision as of 18:00, 24 March 2016 by Brian Wilson (talk | contribs)
Jump to navigationJump to search

Overview

Sticking with the concept of running my own cloud servers instead of putting every bit of my life out on the Internet, I want to store my source code in my own git server.

I have already installed git and ssh and web services on Bellman so now all I need to do is configure it.

Use case: web app development

One of the main reasons I want my own git server is so that I can sync a web app between my laptop and a server.

My work flow: I develop on the laptop, then I push changes to the git server, then I pull the changes down into the live web server.

The theory is that if the live server breaks, I can revert the changes.

Use case: Asterisk servers

Each server needs tweaks to its configuration. When you are bending config files and something stops working you need to be able to back out the changes.

Creating a new repo

See also: http://www.git-scm.com/book/en/v2/Git-on-the-Server-Setting-Up-the-Server

I am using ssh for file transfer.

I find it easiest to create a new empty repo directly on the server and then push existing content into it.

ssh bellman
cd /green/repositories
git init --bare myproject.git create a bare repository
exit

cd Projects
assuming myproject is a folder containing the files you want in a repo
cd myproject
git init
make a gitignore file in myproject
git add some files
git commit -m 'initial commit'
git remote add origin bwilson@bellman:/green/repositories/myproject.git
git push origin master this pushes committed files from the local repository to the master branch on the remote machine

Now I should be able to clone my project onto the web server and start using git to keep it updated.

ssh webserver
cd /var/www/appserver
git clone bwilson@bellman:/green/repositories/myproject.git

On my copy I need to define the remote repo,

git remote add origin [email protected]:/green/repositories/myproject.git

When I make changes on the local laptop, first I commit them locally.

git commit

..then I push them up to the git server

git push origin master

..then I pull them down onto the public web server

git fetch

Then I create a virtualenv and load the requirements.

cd myproject
virtualenv env
source env/bin/activate
pip install -r requirements.txt
python run.py

Branching and all that

This explanation is pretty good. Darn good. http://longair.net/blog/2009/04/16/git-fetch-and-merge/

Git submodules

I keep some code in github, and I want to incorporate it into a project. I can use the submodule feature.

cd myproject
git submodule add [email protected]:brian32768/pyst2.git

This clones pyst2 into a subdirectory (called pyst2 by default) Now I can work on pyst2 and do commits and pushes but it remains separate from 'myproject'. If I ask for status...

git status
On branch master
Your branch is up-to-date with 'origin/master'.
Changes to be committed:
 (use "git reset HEAD <file>..." to unstage)

modified: .gitmodules new file: pyst2

If I am in a virtualenv then I can cd into pyst2 and do 'python setup.py install' and the right thing will happen.