Geek Culture
Published in

Geek Culture

Practical Git Lesson — Part II

Unsplash

Branching

  • A branch represents an independent line of development. Branches serve as an abstraction for the edit/stage/commit process.
  • We can think of them as a way to request a brand new working directory, staging area, and project history.
  • All the new commits are recorded in the history of the current branch, which results in a fork in the project history.
  • The git branch command lets you create, list, rename and delete branches.
  • It doesn’t let you switch between branches or put a forked history back together again.
  • For this reason, the git branch is tightly integrated with the git checkout and git merge commands.

We can find the current branch using the git status command and the default main is the default branch.

Now consider we are building a mobile app, and we are adding a new feature to our application. It is appropriate to create a separate branch to build the new feature, where we can keep track of all the updates related to this new feature alone and merge with the main branch after completion.

How to create a branch?

cmd: git branch feature_menu_bar

cmd: git status

We’ll check the current branch using the status command and then move to the newly created branch.

How to move to a new branch?

cmd: git checkout feature_menu_bar

Check out the command to switching to the new branch, checkout keeps the branch on the latest commit for that branch, like the head commit for the branch.

cmd: git status

How to move to the main branch again?

cmd: git checkout main

Let’s explore the new branch and commit a few things.

cmd: git checkout feature_menu_bar

cmd: nano file1

actions: Edit file1

actions: Save file1

cmd: git commit -a -m “Getting started with Menu Bar”

cmd: git log

The latest commit details are present in the new branch logs

We can switch to main to see what happened to its history using git log.

cmd: git checkout main

cmd: git log

The last commit will not be “Getting started with Menu Bar”. Since it was made in the feature_menu_bar branch.

How to create and switch to a branch using one command?

cmd: git checkout -b “feature_Sidebar”

cmd: git log

Note: the latest commit in this branch will be the same as the main branch because we created this branch while staying in the main branch. It is a version started from the main branch.

If we move to feature_menu_bar and then create the feature_Sidebar branch, then my latest commit would be taken from “feature_menu_bar” since I have started a version from it.

How to find all the branches of the repo?

cmd: git branch

It shows all the branches and the current branch with the symbol.

How to delete a branch?

cmd: git branch -D feature_Sidebar

Merging

As we can infer from the term, merging helps in merging multiple branches. A major challenge faced by programmers during merge is when a file in branch1 and the same file in branch2 changes as it results in conflict.

cmd: git checkout main

cmd: git status

Merge the feature_menu_bar to main. In feature_menu_bar, we have committed a file change in file1.

cmd: git merge feature_menu_bar

cmd: git log

Now, the branch history will be merged i.e. the latest commit in main would be “Getting started with Menu Bar”.

How to resolve conflict while merging?

  • First, we create a ReadMe file in the main branch and save it.
  • Commit the update made in ReadMe
  • Next, we switch to the feature_menu_bar branch.
  • Update the content of ReadME and save it.
  • Commit the update made in ReadMe
  • Switch to the main branch
  • Merge the feature_menu_bar branch with main.

Commands

cmd: nano ReadME

actions: Edit ReadME file

actions: Save the ReadME

cmd: git commit -a -m “Changes made in ReadME”

cmd: git checkout feature_menu_bar

cmd: nano ReadME

actions: Edit ReadME

actions: Save the ReadME

cmd: git commit -a -m “Changes made in ReadME”

cmd: git checkout main

cmd: git merge feature_menu_bar

If git can find the conflict, it tries to resolve it on its own using Auto-merging. If the same line in the same file is changed in both branches the git cannot rectify the conflict.

Changing same file & same line in both the branches

cmd: git branch

cmd: nano file1

actions: Edit file1

actions: Save file1

cmd: git commit -a -m “Changed line 1”

cmd: git checkout feature_menu_bar

cmd: nano file1

actions: edit line 1

actions: save the file

cmd: git commit -a -m “Changed Line 1”

cmd: git checkout main

cmd: git merge feature_menu_bar

Now, Auto-Merging fails. Conflicted files show us both the changes we have made and ask us how to handle them. We can edit it manually and then add and commit again. We can go back to the files and keep the changes required and update the file.

cmd: nano file1

actions: edit line 1

actions: save the file

cmd: git add file1

cmd: git commit -a -m “Conflict removed”

Thus, we have learned how to create a branch, how to merge two branches, and how to remove conflicts. Next, we will see how to work with remote repositories.

Working on Remote Repository

Let us clone the remote repository to the local system. The below command creates a folder with the repo name with all the files from the remote repository.

cmd: git clone url_of_the_repo_new_clone

cmd: git remote

It shows all the remotes to this repository’s origin. It creates a copy/originated from the remote repo.

Note: If you clone a repo from local repo (git clone /git_basics), then it isn’t a remote repository. We need to add the cloned repo to the remote first.

git_basics is the local repository.

cmd: git clone /git_basics git_basic_cloned // origin (name of the remote)

cmd: git remote

cmd: cd git_basics

cmd: git remote

cmd: git remote add our_clone /git_basic_cloned

cmd: git remote

It will help in making both the repo talk to each other.

Push and pull from remote

cmd: cd git_basic_cloned

cmd: git checkout -b notification_bar

cmd: nano file1

actions: edit the file

actions: save the file

cmd: git commit -a -m “change made in file1”

cmd: git push // Won’t do anything specific

cmd: git push notification_bar // edit to mention explicitly to which remote

cmd: git push origin notification_bar

Switch to git_basics and see if the commits made by the above branch is reflecting or not

cmd: cd git_basics

cmd: git branch

The notification_bar branch should be available in the git_basics repo as well. Changes made in the remote branch should be available in the local repo.

I hope you’ve enjoyed the practical guide on git. Thanks for reading. Enjoy the commits you make.

--

--

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store