Version control
On this page 8 topics
Overview
Topics
- Repository
- commits
- staging
- branches
- merges
- conflicts
- .gitignore
Learn first
83 min of reading · 3 h of practice (estimate)
Learning outcomes
- Initialize a repository, stage changes, and write clear commit messages.
- Inspect history and differences with
log,status, anddiff. - Create, switch, and merge branches, and resolve a merge conflict by hand.
- Write a
.gitignorethat excludes build output and secrets.
Lessons
Lab and exercises
Lab
BEG-046-LAB1 · Version your work
Outcomes: 1–4 · Time: ~3 h Put the Bank Accounts project under Git with meaningful commits (at least five). Create a feature branch, make conflicting changes on both branches, merge, and resolve the conflict by hand. Add a .gitignore. Acceptance: clean history (clear messages, no target/ or .idea/); conflict resolved correctly.
Exercises (assessment: terminal-only Git task)
| ID | Outcome | Task |
|---|---|---|
| BEG-046-EX01 | 1 | In a new folder, initialize a repo, create Hello.java, stage and commit it with a clear message. |
| BEG-046-EX02 | 1 | Rewrite these bad messages as good ones: "fix", "stuff", "changes". Use a bug-fix example of your own. |
| BEG-046-EX03 | 2 | Change a file, then show the difference unstaged and staged. Which two commands did you use? |
| BEG-046-EX04 | 2 | List the last five commits in one-line form. |
| BEG-046-EX05 | 3 | Create branch greeting, commit a change, switch back, and merge with no conflict. |
| BEG-046-EX06 | 3 | Make both branches edit the same line, merge, and resolve the conflict. |
| BEG-046-EX07 | 4 | Write a .gitignore for a Java project; prove target/ stays untracked. |
Show solutions
- EX01
git init,git add Hello.java,git commit -m "Add Hello program". - EX02 e.g. "Fix negative balance after withdraw", "Add Account class with deposit", "Rename amount to balance".
- EX03
git diff(unstaged) andgit diff --staged. - EX04
git log --oneline -5. - EX05
git switch -c greeting, commit,git switch main,git merge greeting(fast-forward). - EX06 After
git merge, open the file, keep the intended text, delete the markers,git add file,git commit. - EX07
target/,*.class,.idea/,.env;git statusdoes not list them.
Quiz
Outcomes: O1 init, stage, commit · O2 inspect history · O3 branch and merge · O4 .gitignore.
A self-check for you: nothing is graded, saved or sent anywhere. Choose an answer, then check it. You can try again as often as you like.
Practical part
Terminal-only Git task: init, commit, branch, conflict, merge, .gitignore (EX01–EX07 and the lab). Pass: at least 8 of 10 quiz questions and a clean history with the conflict resolved.
Common mistakes
"fix" commit messages; committing target/ or .idea/; committing passwords.
Assessment
Terminal-only Git task.
Career relevance
Daily tool; team workflows in JNR-053.