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 which installs a terminal version that is similar to linux one

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
 * public / private key - http://git-scm.com/book/en/Git-on-the-Server-Generating-Your-SSH-Public-Key
 * lot of patience
 * lot of beer (as you get more and more frustrated, drink it and git will look awesome to you again)

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 update 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

Diff
In order to see what you changed before you commit do:

git diff

In order to see what you changed after commit do:

git diff HEAD^1 git diff 
 * 1) compare current head with previous commit, that mean it compare what you just commited with what was there before you did that
 * 1) compare with any given id

Commit
Git needs to commit and push every change, unlike in svn, the commiting is done local and then it needs to be sent to server, that has some advantages and tons of disadvantages (git initial clone takes way longer, the repository is huge and hard to manipulate)

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 file2 git add file.cpp git commit git commit -a
 * 1) insert a file to commit
 * 1) in case you decided that you don't want to commit file2 you can revert the git add by doing git reset file2
 * 2) Or all pending changes with just: git reset  (but this won't change the files on disk)
 * 1) commit only the files which were marked with git add
 * 1) or commit also all files tracked by git which were changed

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

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

Squashing
Gerrit doesn't like too many commits in 1 push, so that you need to occasionally make 1 commit of multiple (that is called squashing). This is IMHO a step back because using this feature effectively kill the change history and purpose of versioning software at all. But this is just my opinion and it is not in my powers to convince gerrit maintainers to disable this requirement... So here is a guide

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 git-review -R
 * 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
 * Some repositories require git review

If you are using git-review you need a .gitreview file in a repo, if it's not there, you need to create it: [gerrit] host=gerrit.wikimedia.org port=29418 project= here you add a relative path example: labs/toollabs.git defaultbranch=master defaultrebase=0

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