Git basics [Beginner Mode] Part 2

Image by Roman Synkevych on Unsplash

In the last story, we created a new repository in GitHub and pushed our code to the main branch. Now in this story, I am gonna walk you through the process of looking back in your development and testing new features in your project.

You would’ve updated your project and pushed it to your repo so many times by this time. Each commit you made is a snapshot(or a version) of your project. Now, let’s say you wanna look into an old snapshot for a particular line of code, generally, you will go to GitHub and click commit history to view the changes but how can do that in the command line?. Let’s start with some logs…

git log

This command gives you a detailed list of commits that you or your collaborators made. Each commits contains a random alphanumeric string with a length of 40 characters, this is called a hash and this is used to separate each version and can be used to point to a certain snapshot of project life.

Git uses SHA-1 to generate hash

A single log consists of hash, tag(if present), author, author email, date, and a commit message.

commit <commit hash>
Author: <author name> <author email>
Date: Thu Jun 24 09:34:18 2021 -0700

Commit message

Eg:

commit 7abd5964ad8ef67ac20f5c52eea34e0d3bebe110
Author: hiruthicShaSS <blablabla@gmail.com>
Date: Fri Jun 25 16:42:07 2021 +0530

Opening articles in new tab and card title fixed

All right now you can see all the commits you made in the past but this can grow larger as the project develops and having more collaborators. So to get only the recent commits you can specify the number of commits you want to see by adding a number.

Eg: git log -3

This will show only the last 3 commits.

git log --after="2021–5-1"

This will show all the commits after May 1, 2021. You can replace “ — after” with “ — before” you want to filter in backward or you can use both to get the commits made between certain dates.

But all these logs are so big what if you want just the basic info…

You can use git log --online which will show each commits in one line.

<last 7 characters of commit hash> <commit message>

Eg: 7abd596 Opening articles in new tab and card title fixed

Maybe you don’t want so little information, maybe you want some more data of the commit like tags, branch, etc.

git log — oneline — decorate

This will show the log as one line but with some branch and tag info.

Or if you want a non-developer form of log use “git shortlog".

All right, now we have seen our past versions of our project, now let’s create new branches and work on multiple branches.

A branch is a clone of the current branch with a new name. It will retain the currently committed version of the project and its old commits.

Lets check what are the existing branches.

git branch

You will probably see one line in green saying “main". Now the main branch is fine but as its name says it is the main branch so it will contain the most stable version of your project. It is recommended that you use a separate branch for development and testing. So let’s create a new branch

git branch development

Replace “development” with whatever branch name. Branch names shouldn’t contain spaces and can contain hyphens and underscores.

Now if you type “git branch" you will your new branch listed but still, the main branch is selected(green text and has a '*' at the start of the branch name). To go to the new branch we use,

git checkout development

Now there is one more command that you can use to create a new branch and automatically checkout to the branch after creating.

git checkout -b development

The above command will automatically create a new branch and checks out to that branch once complete.

To be honest I never use git branch <name>. I always just use git checkout -b <name>.

Any changes made to this branch will not affect the other branches and hence you can do crazy stuff without fear of ruining the project.

You can create a new branch for every new feature you create and once you’ve successfully created the feature you can merge your branch to the main branch. If you mess up your project you can always delete your current branch and create a new one from the main branch.

Working with branches in git is like owning the Eye of Agamotto, you can try everything, and every time you mess up, you can go back in time and fix it. Cool right🤩.

It’s not done yet, we will see how to merge, push and delete our local branches to GitHub in the next article. Until then,

Learn git, get fit.

Adios, 😂✌️

--

--

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
hiruthicSha

hiruthicSha

43 Followers

Python, JS developer | Flutter | Fitness and self development