Branching in Git
Branching allows developers to work on new features or fixes without affecting the main branch.
// Setting up a project
mkdir project
cd project
git init
echo newfile > file
git add file
git commit -m "new file added"
git log
Creating a new branch
git branch newfeature
git status
git branch # To view all branches
Switching branches
git checkout newfeature
Visualizing Branches & Commits
git log --decorate --graph --oneline --all
Note - The HEAD
is where Git is pointed at right now in your repository, and the branch is where that branch reference is pointed to.
Detached Head
Reference - https://youtu.be/GN36mrrM12k?si=4zgxsh094rcw6wfF
Normally, HEAD
points to a branch but it can also point to a specific commit, making it “detached”
Lets take a example -
git checkout d6afca4 # HEAD being detached
Make changes to a file and commit it
git add .
git commit -m "Changes made on Detached Head"
Check the Changes
git log --graph --oneline --all
We can go back to master leaving this commits not connected to a branch behind and they may get deleted
git checkout master
Deleting a branch
git branch -d newfeature # Only deletes if merged
git branch -D newfeature # Force delete (even if unmerged)
git push origin --delete newfeature # Deleting a remote branch
Tags
They point to particular commits and just used as fixed labels on commits.
git tag v1 # Tags current commit as v1