For anybody who's interested
First of all, you'll want to clone your remote repository. To do this use
git clone git@myPrivateGitURL
You can pull updates from that URL using
git pull
or push commits using
git push
Enough with the 'basics' already!
To reset your local branch back to the remote branch (delete any local changes you've made), use
git reset --hard
This is also really handy if you want to see where your local branch is at. Typing this will tell you the last commit message, and you'll be able to work from there.
If you want to go back one step i.e. undo a commit, use
git reset HEAD~1
the number after the tidle ~ refers to how many commits you want to step back (one in the case above)
To add a remote repository (e.g. someone elses)
git remote add remoteName git://url
where remoteName is the name you want to give this person's repository, and git://url is the URL for their repository
Then you'll want to fetch info from that repository. git fetch is like git pull, but doesn't merge to any branch. git pull is a git fetch followed by a git merge onto the current branch
git fetch remoteName
where remote name is the name of the remote repository you've just added in the previous command.
Now that the remote repository's commits are on your computer, you'll want to check out a branch of this remote repository
git checkout -b someonesBranch remoteName/remoteBranch
where remoteBranch is the remote branch you want to look at and someonesBranch is the name you want to give your local copy of this branch
the -b basically says "you'll need to make a new branch when checking this out". It also ties the two together for future pulls/pushes
You can now play with this branch, build, test etc. If it's good, and want to add it to the main branch, try
git checkout mainBranch
git merge someonesBranch
where mainBranch would be your main, local branch (connected to your git repository)
Note that git merge will add all the commits from someonesBranch to the main branch. You may only want to add a few of the commits, if so you can "cherry-pick" which ones. To avoid merge conflicts you must start with the earliest commit you want to add.
git cherry-pick SHA-commitDate-01/01/2010
Which will add one commit.
Note that you need to be in the branch you want the commits pulling to. e.g. I want to add commits from someonesBranch to mainBranch, so I must
git checkout mainBranch
first. You can pull commits from anywhere as long as you have the commit SHA (long 2342ecf9d98edcdfe9987a987ef897d thing)
To see the commit history or what the SHA is for a specifiec commit, see github or type
git log
on the branch you're interested in. It'll show you everything for as far back as you want To get out of this just use CTRL + Z like on any terminal process
If you get a warning about the automatic cherry-pick failing, then you have merge problems. What this means is that the file(s) in question have changed between your branch (mainBranch) and the commit you've just cherry picked. That is, there's a commit somewhere before 01/01/2010 that changes that file. Use
git status
to see what and where these conflicts lie.
If you want use a nice GUI to solve the merge conflicts
git mergetool
will let you use FileMerge (Mac OS X) to see the difference between two files and select which one is the best for your final commit
No problems? Solved that? You can cherry pick a commit from a later date now
git cherry-pick SHA-commitDate-10/01/2010
as long as there are no merge problems ;)
Now pushing may also be a problem. If you want to push to your remote repository remoteRepository, and more specifically to the branch remoteRepository/remoteBranch BUT your local branch you're going to push is not called the same thing as your remote branch, you'll get an error on pushing. What you need to do is
git push remoteRepository localBranch:remoteBranch
This is saying "push to remoteRepository my local branch to the remote branch"
If you do some silly things with your commits, like commit something wrong, or accidentally merge an already-merged commit, you will want to use
git rebase -i SHA-1
the -i basically opens up your preferred editor and makes changing commits easier. The SHA-1 is the commit before the bad one (i.e. its parent)
Once you've done this, you can amend commits using
git commit --amend
and finish the rebase using
git rebase --continue
If you've pushed to a remote branch on your repository for testing or pull request purposes, and would like to delete the remote branch, use
git push origin :nameOfBranchToDelete
If somebody else is playing with a remote repo that you have write access to, it's possible that a branch you've downloaded locally (say origin/myNewBranch
) has been removed from the remote. So now you have a local branch which points to nothing on the remote. You can remove these using git remote prune
:
git remote prune origin
If you want to do some proper house cleaning, then run:
git gc
See man git-prune
for more info