0
0
Gitdevops~5 mins

What a branch is (pointer to a commit) in Git - Commands & Configuration

Choose your learning style9 modes available
Introduction
When you work on a project, you often want to try new ideas without changing the main work. A branch in git is like a bookmark that points to a specific version of your project. It helps you keep track of different versions easily.
When you want to try a new feature without affecting the main project
When you need to fix a bug separately from ongoing work
When you want to share your work with others without mixing it with the main code
When you want to keep your main project stable while experimenting
When you want to organize your work into different tasks or ideas
Commands
This command lists all branches in your project and shows which one you are currently on.
Terminal
git branch
Expected OutputExpected
main * feature-1
This command creates a new branch called 'new-feature' pointing to the current commit. It does not switch to it yet.
Terminal
git branch new-feature
Expected OutputExpected
No output (command runs silently)
This command switches your working area to the 'new-feature' branch so you can start working there.
Terminal
git checkout new-feature
Expected OutputExpected
Switched to branch 'new-feature'
This command shows recent commits with branch names pointing to them, helping you see which commit each branch points to.
Terminal
git log --oneline --decorate
Expected OutputExpected
a1b2c3d (HEAD -> new-feature, main) Add new feature code 9f8e7d6 Fix typo in README
--oneline - Shows each commit in one line for easy reading
--decorate - Shows branch names and tags next to commits
Key Concept

If you remember nothing else, remember: a branch is just a name that points to a specific commit in your project history.

Common Mistakes
Creating a branch but not switching to it before making changes
Your changes will still go to the old branch, not the new one you created
After creating a branch, always switch to it using 'git checkout branch-name' before working
Deleting a branch without merging its changes
You lose the work done on that branch if it was not merged
Merge the branch into main or another branch before deleting it
Summary
Use 'git branch' to see all branches and which one is active.
Create a new branch with 'git branch branch-name' to mark a commit.
Switch to a branch with 'git checkout branch-name' to work there.
Use 'git log --oneline --decorate' to see commits and which branches point to them.