November 20, 2011

Git Reference Sheet

Git Repository

It contains a set of 'commit objects' and 'heads' which are references to those commit objects. Commit objects are a set of files that reflect the state of project at any given time. They also include references to parent commit objects. Head is a reference to a commit object. a head has a name. By default, every repository has a head called "master". A repository can contain many heads but at any given time, one head is selected as the "current head" which is always named as "HEAD".

(In simple terms, HEAD is the current branch. But there are exceptions to this. Another definition is to refer to HEAD as a reference to the last commit in the current checked out branch.)

Delete

delete a remote branch
git push git@github.com:....git :mahee2

delete a branch

git branch -d mahee

Push

Push is done when you want to push the work you have done upstream for sharing purposes.

General command is git push [remote-name] [branch-name]

If you want to push your master branch to origin server, run git push origin master

example : git push git@github.com:charithsoori/Cart-beta.git mahee

The real scene behind above commands are a little explanatory. when you give a command as git push origin mahee it means, to take the local branch named 'mahee' and push it to the remote 'mahee' branch. git push origin mahee:mahee does the same. (Take my local 'mahee' and make it remote 'mahee'). If it is a different branch in remote (e.g. test) just give the command as git push origin mahee:test

if above push doesn't work due to not having a common ancestor and you get the following error :-

To git@github.com:.....git
! [rejected]        mahee -> mahee (non-fast-forward)
error: failed to push some refs to 'git@github.com:charithsoori/Cart-beta.git'
To prevent you from losing history, non-fast-forward updates were rejected
Merge the remote changes (e.g. 'git pull') before pushing again.  See the
'Note about fast-forwards' section of 'git push --help' for details.


Do force push using
git push --force
or
git push git@github.com:.....git mahee --force (untested)

Actually force is not a very good option and is not encouraged. if a simple git pull and then pushing doesn't work, then you might have to use force.

Fetch

to pull any new work since clone or last fetch

git fetch origin

here "origin" refers to the remote repository when a clone is taken. The fetch will pull all the data from the remote project which isn't currently available. after this fetch, references to all branches are available and it is possible to merge branches or inspect them at will. Fetch only pulls data to local repository. It does not do any merging or modification to current work. Those should be done manually.

Pull

git pull

If a branch is set to track a remote branch, you can use pull to automatically pull and merge a remote branch to the current branch.

When to fetch and when to pull?

http://longair.net/blog/2009/04/16/git-fetch-and-merge/

Merge

git merge local mahee

merges local and mahee branches to local.

also, another method to do this merging is to checkout to local and then call on git merge mahee

Conflicting merge

git checkout branch1
git fetch origin branch2
git merge FETCH_HEAD

Auto-merging ......
CONFLICT (content): Merge conflict in ...........
Removing ..........
Auto-merging .........
CONFLICT (content): ..........
Automatic merge failed; fix conflicts and then commit the result.

when above output is received you can use a mergetool and perform merging in a little bit of a manual way.


git mergetool
merge tool candidates: opendiff kdiff3 tkdiff xxdiff meld tortoisemerge gvimdiff diffuse ecmerge p4merge araxis emerge vimdiff
Merging:
.........
.........

Normal merge conflict for '....':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (kdiff3):

Normal merge conflict for '.........':
{local}: modified
{remote}: modified
Hit return to start merge resolution tool (kdiff3):


References

No comments:

Post a Comment