Git: Branching

Another important topic in Git – Branching.

Branching is a method of moving into a new direction when creating a new feature, and only combining this new feature with the main part of the code (the main branch) once you’re finished.

HEAD determines which branch you are currently looking at and that could be checked by using “git branch” command:

By default, the HEAD is pointed at the master (main) branch:

To create a new branch: “git checkout -b <new_branch name

  • -b: to create a new branch
  • git checkout <branch_name> – to switch between branches

Now, in the new_feature branch I will change the test_file.py by adding function:

at the same time, the main branch still has the original file:

Push new branch via pulling request and merge:

$  git push --set-upstream origin new_feature
Enumerating objects: 5, done.
Counting objects: 100% (5/5), done.
Delta compression using up to 16 threads
Compressing objects: 100% (3/3), done.
Writing objects: 100% (3/3), 363 bytes | 363.00 KiB/s, done.
Total 3 (delta 1), reused 0 (delta 0)
remote: Resolving deltas: 100% (1/1), completed with 1 local object.
remote:
remote: Create a pull request for 'new_feature' on GitHub by visiting:
remote:      https://github.com/dagolovach/super-duper-octo-enigma/pull/new/new_feature
remote:
To https://github.com/dagolovach/super-duper-octo-enigma.git
 * [new branch]      new_feature -> new_feature
Branch 'new_feature' set up to track remote branch 'new_feature' from 'origin'.

Now, we are going to change the main branch file and try to merge branches.

Main branch test_file.py:

New_feature branch test_file.py:

Note: add/commit and push should be done in each branch

Merging Branches

When we’re ready to merge our two branches together, we’ll check out the branch we wish to keep (almost always the master branch) and then run the “git merge <branch_name>” command .

Git will treat it similarly to a push or pull, and merge conflicts may appear.

Before we can push the merge – we need to fix conflicts (that is just because we changed same lines) and ‘git push

Share

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *