JadedEvan
7/25/2012 - 5:23 PM

Library of useful git commands

Library of useful git commands

# Git Notes

## Merge Two Branches in seperate repos
	
I have two independent repositories (A and B) and would like to merge one repo. (B) into another one (A) with keeping the whole history of both. What do to?

	% cd projectA
	% git remote add test ../path/to/other/repo && git fetch test
	
Adds a new branch called _test_ which pulls in ALL branches from the /other/repo. _git fetch test_ pulls code for all branches
	
	% git branch -r
	
Show the remotely tracked branches (what we just added above)
	
	% git merge test/name-of-branch 
	
!Important - be sure to provide the name of the branch that you want to merge in. I fooed up the first time I did this with the Sails project and merge the master branch, not january-second!
	
	% git push (if you use a remote repo.)
	
Yay
	
## Create a new branch and have it track remote commits

	% git pull (get the latest)
	% git branch --track january-second origin/january-second

### Push a remote branch from a new local branch

	% git push origin nameOfNewBranch
	
Similary, to delete a new remote branch
	
	% git push origin :nameOfNewBranch
	
But also remember to delete the branch locally as well.

### Update a local branch to track a remote repository

	git config branch.local_branch_name.remote your_remote
	git config branch.local_branch_name.merge refs/heads/remote_branch_name

* _your_remote__ is usually called _origin_, particularly for GitHub users.
* _local_branch_name_ refers to the local branch you're wanting to set up to track the remote branch.
* _remote_branch_name_ is the remote branch that the local branch will track.


### Generate a patchfile with git

	git diff --no-prefix > patchfile

Then apply the patch:
	
	patch -p0 < patchfile
	
### Stash unsaved changes within a commit

While working on a project, you need to switch to a clean copy and make an immediate bug fix. Rather than discarding the changes or prematurely committing them, you can stash the changes temporarily and re-apply them when ready:

	% git stash save "Your message here"
	
	% git stash list
	
Shows you the saved stashes

	% git stash apply
	
Applies that most recent saved stash to the repo

### Generate and apply patchfile from two different commits

Say you're working on a local branch called _experimental_, which is a branch of _master_. Someone pushes some fixes to _master_ which you want to incorporate immediately into your _experimental_ branch. You can generate a patchfile between two commits and apply those to your new branch.

	% git checkout master
	% git pull master
	% git diff --no-prefix HEAD^1 HEAD > /path/to/patchfile.txt
	% git checkout experimental
	% patch -p0 < /path/to/patchfile.txt
	
I might suggest doing a google search on the _--no-prefix_ and _-p0_ switches in some of the steps above. Explains relative path handing within the patch file.


### Delete a remote branch

	% git push origin :branch-name

### Delete a remote tag

        $ git tag -d 12345
        $ git push origin :refs/tags/12345
	
### Revert _master_ to a prior commit

	% git reset HEAD^ --hard
	% git push mathnet -f

Where HEAD^ represents the history of the branch. HEAD^2 goes back to, HEAD^1 goes back one. Optionally, you can specify a commit SHA instead.

### Show files in a commit:

	% git show --name-only SHA
	
### Show the difference between current file and a commit

	$ git diff COMMIT_HASH path/to/file.rb
	
### Show history for a single file

	$ git log -p -- FILENAME
	
### Find all commits containing changes to {FILE}

	$ git log -- {FILE}