- Branching is where things start to get little complex for beginners.
- It happens because our mind has always been trained to think logic in a linear fashion and so is how we think in software development process.
- Before going on we should keep following things in mind.
- Branches are awesome.
- Branching allows us to play with the software while preserving the serious developments of software.
- Branches lets us collaborate with others. Everybody using our software can modify or fix bugs in our software to make it better and send back the changes for us to merge in the main software.
- Branching allows us to test for new feature addition and check if it doesn't break down the original code.
- Branching allows to create seperate flow of commits for everyone so that their work once pushed to the remote central repo, doesn't disturb the stable flow of commits and can be reviewed before final integration.
- last commit is said to be parent for the new commit. and the new commit point to its parent commit. View it here.
- Branches are merely pointers to a commit and can grow on seperately of the main development flow.image.
- two branches can point to same commit at a time. image
- A tag is similar to branch but one tag can point to a just single commit and remain pointing to that commit even when newer commits are made. Donot worry we will see it along other branch names in the history of branches section
- branch name cannot have spaces or double dots(..) in between their names.
you may be working on a new feature but realize that a critical error in your application needs to be fixed immediately. You can quickly create a new branch based off the version used by customers, fix the error, and switch back to the branch to which you’d been committing the new feature.
- Viewing local branches
- Current branch is the branch in front of asterisk(*) sign and colored.
git branch
- Viewing remote branches
git branch -r
- Viewing all branches(local and remote)
git branch -a
- creating branch without checking out there
git branch <branch_name>
- creating branch with checking out there
git checkout -b <branch_name>
- creating branch by defining its <start-point>.
git branch <branch_name> <start-point>
-Examples
git branch abc master~3
git branch abd b35d1c5
git branch BugFix-2132
- Checking out refers to moving on some node/commit in histroy, whether its on same or different branch.
- If we simply use branch name that will take us to latest commit in that branch.
- We have to use SHA-1 hash of the commit or we can use simply references.
git checkout <address/commit/branch_name>
=> btw branch name is also a referrence. Example:
=> git checkout master~3
- Deleting from remote:
git push --delete <branch_name>
- Deleting from local:
git branch -d <branch_name>
git branch -m <new_name>
- Viewing a single branch history is same as viewing history normally using
git log
commands.
git log --decorate --oneline
git log --graph --decorate
-
But what takes little efforts is to learn how to view history of all branches together or to view differences between two of them.
-
Full difference with relative commit times and branching.
git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative master..branch-X
- If above is a bit complicated then we can do alias in unix like systems.
alias diff-branches="git log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr)%Creset' --abbrev-commit --date=relative"
. Then do this diff-branches master..branch-X.
Now it's not complicated
- Basic difference between logs of two branches.
- We use 3 arguments to make it comfortable to read
--oneline
,--graph
&--decorate
. - We can use them seperately or merge them together in combinations
- only downfall of using this is the time visualization. that will be missing in comparision to above.
git log master..sandbox
git log --oneline --graph --decorate master..sandbox
- Visualizing all log with branching in terminal.
git log --oneline --graph --all --decorate
-> usually I prefer single line commits even for all with just branch's end information. git log --all --decorate
does that job for me.
- We can even limit the latest number of commits to be shown by just passing a numeral argument.
git log --oneline -5 master..refactor
- Merging is the process of importing commits/changes from other branch to the current branch.
- Usually every commit has one pointer to its parent commit. But if the merge commit is formed, it has 2 parent pointers to 2 branches(1. branch that is getting import, 2. branch that into which import is done.)
- Sometimes we see a message fast_forward_merge while performing merge. It means that seperate merge commit was made. Simply commits from the branch getting imported is added on top of the branch to which the import is being done.
git merge <branch_to_import>
- Before Handling conflicts, it is important to understand when does a conflict occur.
- Conflict does not occur when we add spaces in between code.
- Conflicts does not occur if we add more code to same lines.
- Conflict does not occur if we had new lines between code.
- Conflict does occur when in two seperate branches we edit the same line of code not neccessarily on same line number. That means:
- Adding different code words.
- Deleting different code words.
- Addition and deletion of words in same line.
For Example following mentioned code in same example are in two different branches but in same file and same position relative to other code.
Ex1: echo "Hello there";
&& echo "Hello there. My name is Gaurav";
does not produce conflicts.
BUT
Ex2: echo "Hello there";
&& echo "Hello Buddy!";
does produce conflicts.
- There are two way of handling conflicts.
- Manual Handling.
- Using mergetool
- It is difficult to explain conflicts in plane text. Below videos does wonderful jobs for the same.
Youtube - Jeff-Shantz: Manual Handling
Youtube - Apple Juice: Merge Tools
- As discussed previously, tags are just the constant pointers to a specific commit.
- Think of tags same as we think of milestones in any journey.
- We can define the milestones in the journey of software development.
- Usually people tag a particular commit as the software versions. See Semanitc software versioning here
- Adding tag:
git tag 0.0.1
- View Tag list on a branch
git tag
git tag -n
--> displays tag with first line of commit
- checking out to a tag.
git checkout 0.0.1