Git Commands

http://www.ndpsoftware.com/git-cheatsheet.html#loc=index;

GitHub error(can’t push to github public key) – http://help.github.com/linux-set-up-git/

$ git reset --hard 1e45d (Deletes the commit after you selected or moves the branch pointer to where you decide to go)

# equivalent to svn revert, If you want to throw out all of the changes you’ve been working on
$ git reset --hard

# reset/undo a single file, revert
$ git checkout -- [filename]

#go back one commit
$ git reset [branch]~1

#undo the commit
# without auto commit
$ git revert -n 1sdf34
# with auto commit
$ git revert 1sdf34

$ git branch -d [path] # delete branch
$ git push origin --delete [branch_name] # delete remote branch
$ git push origin master-testing:master-testing # push to a branch that is not currently checked out
$ git branch -a #show all branch local and remote
$ git branch -rd public/master # removes remote branch
$ git branch [branch name] (creates branch but doesn't check it out)
$ git checkout -b [branch name] (shortcut: creates branch and checks it out)
$ git checkout -b new_database upstream/new_database (creates branch, checks it out and track from upstream)
$ git branch -u [remote] [branch name] (set current branch upstream)

# this will go to that commit but you will be in a no name branch
$ git checkout fa15gvwsgc

# get a specific file from another branch into current branch
git checkout [branch] [file_path]

# clone and checkout only a file
# http://stackoverflow.com/questions/2466735/how-to-checkout-only-one-file-from-git-repository#answer-2466755

# to remove the red delete: not the whole directory system, stage the deleted files
$ git add -u

# add parts of the file, chucks
$ git add -p

#to remove from stage or untrack a file
$ git rm --cached -f <file name>

$ git branch (check branches and which one your in)
$ git diff branch1 branch2 (show what line of code has been changed or git difftool)

# Show git diff on file in staging area, view code that is going to be committed
$ git diff --staged

# difference between file in 2 different branches
git difftool [branch1] [branch2] — [file]
git diff [branch1] [branch2] — [file]

# Showing which files are different in 2 branchesf
git diff –name-status [branch1] [branch2]

$ git commit -am [commit description] (shortcut: this allows you to add (-a) and name the commit (-m) all at once)

$ git config --global color.ui true (set git to be colorful)

$ git clone git://github.com/schacon/ticgit.git #read Only - (this downloads a folder(directory) from github on to your computer. to work with it you need to change the directory(cd))
$ git clone username@192.168.1.114:/custom_path
$ git clone server:/path [custom folder name]
$ git clone -o [custom name] user@baligena.com:/custom_path

# git inside another git
# view all submodules
$ git submodule
# http://git-scm.com/book/en/v2/Git-Tools-Submodules
$ git submodule add https://github.com/baligena/firstRepo.git [name/path]
# update to newest remote commit, will unbranch
$ git submodule update --remote [repository]
or
$ git submodule update --init
# easy way pull latest of all submodules
$ git submodule foreach git pull origin master

# remove submodule
$ git submodule deinit [submodule name]
$ git rm [submodule name]

pull == fetch + merge #stick with fetch and merge, pull may throw error if you dont know what you doing. Like trying to pull anything other than the master branch

$ git log --online --graph --all --decorate # a nice/pretty way to see the git log
$ git log --pretty=format:"%an %h" (formats the log output to your liking)

# reachable different between two branches
# what commits hasn’t been merged into master
# what commits is in branchA that is not merged into master yet? is there anything unique
$ git log branchA ^master

# shows how many lines were change by the plus and minus symbol
$ git log --stat

# shows what was changed (diff) in each file/commit ( 2 first commits )
$ git log -p -2

# show log tree of only local branches
$ git log --branches --not --remotes

# show log tree of only remote branches
$ git log --branches --remotes

# show log of branches by regular expression
$ git log --branches=version1*

# show log of branches by regular expression
$ git log --branches=version1*

# Look for differences whose patch text contains added/removed lines that match, regular expression (regex)
$ git log -G fsq*

# commits per user
$ git shortlog -sne

# show git settings
$ git config --list

#set alias
$ git config --global alias.lol "log --oneline --graph --decorate"

#set alias with date, https://coderwall.com/p/euwpig
$ git config --global alias.lol "log --graph --pretty=format:'%Cred%h%Creset %Cgreen%cr%C(yellow)%d%Creset %s %C(bold blue)<%an>%Creset'"

$ git stash save "name" #saves the current state without having to commit see http://book.git-scm.com/4_stashing.html
$ git stash list #shows your stashes
$ git stash apply stash@{1} #apply you save stash

# video on how to set up your own git server like github – http://www.youtube.com/watch?v=DDzeiI2yZL8&feature=related

#when pushing to the server make sure your not in the branch your pushing to
# its located in the .git/config line 10
# make sure you have permission to .git folder (sudo chgrp)
$ git remote add origin username@xxx.xxx.x.xxx:/directoryName
#removes origin, so you can change it. file location is => /.git/config
$ git remote rm origin

# rename a remote
$ git remote rename <old> <new>

#creates an alias
$ git push origin [branch Name] or $ git push origin --all
$ git fetch origin [branch Name]

# I tried to tie htdocs in lampp a server and was getting an error when I push because I had to change the owners from root to username. I ran
$ chown username -R htdocs

#git mergetool set which one you want to use/candidates
$ git mergetool -t <candiatename>

# instrustions on how to use kdiff3 – http://kdiff3.sourceforge.net/doc/merging.html

# merge a specific/selected commit/file/changes – http://technosophos.com/content/git-cherry-picking-move-small-code-patches-across-branches/
$ git cherry-pick 5d3e1b6

# only merge if theres no conflict, fast forward
$ git merge --ff-only [branch]

# Git command line for merging without committing
# http://stackoverflow.com/questions/12912985/git-visual-diff-between-branches#answer-16790950

git merge --no-commit --no-ff branchB

$ git reset --merge #cancel the merge

# want to change the order of commits, add additional one, combine, previous, remove/delete
$ git rebase -i [branch]~[number of commits]
# if confict you may need to $ git mergetool
$ git rebase --continue # to continue with rebase
$ git rebase --abort # to quit rebase

#rebase, https://www.youtube.com/watch?v=Ypi4Kwx0GJw
#when on a different branch
$ git rebase [name of branch to go under current checked out branch]

#how many commits per file
$ git ls-files | xargs wc -l

#adding patches in git
, http://ariejan.net/2009/10/26/how-to-create-and-apply-a-patch-with-git/
#on the new branch

$ git format-patch master --stdout > newpatch.patch #what this does is get the difference between the branches into a text file

#back on the master branch

$ git am < newpatch.patch

#if error
$git am --skip

# adding patch 2
# on test branch
$ git diff ..master > master.patch
$ git apply master.patch

#rename a commit
git commit –amend
or
git commit –amend -m “Your new message here”

#unstage a file
git reset — <file>

http://git-scm.com/book/en/Git-Basics-Tagging
$ git tag
$ git tag v2.0

# when trying to git clone a repository in github so you can edit(ssh) on a new computer you need to create a key
$ ssh-keygen
Enter file in which to save the key (/home/sock/.ssh/id_rsa): (enter)
Enter passphrase (empty for no passphrase): (enter)
Enter same passphrase again: (enter)
#get key:
$cat ~/.ssh/id_rsa.pub
#then go to https://github.com/settings/ssh and add the ssh key

#serving git
http://toroid.org/ams/git-website-howto
$ git push web +master:refs/heads/master

http://pythonsweetness.tumblr.com/post/52587443706/devs-please-stop-serving-git-to-the-outside

There are two types of repositories: bare and non-bare
non-bare you cannot push to a working copy, thus: [remote rejected] master -> master (branch is currently checked out), cannot push to master

Bare repositories do not have a working copy and you can only push, pull and fetch to them.
Those are the types of repositories you get in Github! If you want to create a bare repository, you can use

$ git init --bare

to change from normal to bare

$ git config --bool core.bare true

# add this block of code to the end of .git/config to not have to specify the branch and remote repository
# everytime you push or pull
[branch “master”]
remote = server
merge = refs/heads/master

# Find/Search a String in the Entire Git History
git rev-list --all | xargs git grep -F ''

# removing sensitive data, passwords, keys and things like that
http://code.tutsplus.com/tutorials/git-tips-from-the-pros–net-29799#highlighter_961797

# good resource:
http://code.tutsplus.com/tutorials/git-tips-from-the-pros–net-29799

# host git on dropbox
https://github.com/anishathalye/git-remote-dropbox

### using git to deploy to production server
http://stackoverflow.com/questions/279169/deploy-a-project-using-git-push/3387030#answer-327315
http://stackoverflow.com/questions/3221859/cannot-push-into-git-repository#answer-12065924
http://stackoverflow.com/questions/9227282/git-push-to-checkout

Google API maps example/snippet

Google API maps example/snippet:

<script type="text/javascript" src="http://maps.google.com/maps/api/js?libraries=geometry&sensor=false&language=en"></script>			

" ((ACOS(SIN($lat * PI() / 180) * SIN(this.latitude * PI() / 180) + COS($lat * PI() / 180) * COS(this.latitude * PI() / 180) * COS(($lng - this.longitude) * PI() / 180)) * 180 / PI()) * 60 * 1.1515) "; 
			

<script>
/**
*
* uses google API to get location in the search text input 
* @author 2013-06-25
* 
*/

function searchLocations() {
    var deferred = $.Deferred();
    var address = $("#te_qkeywords").val();
    var geocoder = new google.maps.Geocoder();

    geocoder.geocode({address: address}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
            var latitude = results[0].geometry.location.nb;
            var longitude = results[0].geometry.location.ob;

            $('#latitude').val(latitude);
            $('#longitude').val(longitude);

            if(latitude == undefined || longitude == undefined){
              console.log('%c FSQ ERROR: latitude or longitude is blank', 'color: red');
              return;
            }
            deferred.resolve();

        } else {
            alert(address + ' not found');
        }
    });
    return deferred;
};

