Basic usage of Git

Documentation on Git

Apart from the reference documentation there are several resources on the web that can help you getting along with Git. Here are some examples:

When working with Eclipse you also have the option to use the Eclipse Git Team Provider EGit instead of a command line client. Detailed documentation on EGit can be found in the EGit User Guide.

Working with different branches

You can use the git checkout command to switch to another branch, e.g. to the exp-2.5.0 branch:

git checkout exp-2.5.0

Basic local workflow

Update the repository with remote changes

git pull --rebase

The --rebase option uses a rebase instead of a merge. This serves for you to have a nice linear history instead of loads of merge commits.

When updating the repository you should also update the submodules:

git submodule update

If you have made changes to the files in the repository you can check the repository state

git status # list files that have been modified/added/removed since the last commit
git diff # look at differences since the last commit

If you want to commit all of these changes, run:

git commit -a

and enter your commit message. This creates a local commit only. It hasn't been pushed to the server yet. If you only want to add changes from some files but not others, run:

git add file1 file2 file3 ...
git add -i file4 # if you only want to add some diff hunks and not others
git commit

and enter your commit message. After this, git diff will still show the uncommitted changes. While you're adding changes to be committed, you can run git diff --cached to see what's going to go in the next commit.

After you've made however many commits, you can push up to the server with:

git push

Gerrit will not allow you to push directly to the remote branch. Instead you have to push to a special ref named refs/for/$BRANCHNAME, $BRANCHNAME being replaced by the name of the branch.
For example, to push your current changes for the master branch you can use the following command:

git push origin HEAD:refs/for/master

To just use git push you have to configure the repository in the following way:

git config remote.origin.push HEAD:refs/for/master

Or, if you are working with the exp-2.5.0 branch:

git config remote.origin.push HEAD:refs/for/exp-2.5.0

Edit local commits

If you made a mistake in your last commit you can change it using

git commit --amend

You can use it to edit the commit message or you can modify the index and then include the changes in the last commit using this command.

To manipulate others than the last commit you can use an interactive rebase. Please note that you never should modify a commit that is already merged into the repository, but only commits that you have only locally or that are present as open changes in Gerrit.

As an example, to do an interactive rebase against the remote master branch use:

git rebase -i origin/master

An editor will open where you can reorder and manipulate the commits. For example if you are using reword instead of pick for a commit you can edit its commit message. If you use edit instead of pick you can amend the commit.

Oops, I added something to my last commit that shouldn't be in there!

No problem with Git - just reset the file(s) to the previous commit, then amend the current commit:

git reset HEAD^ -- <path>
git commit --amend

Make sure to replace <path> by the actual file path.