Category: Github

Private Remote Git Repository Debian Wheezy and other stories from Hell

Setting up your Private Remote Git Repository

So you need a private remote git repository for collaborative, backup or convenience reasons. You have your local repository setup and now you want another repository in the cloud. Recently, a few moments ago, I started this task and here I am about an hour and a half later sitting a little frustrated and annoyed. Still not knowing whether my git remote is working seamlessly. So let me save you some time…

Has your server security measures / paranoia ever bitten you in the butt? Well good that means you’re doing well.

Security First and Foremost

As a wise server administrator you have installed fail2ban and denyhosts on your server and configured them with severe measures, such as max 3 retries for ssh attempts. Fail2ban scans log files for too many attempts from a specific IP of a specific protocol, so if there is even an inkling of a password brute force attempt on one of your hosted sites or over secure shell the offending IP will be banned in an instant. DenyHosts is similar but focuses primarily on thwarting ssh dictionary attacks. Now if you have ever gazed upon /var/log/auth.log. You will know that our collective paranoia has some backing.

These are great tools and I was sent packing by fail2ban after 3 failed attempts, and my ssh session was cut short prematurely. Shame and I had set the lockout time to 600 hours or 25 days. If this ever happens to you on a static ip, you needn’t worry, just wait the 25 days. Otherwise if you are on a dynamic ip as most home adsl services are on you can wait to get new details via DHCP or force new deets. Otherwise you can ssh into another machine somewhere on the interwebs and then ssh into your server from their.  As fail2ban and denyhosts used iptables (linux firewall) you will use the following commands to unblock yourself…

iptables -L

to see the offending ip’s and spot your own

iptables -D

taken from: HowtoForge

Preventing the Ban

So yes great tools, but why was I locked out. Well I had created a user for the git repository, namely git. However when you first configure ssh (an inherent package of  linux), you configure it in /etc/ssh/sshd_config specifying the port, protocol, whether to permit root logins and also specify users to allow. After creating the git user, I forgot to do this and when the repository was being pushed locally to remote, after 3 attempts I was locked out. So REMEMBER TO ALLOWUSER IN SSHD_CONFIG. Oh AND REMEMBER TO RESTART SSH FOR CHANGES TO TAKE EFFECT.

Setting up Your Private Remote Git Repository Debian Wheezy

Private Remote Git Repository: The Holy Grail

Generate SSH Key

cd ~/.ssh
//if exists id_rsa.pub
clip < ~/.ssh/id_rsa.pub
//else
ssh-keygen -t rsa -C "your_email@example.com"
ssh-add id_rsa
//enter passphrase (Remember to Remember)
clip < ~/.ssh/id_rsa.pub

Create a User for Git

don’t know if this is entirely necessary…

adduser git
//userdel -r git, if you made a mistake

Send id_rsa.pub to the server

ftp or

scp ~/.ssh/id_rsa.pub gituser@server_ip:./

Log in as git

su git

Append to servers authorized keys

cat id_rsa.pub >> /home/git/.ssh/authorized_keys

Add a remote to your local git repository

git remote add [shortname] [url]
git remote add origin git@server.co.za

Initialise the Repo on Server

git init --bare repo.git

Push your local to remote

git push [remote] [branch]

git push origin master

if it asks for a password you are doing it wrong, as you have set up ssh keys for that very reason, not using a password. You should just need that passphrase that you remembered.

Remember Permissions

Permissions of folder should be git:git and 775.

Sources:

Digital Ocean Git

Merkados Git

Git SCM Remotes

Stackflow Git

Bonus: Show branch and colour in bash

Git Github Quick Reference Tutorial

What is Git Github?

Git Github is distributed version control. It is free and open source, made by Linus Torvalds.

It is different from previous version control techniques:

  • Different users maintain their own repositories.
  • There is no single failure point.
  • Changes are stored as change set.
  • Change sets can be exchanged between repositories.
  • Branches can be used, with fast context switching, to control your working development directory.
  • Much faster than any other version control system.
  • No network access required.

Git is for anyone who wants to track changes, it was designed by developers for developers but anyone wanting to track changes can use it. However Git is no good for:

  • Images
  • Movies
  • Fonts
  • Music
  • Microsoft Office (Word, Excel, etc)
  • PDF

Git and Github Quick Reference

Download Git

go to: http://git-scm.com
download and install with Git Bash

Now open Git Bash and go to the folder where you want a new repository:

Initialise a project Git:

git init

Staging Changes Git:

git add .

Commiting Changes Git:

git commit -m "Message"

How to write commit messages Git:

  • Substitute “Message” above with:
  • A short single line description of your changes or actions
  • Optionally followed by a blank line and a more complete description
  • Each line should be less than 72 characters
  • Present Tense
  • Asterix, Hyphen or ‘>’ as bullets
  • It is not email, no dates or times

View change log Git:

git log

Change log additional commands Git:

#single line per commit
git log --oneline
#show just 3
git log --oneline -3
#since
git log --since="2012-02-20"
#until
git log --until="2 weeks ago"
#author
git log --author="Name"
#Searching
git log --grep="test"
#difference of log
git log -p
#statistics
git log --stat --summary
#email format
git log --format="email" 
#graph
git log --graph
#well formatted (works better on unix)
git log --oneline --graph --all --decorate

Architectural differences between Git and other Version Control Git:

Other version control:

  • Two-tree Architecture
  • Repository -> checkout -> sandbox -> commit -> repository

Git:

  • Three-tree Architecture
  • Repository -> checkout -> sandbox -> staging index -> commit -> repository

