Git: history and changes
Since topic 1 you have finished every task with a commit. Each commit is a snapshot of the project with a message. This topic already has several, so it is time to use the history.
The history
Alt+9 opens the Git window, the Log tab. On the left is the list of commits, newest at the top. Click Reformat code: on the right you see which files changed. Double-click main.c and a diff opens: what it was on the left, what it became on the right.
Compare it with Start counter at 0: one line changed there. That is why it pays to do one task in one commit: a month later you see straight away where the bug was fixed.
Before a commit
Ctrl+K shows all the changed files. Double-click a file and look through exactly what you are about to save. There must be no build/ or .exe in it: .gitignore keeps them out.
Undoing
- Not committed yet: right-click the file in the Commit window → Rollback. The file goes back to how it was in the last commit.
- Already committed: right-click the commit in the Log window → Revert Commit. A new commit is made that undoes that change, and the history stays.
- Who changed a line, and when: right-click the gutter → Annotate with Git Blame.
A good commit message
The message says what the commit does and, if it isn't obvious, why. In this course it is short, in English and in the imperative, as if finishing "This commit will ...": Add grade input, not Added grade input. About 50 characters at most.
| Good | Bad |
|---|---|
Start counter at 0 |
fix |
Check input in a while loop |
changes |
HW 2: Round with (int)(x + 0.5) |
asdf |
Push
Commits are kept on your computer. Push sends them to GitHub: Ctrl+Shift+K. Only then does GitHub run the tests and the lecturer see your work.
The same in the terminal
Git commands work in CLion's terminal (Alt+F12) too:
| Command | What it does |
|---|---|
git status |
what changed but isn't committed yet |
git log --oneline |
the history, one line each |
git diff |
exactly what you changed |