User:Petrb/Git for idiots

Because nearly every time I work with gerrit I feel like an idiot, I decided to make this simple guide for git and gerrit. In case you feel same stupid as I do, it may be useful for you :-) feel free to update this page, but try to keep it stupid people friendly :)

Please note: If you really need to use this guide to understand git, it doesn't imply you are an idiot, rather it implies that git is complicated ;)

Windows
I don't use windows that much, however you should try http://git-scm.com/download/win

Linux
For a weird reasons the git package in debian and ubuntu is called "git-core" and git doesn't link to it. Getting git-core will install it.

On other distro's visit git-scm.com

Setting it up
You need 4 things in order to push
 * account on wikimedia gerrit
 * e-mail
 * nickname
 * internet

you can configure your username and e-mail by typing (please use your own mail and name :D): git config --global user.email name@blah.com git config --global user.name Name

Everytime you clone, use ssh and prefix url with username@, eg. ssh://petrb@wikimedia.org or you won't be able to push

Checkout or update
Git doesn't have checkout so simple as svn had, the git's checkout is divided to many types:

git clone git pull git pull --rebase
 * initial checkout (svn checkout) - called "clone" it get a repo from remote server and save it to local folder
 * update (svn up) - called pull
 * update which automatically merge stuff - called rebase, update the repository and merge it with your commits

Commit
Git needs to commit and push every change, unlike in svn, the commiting is done on local level and then it needs to be send to server, that has some advantages and tons of disadvantages

Unlike in svn you need to specify which files will be commited. Even if you change the file it won't be commited unless you tell git to do that. You can use command git add in order to make it commit the changes git add file.cpp git commit
 * 1) insert a file
 * 1) commit


 * or

git commit -a

In case you want to change the commit you made, do commit --amend

Now when you commited the change, you may be ready to push it. But that's where the pain begins :)

Pushing
This is a biggest evil of git for us, idiots :)

When everything is fine
Ideally you type  and everything works. Unfortunately, that works rarely.

When it's not fine
scp -p -P 29418 username@gerrit.wikimedia.org:hooks/commit-msg .git/hooks/ git commit --amend git pull --rebase git push origin HEAD:refs/publish/master git push-for-review-production
 * You get rejected because you don't have permissions:
 * You may have missing username in url, everytime you ssh somewhere, you need to specify your username (petrb@server.com)
 * You checked out using http
 * You can change the url in .git/config
 * You really don't have permissions
 * You need to contact the owner of repo
 * You are missing some hooks
 * Gerrit requires the commit-msg hook
 * There were some updates and you are not up to date
 * You are using wrong command to push
 * Sometimes it's needed to use following, should you know why, feel free to explain it:
 * Some repositories have own system

Reseting the repository
Git makes it very hard to revert the changes or reset the repository to original state as it was when you cloned it. In svn, you can simply delete the files and run svn up which restore them. In git doing so has no effect.

There are many ways to reset the repository to original state, but I found it simplest just to delete it and clone it again. In case it's a huge repository, you probably don't want to waste time and try these:

Sample
svn checkout - clone git config --global user.email benapetr@blah.com git config --global user.name Petrb git clone ssh://petrb@gerrit.wikimedia.org:29418/mediawiki/core.git cd core scp -p -P 29418 petrb@gerrit.wikimedia.org:hooks/commit-msg .git/hooks/ git commit -a git push
 * 1) configure git
 * 1) get a repo
 * 1) now get a hook
 * now, do some changes and commit them:
 * 1) now push them