Skip to content

Instantly share code, notes, and snippets.

@JadedEvan
Created July 25, 2012 17:23
Show Gist options
  • Save JadedEvan/3177374 to your computer and use it in GitHub Desktop.
Save JadedEvan/3177374 to your computer and use it in GitHub Desktop.
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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment