Skip to main content

Git Branching- Learn how to manage multiple versions of a project with Branching.

  • Git Branch:
Up to this point, you've worked in a single Git branch called master. Git allows us to create branches to experiment with versions of a project. Imagine you want to create version of a story with a happy ending. You can create a new branch and make the happy ending changes to that branch only. It will have no effect on the masterbranch until you're ready to merge the happy ending to the master branch.


In this lesson, we'll be using Git branching to develop multiple versions of a resumé.
You can use the command below to answer the question: “which branch am I on?”
Command: git branch
Check what branch you are currently on. In the output, the * (asterisk) is showing you what branch you’re on.







The diagram above illustrates branching.
  • 1. The circles are commits, and together form the Git project's commit history.
  • 2. New Branch is a different version of the Git project. It contains commits from Master but also has commits that Master does not have.

  • Creating new Branch:

To create a new branch, use:
Command: git branch new_branch
Some points to be noted:-
Here new_branch would be the name of the new branch you create, like photos or blurb. Be sure to name your branch something that describes the purpose of the branch. Also, branch names can’t contain whitespaces: new-branch and new_branchare valid branch names, but new branch is not.
For example, here we are gonna create a new branch name "fencing".
Let's create the new branch: git branch fencing
Check the branches: git branch
  • Git checkout:

The master and fencing branches are identical: they share the same exact commit history. You can switch to the new branch with-
git checkout new_branch
You will be now able to make commits on the fencing branch that have no impact on master. Let's switch to the fencing branch.
git checkout fencing >> for changing from master to fencing branch;
git branch >> confirming that fencing in now the current branch you are working on;
  • Commit on a new branch:

After switching to a new branch, now we could add and commit our changes to the new branch. 











Exercise steps:
1. Print the Git commit log.
Notice the output:
  • The commits you see were all made in the masterbranch. fencing inherited them.
  • This means that every commit master has, fencingalso has.
Note: if you find that your cursor is stuck in Git log, press qto escape.
2. Make changes in any file.
3. Add modified file in the staging area.
4. Commit the changes to the repository (aka repo) with a commit message. 
  • Git Merge:

What if you wanted include all the changes made to the fencing branch on the master branch? We can easily accomplish this by merging the branch into master with:
Command: git merge branch_name
In a moment, you'll merge branches. Keep in mind:
1. Your goal is to update master with changes you made to fencing.2. fencing is the giver branch, since it provides the changes.3. master is the receiver branch, since it accepts those changes.
Exercise:
1. If you are on the fencing branch, Switch over to the master branch.
2. merge changes from fencing branch to master branch-
git merge fencing
Output:







Notice the output: The merge is a "fast forward" because Git recognizes that fencing contains the most recent commit. Git fast forwards master to be up to date with fencing.
  • Merge conflict:

The merge was successful because master had not changed since we made a commit on fencing. Git knew to simply update master with changes on fencing.
What would happen if you made a commit on master before you merged the two branches? Furthermore, what if the commit you made on master altered the same exact text you worked on in fencing? When you switch back to master and ask Git to merge the two branches, Git doesn't know which changes you want to keep. This is called a merge conflict.
Exercise:
1. Check if you are on the master branch.
2. Make some changes on the test file.
3. Add the edited file to the staging area.
4. Commit the changes to the repo with a commit message.
5. 
Imagine a few weeks have passed, and you'd like to develop your fencing branch some more.Switch back to the fencing branch.
6. From fencing branch, make changes on the same file you made changes on step 02.
7. Add the file to the staging area
8. Commit to the repo.
Let's say you decide you'd like to merge the changes from fencing into master.Here's where the trouble begins!You've made commits on separate branches that alter the same line in conflicting ways. Now, when you try to merge fencing into master, Git will not know which version of the file to keep.
Exercise:
1. Switch to the master branch.
2. Then merge fencing branch to the master branch.
3. 
In the output, notice the lines:CONFLICT (content): Merge conflict in testFile.Automatic merge failed; fix conflicts and then commit the result.





4. We must fix the merge conflict. 









look at the test file above. Git uses markings to indicate the HEAD (master) version of the file and the fencing version of the file, like this:




Git asks us which version of the file to keep: the version on master or the version on fencing. You decide you want the fencing version.
From the test file-Delete the content of the line as it appears in the master branchDelete all of Git's special markings including the words HEAD and fencing. If any of Git's markings remain, for example, >>>>>>> and =======, the conflict remains.
5. Add the changes to the staging area.
6. Commit the changes to the repo.
  • Delete Branch:

In Git, branches are usually a means to an end. You create them to work on a new project feature, but the end goal is to merge that feature into the master branch. After the branch has been integrated into master, it has served its purpose and can be deleted.
The commandgit branch -d branch_namewill delete the specified branch from your Git project.
Now that master contains all the file changes that were in fencing, let's delete fencing.
Exercise:Delete the fencing branch and see the output message. Now, verify that you have indeed deleted fencing by listing all your project's branches on the terminal.Notice in the output that only one branch, master, is shown.





Let's have a recap:
Git branching allows users to experiment with different versions of a project by checking out separate branches to work on.
The following commands are useful in the Git branch workflow.
1. git branch: Lists all a Git project's branches.
2. git branch branch_name: Creates a new branch.
3. git checkout branch_name: Used to switch from one branch to another.
4. git merge branch_name: Used to join file changes from one branch to another.
5. git branch -d branch_name: Deletes the branch specified.







Comments

Popular posts from this blog

Basic git commands to handle codes

Creating a Repository on GitHub: ------------------------------------------ 1. Sign in to GitHub 2. Click the "+" sign beside your profile and click "New Repository" 3. Provide repository name and other necessary things if needed. Pushing Code/project folder into GitHub Repository: ------------------------------------------------- 1. Go to the project location on your pc/laptop. 2. Enter into the Project. 3. Right click on mouse and select "Git Bash Here" 4. Run the following commands SEQUENTIALLY -            git init            git add -A ( add your files )            git status            git commit -m "Your Message" ( commit locally )            git remote add origin GITHUB HTTPS PROJECT LINK                                   ...

Basic Git Workflow- how it's work

Basic Git Workflow: Git is a software that allows you to keep track of changes made to a project over time. Git works by recording the changes you make to a project, storing those changes, then allowing you to reference them as needed. Turning a directory into a project>>  command: git init The word  init  means  initialize . The command sets up all the tools Git needs to begin tracking changes made to the project. Output: would be like-  Initalized an empty g it repository in /home/ccuser/workspace/sorcerers-code/.git/ By the above output, we could say that  The Git project was created. Git workflow: A Git project can be thought of as having three parts: 1. A  Working Directory : where you'll be doing all the work: creating, editing, deleting and organizing files 2. A  Staging Area : where you'll list changes you make to the working directory 3. A  Repository : where Git permanently stores those chan...