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…
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
Author: hiruthicShaSS <email@example.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.
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>
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.
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.