Git

Our git workflow is based on this article. In addition, we follow the angular git commit guidelines which can be followed with theĀ commitizen cli.

Getting Started

There are many tutorials and cheat sheets available to give you a basic overview about git.

  1. Cheat Sheets (git tower, github)
  2. git book

This article will help you with the command we use most often. For further information, be sure to consult the posted book or search the web for specific issues.

Best Practices

  • Never commit on the master directly.
  • Use a new branch for every connected feature.
  • Make as many commits as possible and push them immediately.
  • Create pull requests as soon as possible.
  • Delete branches after you accepted a pull request.
  • Delete your branches locally if a pull request has been accepted.
  • Prune branches to stay on top of things (git remote prune origin).

Create a new branch

We always work on topic or feature branches that were created off of the current, up-to-date master branch.

git checkout master
git fetch
git merge origin/master
git co -b {topic}

Work on a branch

  • When you are working on a feature/bug/etc. make sure you are on the specific branch for it.
  • Always make sure you do not commit unused or unnecessary code (use git diff before you commit).
  • Use meaningful commit messages / follow the angular git commit guidelines.
  • When you review every change you made (git add -p), stage with y or do not stage with n.

There are several ways to stage/add/commit your changes.

  1. The easiest one just commits every change you have made in the project:
git add .
git commit -m '{meaningful_commit_message}'
  1. To commit only modified files, not new ones:
git commit -am '{meaningful_commit_message}'
  1. To commit only modified files and review the change or commit only certain parts of them:
git add -p [{filepath}]
git commit -m '{meaningful_commit_message}'
  1. To commit specific files only (new or modified):
git add {filepath}
git commit -m '{meaningful_commit_message}'

Merge a branch

If you merge a branch with git merge {branch} there might be merge conflicts.
Solve all conflicts and commit with the automatically generated commit message.

git add . (or) git add -p
git commit -m (or) git commit --continue

This might open vim if that is your default editor. To save and quit type :wq and press the enter-key.

Push a branch

If a branch is ready to go through review and to be merged do the following:

git checkout {topic}
git fetch
git merge origin/{topic}
git merge origin/master
git push origin {topic}

Pull request

Create a pull request if you want your changes to be merged into the master.
After the pull request was accepted:

git checkout master
git fetch
git merge origin/master
git branch -d {old_topic}

Rewriting history

Most of the things you do with git are harmless. However, you have to be careful when ‘rewriting history’. Especially if the branch you are working on was already pushed to origin. You will get an error when pushing the branch to origin the next time.

Commands that do alter the commit history are:

  • git commit --amend
  • git reset
  • git rebase

and some others, that if you do not know about them, you shouldn’t ;).

Helpful commands

git bisect

git can also be used to find the last working version of a repository. This is a whole topic in itself. For example see git-scm.com/docs/git-bisect for more details.

The basic gist is you run git bisect start at the current revision. If this commit is broken, run git bisect bad. Then checkout one commit which you know was good git checkout abcdef1234 and run git bisect good. From that point on, git will check out different commits. Everytime you have to either run git bisect good or git bisect bad to continue. When you are done, it will tell you the first commit that is broken.

git add -i

TODO: add description