Git changes and Checksums Git:

  • When anything is changes in Git, a change set is created.
  • Git converts the change set into a SHA-1 checksum
  • Every change including the change message has a unique checksum
  • Therefore is maintains the integrity of changes

Head in Git:

head is a pointer to the tip of the current branch in the repository

Git Status:

Shows current branch and all untracked files (changes not yet staged)

git status

 Differences in Git:

#working directory
git diff

#staged differences
git diff --

#differences between branches
git diff master..sandbox

Deleting in Git:

#Unstaged
#just delete the file

#staged differences
git rm <filename>
git commit -m "Message Delete"

Renaming in Git:

git add <renamed>
git rm <old file>
git commit -m "Rename Message"

Moving Files in Git:

git mv <onefile.txt> <otherfile.txt>

Converting a Real Project to a repository (Skipping Staging) iwth Git:

git init
#skip add just commit
git commit -a

Undo Unstaged Changes with Git:

git checkout -- <Filename>

Undo Staged Changes with Git:

git reset HEAD <Filename>

Undo Commits with Git:

  • Changing the latest commit forces all other commits down the line to change due to checksums
  • You can change the last commit (at HEAD)
  • Below can be used just to change the last commits message
git commit --amend -m "Amend Message"

Undo Commits of Specific Commit with Git:

git checkout <CommitSHA/Checksum> -- <Filename>
#eg.
git checkout 4d5shd6a -- test.php
#or
git checkout b47hsdf4

#Use: git log to get the SHA/Checksum

Revert to Specific Commit with Git:

#stage and commit
git revert <Commit SHA / Checksum>
#no commit no stage
git revert -n <Commit SHA / Checksum>

Reset in Git:

#USE WITH CAUTION - Changes position of HEAD
git reset --soft
git reset --mixed (default)
git reset --hard
  • Soft: Moves Head Pointer, No staging, No Changes in Working Directory.
  • Mixed: Moves head pointer, changes staging directory to match repository, No Changes in Working Directory.
  • Hard: Changes Staging and Working Directory.

Remove Untracked Files from Working Directory in Git:

#test run
git clean -n
#force to run
git clean -f

Ignoring Files in Git:

#in project/ .gitignore
#if not there create
#basic regs: * ? [aeiou] [0-9]
*.php

Typical things to ignore with .Gitignore:

  • Compiled Source Code
  • packages
  • Compressed Files
  • Logs
  • Databases
  • Operating System Generated Files
  • user-Uploaded Assets (Images, PDF, Videos)

Global Ignores in Git:

#personal preferences specific to OS
git config --global core.excludesfile /users/Username/.gitignore_global

Remove just from Staging index in Git:

git rm --cached <Filename>

Tracking an Empty Directory Git:

#Place a small file in the directory, Git only tracks files not directories

Treeish – what is it with Git:

Treeish: References part of a tree

Can Use:

  • full SHA-1 Hash
  • short SHA-1 Hash (4 to 15 characters)
  • HEAD pointer
  • branch reference
  • parent commit: -HEAD^ or -HEAD~2
  • tree is a directory, blob is a file
git ls-tree <treeish>
git ls-tree HEAD

Branching in Git:

  • Don’t use much resources
  • Easy to Create
  • Fast
  • Little space required
  • Ability to try new ideas and test
  • Can create for specific parts of project
  • One working Directory
  • Fast context switching
#check branches
git branch

#new branch
git branch sandbox

#switching between branches
git checkout master

#create and switch branch
git checkout -b master

Checkout Files in Git:

git checkout -- <Filename>

Finding if branch which has all commits within:

git branch --merged

Delete branch in Git:

git branch -d <branchname>

Merging branches with Git:

git merge <branchname>

#fast forwardn is not a true merge
#no fast forward
git merge --no-ff <branchname>
#fast forward only
git merge --ff-only <branchname>

Merge Conflicts in Gits:

  • Go through and decide which changes you want
  • abort merge
  • Resolve Conflicts manually
  • use merge Tool

Avoiding Merge Conflicts with Git:

  • Keep lines short
  • No unnecessary whitespace edits
  • Keep focused do not wander around the project
  • Merge Often

Stashing uncommited Changes in Git:

git stash save "Stash Message"
git stash list
git stash show -p stash{0}

Stash brought into any Repository:

#default puts latest one in
git stash pop
#put specific
git stash pop stash@{2}
#pops but keeps in stash
git stash apply

Delete Items from Stash in Git:

git stash list
git stash drop stash@{1}
#clear stash
git stash clear

Remote Servers and Github:

  • Used as convention Remote not necessarily required
  • Git makes an origin/master referencing remote server

List of remotes:

git remote

Telling Git where the remote repository is, usually Github:

git remote add <name usually origin> <https address or ssl>

Pushing an Existing Repository or github:

#push masterbranch to github
git push -u origin master

Getting a from a Git Github:

#find project on git
#go to folder you want it to reside
git clone <https/ssl of project> <project name>

Pushing changes to Remote or Github:

git push origin master

#if tracking branch
git push

Fetching from remote repository or Github:

git fetch origin
#fetch and merge
git pull origin

Fetching Tips for Github and Remote Servers:

  • Fetch before you work
  • Fetch before you push
  • Fetch often

Delete a remote branch from Github:

#push nothing to origin
git push :<branch>
#new way
git push origin --delete <branchname>

Using an IDE, Git and Github:

many IDE’s have this functionality built in to make it much easier

More Info Git Github:

Git Official Tutorial

Git-Github-tutorial

Reconciling a Detached Head with Master/origin