Merge
-
Merge is done when changes from one branch has to be brought to other branches.
-
Merges can be of two kinds
-
Fast-Forward:
- No Extra commit gets created
- When the Parent branch has no changes and if we try merging child to parent branch
- Can be skipped
-
Merge:
- Extra Commit with two parents will be created
- In all the other cases apart from Fast Forward
-
-
Lets continue from previous repository created from here
-
Merge the changes from sprint-1 to master.
-
Note that there were no changes in master branch after sprint-1 branch was created, now merging will lead to fast forward
-
Lets merge
# move to the destination branch (master)
git checkout master
git merge sprint-1
- Create a branch called as sprint-2 from master
git checkout -b sprint-2
- An Important fix from sprint-1 is done on master.
git checkout master
echo "Updated Docs of Sprint1" >> Readme.md
git status
git add .
git commit -m "added imp fix"
- Continue working on sprint-2 branch
git checkout sprint-2
echo "sprint-2" >> src/main.py
echo "sprint-2" >> test/main.py
git status
git add .
git commit -m "features in sprint 2"
- Now try to merge the changes from sprint-2 to master
git checkout master
git merge sprint-2
-
New commit gets created and HEAD & Master will be pointing to this new commit.
-
Create a new branch called as sprint-3.
git checkout master
git branch sprint-3
git checkout sprint-3
Rebase
- Now add some changes for sprint-3 branch
- Now an important bug fix is done on master, which is required for sprint-3
- Now to rebase from master onto sprint-3, execute the following commands
git checkout sprint-3
git rebase master
- References:
Merge-Conflict
-
Merge Conflicts can happen during
- merge
- rebase
- pull
-
Lets create a child branch from master and call it as poc-qt
git checkout master
git checkout -b poc-qt
- Now add the following to src/main.py at the end
working for qt
- Now master branch has one commit which changes in src/main.py.
- In master branch main.py has the following contents
Sprint 1 Started
SPrint-2
testing is in progress
need to add many comments
- In qt-poc branch main.py has the following contents
Sprint 1 Started
SPrint-2
working for qt
- Conflicts has to be resolved manually.
Viewing Differences
- To view differences, use
git diff
- Try the following commands
# make local changes on Working tree
git diff
git diff <commit-id-A> <commit-id-B>
git diff <branch-A> <branch-B>
How is git Working
- Git is internally a very simple content tracker
- To understand this, lets create a new folder called as understanding-git
mkdir understanding-git
cd understanding-git
git init
- Now lets get into .git folder and see the contents
- lets make our first change
mkdir src
touch src/main.py
git add .
git commit -m "Commit-1"
git log
- Now lets make other change
mkdir test
touch test/main.py
git add .
git commit -m "Commit-2"
git log
- To understand how git works , we need to understanding Hashing SHA1
- Git Uses hashing (SHA-1) to calculate commit ids. Commit id is hash of many things.
- To understand contents of commit
git cat-file -p <commit-id>
- Lets examine commit-2 contents
- Take a look at below image