Branches and .gitignore
In Part 4, you connected your local repository to GitHub. In this final Git fundamentals post, you will keep main stable with branches and keep unnecessary files out of Git history with .gitignore.
What We Will Do
- Create and switch branches
- Merge branches
- Configure .gitignore
- Learn practical tips
Requirements
Lab 1: Understand Branches
What is a branch?
A branch is an isolated line of development inside the same repository. It lets you change files without affecting main right away, which is useful when you are building a feature, fixing a bug, or testing an idea.
Branches are local until you push them. In a team, that means different people can work on different tasks at the same time.
Example branches:
main: stable code
feature-login: building the login feature
bugfix-error: fixing a bug
The branch workflow
The basic idea is simple:
- Start from
main
- Create a branch for one task
- Commit your work on that branch
- Merge it back into
main when it is ready
This keeps unfinished work away from your stable branch.
Branch naming tip
Use short, descriptive names such as feature-login or bugfix-nav-error. Avoid vague names like test or work2.
Lab 2: Practice Branches
Check the current branch
First, confirm that you are starting on main.
Ready. Press Replay to run the scripted session.
The * marks the branch you are on.
Create a new branch
This creates the branch name but keeps you on main for the moment.
Ready. Press Replay to run the scripted session.
Switch branches
Now move into the new branch so your next commit belongs there.
Ready. Press Replay to run the scripted session.
git switch is the modern command. Older tutorials may use git checkout feature-login, which does the same job here.
Work on the branch
Create one new file and commit it on feature-login.
Ready. Press Replay to run the scripted session.
Go back to main
Switch back to main and inspect the working directory again.
Ready. Press Replay to run the scripted session.
Check the files
login.txt is missing because main does not have that commit yet. Your work is not lost. It is still recorded on feature-login, and it will reappear if you switch back to that branch.
Merge (Fast-forward)
Now merge the finished feature branch into main.
Ready. Press Replay to run the scripted session.
This is called a fast-forward merge because main did not move after you created feature-login. Git only has to move the main pointer forward to the newer commit.
Verify again
After the merge, main now includes the new file.
Ready. Press Replay to run the scripted session.
Delete the feature branch
Once a branch is merged, deleting the branch name is usually safe because the commits are already part of main.
Ready. Press Replay to run the scripted session.
If the branch was not merged yet, Git would usually warn you instead of deleting it quietly.
If a merge stops with a conflict
Sometimes both branches change the same line. In that case, git merge pauses and marks the file with conflict sections.
# open the conflicted file and choose the final content
git add <file>
git commit
You will learn more advanced conflict handling later, but this is the basic recovery flow.
Now that you know how to control what goes into main, the next step is controlling what should never be committed at all.
Why use .gitignore?
Some files should stay out of Git history:
node_modules/: large installed dependencies you can recreate later
.env: secrets or local configuration
*.log: temporary log files
.DS_Store: macOS Finder metadata
Create .gitignore
Create a .gitignore file and add a few common patterns.
Ready. Press Replay to run the scripted session.
Test ignored files
Create a log file and check whether Git tries to list it.
Ready. Press Replay to run the scripted session.
test.log does not appear because .gitignore is blocking new untracked files that match *.log.
Important caveat: .gitignore does not untrack files
.gitignore only affects files Git is not tracking yet. If you already committed a file, adding it to .gitignore later will not remove it from the repository by itself.
To stop tracking an already committed file, use:
git rm --cached secret.txt
git commit -m "Stop tracking secret.txt"
Commit .gitignore
After checking the patterns, commit the .gitignore file itself.
Ready. Press Replay to run the scripted session.
Lab 4: Five Essential Git Habits
1. Check status often
Use this before and after important commands so you always know what changed.
git status
2. Write meaningful commit messages
Good messages explain what changed, not just that something happened.
git commit -m "Add login validation"
3. Commit small, commit often
- One feature = one commit
- Commit several times per day
4. Review changes with diff
Look at your edits before you commit them.
git diff
5. Pull before you push
For beginners, use the simple version first.
git pull origin main
Some teams prefer git pull --rebase, but that is an advanced history-cleanup workflow you do not need yet.
Bonus Tips
6. Resolve conflicts carefully
# edit conflicting files
git add <file>
git commit
Use this only after you read the conflict markers and decide what the final file should contain.
7. Stash temporary work
git stash is useful when you need to switch branches before you are ready to commit.
git stash
git stash pop
8. Undo the last commit safely
If the commit was already pushed, use revert. If it was not pushed yet, reset --soft is often a better fit.
git reset --soft HEAD~1 # undo last unpushed commit, keep changes staged
git revert HEAD
git revert HEAD creates a new commit that reverses the previous commit. It does not erase history.
9. Compact graph view
This helps you see branches and merges in one screen.
git log --oneline --graph --decorate --all
10. Ask Git for help
git help <command>
Troubleshooting
This usually happens when your local repository and remote repository started with different first commits.
Fix:
git pull origin main --allow-unrelated-histories
"error: failed to push"
This usually means the remote branch has newer commits that you do not have locally yet.
Fix:
git pull origin main
git push origin main
If Git reports a conflict during the pull, resolve the file first, commit the merge result, and then push again.
Accidentally committed a sensitive file
Adding the file to .gitignore prevents future tracking, but it does not erase the file from earlier commits.
Fix:
# Add to .gitignore
echo "secret.txt" >> .gitignore
# Remove from the index
git rm --cached secret.txt
git commit -m "Remove secret file"
If the file contained a real password, token, or private key, rotate that secret immediately.
Wrap-Up
You now know the core Git workflow: create a repository, commit changes, connect GitHub, branch safely, and ignore files that should stay local.
What you learned:
- Why Git is necessary
- Installation and basic setup
- Create a repo and commit
- Connect GitHub
- Branches and .gitignore
Next topics to explore:
- GitHub collaboration: Pull Requests, Code Review
- Advanced Git: Rebase, Cherry-pick
- CI/CD: GitHub Actions
Git Series Overview
Practice Checklist
💬 댓글
이 글에 대한 의견을 남겨주세요