git cheatsheet

Table of Contents
works with 1 or more simultaneously downloaded repositories
username@machineName ~/git/ops (master)
$ git pull
Already up-to-date.

username@machineName ~/git/ops (master)
$ git status
On branch master
Your branch is up-to-date with 'origin/master'.

Untracked files:
  (use "git add <file>..." to include in what will be committed)

        create_config/generate_config.pl

eothing added to commit bu  (use "git add" to track)

username@machineName ~/git/ops (master)
$ cd create_config/

username@machineName ~/git/ops/create_config (master)
$ ls -laF
total 27
drwxr-xr-x    1 username   Administ     4096 Oct 24 15:00 ./
drwxr-xr-x    1 username   Administ     4096 Oct 24 14:57 ../
-rw-r--r--    1 username   Administ     3151 Oct 24 14:57 README
drwxr-xr-x    1 username   Administ     4096 Oct 24 14:57 cisco_asa/
-rwxr-xr-x    1 username   Administ    17088 Oct 24 15:00 generate_config.pl*
-rwxr-xr-x    1 username   Administ    17088 Oct 24 14:57 generate_config.pl*
drwxr-xr-x    1 username   Administ     4096 Oct 24 14:57 templates/

username@machineName ~/git/ops/create_config (master)
$ vi generate_config.pl
You may need to do this... git pull origin master git clone git.company.com:dns (pulls down repo 'dns') 

Remote Repositories

$ git remote add yoursvrname https://server/namespace/project.git
git remote -v (list remote URLs)
git remote add pb https://github.com/paulboone/ticgit

git fetch <remote> (get all info from remote svr that you don't already have...

$ git clone https://server/namespace/project.git

Branches

git branch dabranch2 (creates branch dabranch2)
git checkout <branch> (change to a branch - ACTUALLY changes HEAD to point to branch in question) 

git show <sha1 hash for branch>

renaming branch

$ git branch --move bad-branch-name corrected-branch-name

github pull forking a change and then submitting

github fork

github creating a branch

show branches in logs

git log --oneline --decorate (list out branches as they occured)
git log --oneline --decorate --graph --all

git branch (check if you are using master or a branch)
git branch -v (list all branches incl master)
git branch <branch> (create a branch)

git branch -d <branch> (delete a branch)

typical branch workflows

$ git checkout iss53 Switched to branch "iss53" $ vim index.html $ git commit -a -m 'Finish the new footer [issue 53]' [iss53 ad82d7a] Finish the new footer [issue 53] 1 file changed, 1 insertion(+) $git branch master $git merge iss53
$ git remote add dafork $ git clone $ cd project $ git checkout -b dafeature ... work ... $ git commit ... work ... $ git commit $ git push -u dafork dafeature OR $ git request-pull origin/master myfork (mail output to project maintainer manually

rebase

(move your feature work to after the patches that project maintainer has merged (now your feature no longer cleanly merges))
$ git checkout dafeature $ git rebase origin/master $ git push -f dafork dafeature This rewrites your history to now look like Commit history after patches that proj maintainer merged
$ git log --pretty=format:"%h %s" --graph (adds visual representation for branches)

adding files

cd <dir>
git add dafile
git add .
git commit -m 'Initial commit'

showing logs

git log (what have you done, committed, deleted, staged, etc. ...)
$ git log --pretty=format:"%h l-%an, %ar : %s"
$ git log --abbrev-commit --pretty=online
$ git log --pretty=format:"%h %s" --graph (adds visual representation for branches)
$ git log --since=2.weeks

git diff HEAD (what's different from your last commit)
git diff --staged (look at diff on what you've staged/committed

git reset <filename> (unstage file)
git reset --hard origin (reset all the files to be from the repo)

git checkout -- <filename> (revert file to content before last commit for file)
git branch clean_up (get rid of multiple intermediate file versions)
git rm <filename> (remove local files and stage git repo removal of files)

git checkout master (or <branch>) (REVERTS MASTER OR BRANCH TO LAST COMMIT AND SWITCHS TO THAT master/branch)

git merge <branch> (merge branch into master)
git stash (Stash the changes in a dirty working directory away, return directory to current uploaded git status)

git commit

$ git commit --amend (after adding\correcting files forgotten during previous commit)
$ git commit -m 'Initial commit'
$ git add forgotten_file
$ git commit --amend

Unstaging a Staged File

$ git add *
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed: README.md -> README
modified: CONTRIBUTING.md

$ git reset HEAD CONTRIBUTING.md
Unstaged changes after reset:
M CONTRIBUTING.md
$ git status
On branch master
Changes to be committed:
(use "git reset HEAD ..." to unstage)
renamed: README.md -> README
Changes not staged for commit:
(use "git add ..." to update what will be committed)
(use "git checkout -- ..." to discard changes in working directory)
modified: CONTRIBUTING.md
git revert  (revert commit to 'before it is pushed')

username@machineName ~/git/ops/create_config (master)
$ git commit -am "testing small change to generate_config.pl"
[master a89fe0a] testing small change to generate_config.pl
 1 file changed, 1 insertion(+), 1 deletion(-)

username@machineName ~/git/ops/create_config (master)
$git revert a89fe0a (roll back commit before push)

username@machineName ~/git/ops/create_config (master)
$ git push origin master

Git commandline admin

Nagios
username@username-machineName:/h/git/nagios/etc> grep <Site> *
(grep Atlanta *)

username@machineName:/h/git/nagios/etc> vi servicegroups.cfg
	search for site, put in entry in ro-isp-1 and ro-isp-2 line - ",ro-isp-1,<Site> ISP # / <ISP>"
	(,ro-isp-1,city2 ISP 1 / Orange)
username@machineName:/h/git/nagios/etc> vi services_network.cfg
	define service {
		use     EST-service
		host_name   VPN_Tunnels
		service_description city / xx.yyy.zz.a/22 -  datacenter
		check_command   check_ipsec_tunnel!bb.cc.ddd.ee!fff.ggg.hhh.iii
	}
username@machineName:/h/git/nagios/etc> vi services_unix.cfg
define service {
	use                     critical-24x7-service
	host_name               ro-isp-1
	service_description     city2 ISP 1 / Orange
	check_command           check_ro_isp_1!80.11.196.68!60!80
	}
define service {
	use                     critical-24x7-service
	host_name               ro-isp-2
	service_description     city2 ISP 2 / OVH
	check_command           check_ro_isp_2!109.190.86.25!60!80
	}
username@machineName:/h/git/nagios/etc> git commit -am "<ticket £> <change description>"
(git commit -am "corrected ISP addresses for city")

Getting Git/SSH to work
Make sure local repository has permissions that allow you to modify the files:
	[username@server .ssh]$ cd -
	/usr/local/company/asterisk
	[username@server asterisk]$ ls -l
	total 4
	drwxrwxr-x 2 username zipgroup500 4096 Nov  3 14:18 etc
	[username@server asterisk]$ ls -l etc/
	total 32
	-rw-rw-r-- 1 username operations  2071 Nov  3 14:11 server1.conf
	-rw-rw-r-- 1 username operations  2221 Nov  3 14:11 server2.conf
	-rw-rw-r-- 1 username operations  2070 Nov  3 14:11 server3.conf
	-rw-rw-r-- 1 username operations  2099 Nov  3 14:11 server4.conf

Make sure ~/.ssh/ directory includes, with something like the contents noted:
	config
		Host git.company.com
		User gitolite
		IdentityFile ~/.ssh/nopass_id_rsa
	nopass_id_rsa
		(generated private key)