$('#search_button').click(
  function(event){
      event.preventDefault();

      searchLocations().done(function(){
          $('#target').submit();
      });
});


</script>

<script type="text/javascript">
/*-------------------------------------------------
	Google Map API, 2013-07-01
--------------------------------------------------*/

var get_coordinate = function(coordinate){
		var str = document.URL;

		var n = str.search(coordinate);
		var http_request = str.substring(n);
		var and_sign = http_request.search('&');
		var equal_sign = http_request.search('=');
		return http_request.substring(equal_sign+1,and_sign);
};

var me = new google.maps.LatLng(get_coordinate('latitude'),get_coordinate('longitude'));

var fit_all_markers_in_map = new google.maps.LatLngBounds();

var map = new google.maps.Map(document.getElementById('map'), {
  zoom: 10,
  //center: new google.maps.LatLng(-33.92, 151.25), //default location
  mapTypeId: google.maps.MapTypeId.ROADMAP
});

var infowindow = new google.maps.InfoWindow();
var marker_position = [];
var distance_between_search = [];
var meters_to_miles = 0.000621371;
var marker, i;

for (i = 0; i < locations.length; i++) {  
    marker = new google.maps.Marker({
    position: new google.maps.LatLng(locations[i][1], locations[i][2]),
    map: map,
    icon: locations[i][3],
  });
  
  distance_between_search[i] = google.maps.geometry.spherical.computeDistanceBetween( me, marker.position ) * meters_to_miles;
  marker_position[i] = marker.position;
  
  //check if there a http request
  if ( document.URL.search('latitude') > -1 ){
  	$('#distance' + (i+1)).html(parseFloat(distance_between_search[i]).toFixed(2) + ' miles');
  }

  fit_all_markers_in_map.extend(marker.position);

  google.maps.event.addListener(marker, 'click', (function(marker, i) {
    return function() {
      infowindow.setContent(locations[i][0]);
      infowindow.open(map, marker);
    }
  })(marker, i));
}

map.fitBounds(fit_all_markers_in_map);

var zoom_and_pan_to_marker = (function(){
	$('tr').hover(function() {
		map.setZoom(15);
		map.panTo(marker_position[this.id-1]);
	});
})();

/*---------
	End
----------*/

</script>