Git Branches
Create a new branch and switch to it
Description: Create a branch named feature-login, move to it, add a file login.html and commit it. Then show all branches.
Hint:
git switch -ccreates a branch and moves to it.
Solution
$ git switch -c feature-login
Switched to a new branch 'feature-login'
$ echo "login" > login.html
$ git add login.html
$ git commit -m "Add login page"
[feature-login 001dfd4] Add login page
1 file changed, 1 insertion(+)
create mode 100644 login.html
$ git branch
* feature-login
main
How it works
- The new branch starts from the commit where you were, so it has all the old files.
- The new commit belongs only to
feature-login, andmainis not changed.
Read Git Branch and Git Checkout and Switch.
Merge a feature branch into main
Description: You have finished the work on feature-search. Merge it into main and show the history.
Hint:
- Move to the target branch (
main) first, then rungit merge.
Solution
$ git switch main
Switched to branch 'main'
$ git merge feature-search
Updating a17f7f4..d37f093
Fast-forward
search.html | 1 +
1 file changed, 1 insertion(+)
create mode 100644 search.html
$ git log –oneline
d37f093 (HEAD -> main, feature-search) Add search page
a17f7f4 Add home
How it works
mainhad no new commits, so Git made a fast-forward merge and moved the pointer.- After the merge, the branch
feature-searchcan be deleted.
Read Git Merge.
Resolve a merge conflict
Description: Both main and feature changed the first line of title.txt. Merge feature into main and solve the conflict by keeping the text “Learn Git Fast”.
Hint:
- Edit the file to the final text, remove the markers, then
git addandgit commit.
Solution
$ git merge feature
Auto-merging title.txt
CONFLICT (content): Merge conflict in title.txt
Automatic merge failed; fix conflicts and then commit the result.
$ cat title.txt
<<<<<<< HEAD
Learn Git Easily
=======
Learn Git Quickly
>>>>>>> feature
$ echo "Learn Git Fast" > title.txt
$ git add title.txt
$ git commit -m "Merge feature and solve conflict"
[main b4f0e8e] Merge feature and solve conflict
$ git status
On branch main
nothing to commit, working tree clean
How it works
- Git stops the merge and writes the markers in the file.
- We replaced the file content with the final text, so no markers are left.
git addtells Git the conflict is solved, and the commit finishes the merge.
Read Git Merge Conflict.
Delete a merged branch
Description: The branch feature-menu is merged into main. Show the merged branches and delete it.
Hint:
git branch --mergedlists merged branches.-dis the safe delete.
Solution
$ git branch –merged
feature-menu
* main
$ git branch -d feature-menu
Deleted branch feature-menu (was cda1bfa).
$ git branch
* main
How it works
- The safe delete
-dworks only when the branch is merged. Otherwise Git refuses. - The commit is not lost, because it is already part of
main.
Read Git Branch.
Rename a local branch
Description: Rename the branch fix-hedaer (a spelling mistake) to fix-header.
Hint:
- Use
git branch -m old new.
Solution
$ git branch -m fix-hedaer fix-header
$ git branch
fix-header
* main
How it works
-mmeans move (rename). Only the name changes, not the commits.- If the branch was pushed, push the new name and delete the old name on the remote.
Read Git Branch.