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
% git pull (get the latest)
% git branch --track january-second origin/january-second
% 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.
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.
git diff --no-prefix > patchfile
Then apply the patch:
patch -p0 < patchfile
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
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.
% git push origin :branch-name
$ git tag -d 12345
$ git push origin :refs/tags/12345
% 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.
% git show --name-only SHA
$ git diff COMMIT_HASH path/to/file.rb
$ git log -p -- FILENAME
$ git log -- {FILE}