Main branch keeping a clean history of releases
Room for experimentation without breaking anything
Simultaneous work for many people
Easy to reverse work on a feature and continue with main
(Source: Git Better)
Recall master
or main
branch is an indicator
You create a new branch using git branch <branch-name>
The new branch is a new indicator with its own staging area
and working directory
Scroll down to see what happens step-by-step
main
Recall: HEAD
points to 'where we currently are'
main
points to the latest commit on branch
git branch feature
This creates a new indicator pointing to where we are.
But HEAD
still points to the main
branch.
git checkout feature
to get to the new "feature" branchThe HEAD
indicator goes to the feature
branch.
mkdir RR_git3
cd RR_git3
git init CentralRepo --bare
git clone CentralRepo Dev1
git clone CentralRepo Dev2
dev1
create a short history of commits (minimum two) and push itcd dev1
echo "a first file" > file1.txt
git add .
git commit -m "Added a first file"
echo "a second file" > file2.txt
git add .
git commit -m "Added a second file"
git push
dev2
pull the informationcd ../dev2
git pull
feature1
cd ../dev1
git branch feature1
git branch
option to do that)git branch -l
git checkout <branch-name>
)git checkout feature1
You can add commits only to one of the branches at a time.
When you switch between branches, Git 'refreshes' your staging area and working directories to match HEAD
.
In a big project, e.g. if you're the one working on the feature
, changes to main
might not be yours (other people push their features/fixes, etc. in the meantime).
feature
branch with git checkout feature
main
with git checkout main
feature1
branch, add a file, stage and commit it. Then do a push
.echo "some text" > file_on_feature1.txt
git add .
git commit -m "First file on feature1 branch"
git push
Read the message and follow the hints.
git push --set-upstream origin feature1
main
branch and notice what's going on in your working directory.ls
git checkout main
ls
(or look at it in Explorer/Finder)
echo "some other text" > some_file.txt
git checkout feature1
What happens? (put it somewhere as part of a new commit and push it)
git add .
git commit -m "New file added to history"
git pull
and check git branch -l
cd ../Dev2
git pull
git branch -l
git checkout feature1
and check git branch -l
againgit checkout feature1
git branch -l
main
branchgit checkout main
echo "another text" > file_dev2main.txt
git add .
git commit -m "A file added to main by dev2"
feature1
branchgit checkout feature1
echo "another text" > file_dev2feature.txt
git add .
git commit -m "A file added to feature1 by dev2"
git status
on the main
branch, push the changesgit checkout main
git status
git push
git status
git status
on the feature1
branch, push the changesgit checkout feature1
git status
git push
git status
You might recall merge
from how we united main
with origin/main
.
main
do git merge feature
Recall: merge
creates a new commit.
feature
branchIt doesn't include the merge (unless you also do git merge main
while on the branch feature
).
Can you trace what's been going on in this project?
(Source: Git Better)
cd ../dev1
git pull
git checkout main
git pull
feature1
branch to the main
branch using git merge <branch-name>
. Push.git checkout main
git merge feature1
Conflict resolution is the same as in the last week's case.
git push
Rebasing is making your branch start from a different point
git checkout feature
)git rebase main
Important: rebase
destroys original commits and creates new ones! Probably not a good thing if the original ones were shared with others or in a remote repository!
Assume you start with this:
feature1
branch proved a dead end, but feature2
is promisingfeature2
is actually unrelated to feature1
main
branch introduced something relevant to feature2
feature2
to main
git checkout feature2
git rebase --onto main feature1 feature2
`git rebase –onto <new-base> <old-base> <moved-branch>
Can you see how rebase
could come in handy for the lower feature
branch?
(Source: Git Better)
Read Merging vs rebasing on Atlassian for a nice description of both approaches (also their individual pages).
Also, take a look at git bisect
, to understand when rebasing might be helpful.