Commit, branch and merge with Git
In this task you'll record a change, try it on a branch and merge it.
Git records every change to a set of files as a commit, with who made it, when and why. A branch is a line of commits you can work on without touching main; merging brings the work back.
Small, frequent commits with clear messages make history useful: you can see exactly when something changed and undo it.
Example. A SS Kaiser developer fixes a typo in the checkout page on a branch called fix/checkout-typo, commits with the message 'Fix typo in checkout total label', and merges it into main after review.
Before you start
You need: Cloud Shell (Git is installed). Time: 20 minutes.
Where to look
- git init
- git add
- git commit
- git switch -c
- git merge
Steps
- Run
git config --global user.name "Your Name"and the same withuser.email.Every commit records this. Use the email your GitHub account knows. - Make a folder and start a repository:
mkdir shop && cd shop && git init -b main. - Create a file (
echo "<h1>SS Kaiser shop</h1>" > index.html), thengit add index.htmlandgit commit -m "Add home page". - Make a branch:
git switch -c feature/title, change the file, add and commit it.main hasn't changed: check withgit log --oneline main. - Go back and merge:
git switch main, thengit merge feature/title.git log --oneline --graphshows the history.
Commands
git init -b main
git add .
git commit -m "Describe the change"
git switch -c feature/x
git switch main
git merge feature/x
git log --oneline --graph
Watch out for
- Cost
- Free.
- Security
- Commits are permanent history: anything you commit, including a password, stays in it even if you delete the file later.
- Common mistakes
- Huge commits with messages like 'changes', and committing straight to main instead of a branch.
Clean up
Delete the practice folder: rm -rf ~/shop.
Words to know
- Commit
- A saved snapshot of changes, with a message.
- Branch
- A movable pointer to a line of commits.
- Merge
- Combining one branch's commits into another.
Tick off each step and track your progress in the guide.
Open this task in DevOps, Hands On