Git basics, learning Git Part-1

Being a developer, at whatever experience level you are, you have used git, got stuck with it, resolved and moved your project to production. If you are beginning your journey or on the way, it is likely to have faced/might have difficulties with git at certain levels. At a beginner level, merge conflicts, git rebase, and aborting pull “as fast-forward is not possible” are some of the errors which make our eyebrows collide. Sometimes you push a commit which should not be on the repo. We will get to these points in the below sections. Let’s brush up on some basic git commands to get started, I will try to cover all the possible git errors I faced and solve them later. Resources are linked along.
Getting Started
I hope your system has Git installed. You can check with the following: git --version
. The initial errors you will face if git is not installed are:
- git: command not found
- git’ is not recognised as an internal or external command, operable program, or batch file.
Download git from here and you can follow this guide to set up git on your machine. For your project to support your git account, you need to embed your username and email as git binds this information with your commits.
- `git config — global user.name “Your Name”`
- `git config — global user.email “youremail@domain.com”`
Check your configuration with: git config --list
After initializing git in your project with `git init` and working on the app feature, you can check git branches which are on your machine with `git branch –list`.
The above response should pop up
If you want to create a new branch which doesn’t exist on your machine as well as on the repository you can create one with: git checkout -b <brach nane>
. This will create a new branch from the current branch you were on. Let’s say I am currently on dev and git checkout -b staging
this will create ‘staging’ from ‘dev’. This was just, for example, it recommended naming branches according to the feature or the version you are working on.
If the branch exists on the repository but not on the machine that means you want to move to a branch, not create a new one, you can omit -b and git checkout <brach name>
Example: git checkout db_changes
When collaborating with a team you have to utilise remote repositories or branches. If checking out directly to a branch is not working or doesn’t show any changes that is because you have to fetch all the contents of that branch with: git fetch -all
and then checkout.
Stashing
What if you have unfinished work and need to change your branch or take a pull, changes aren’t ready to commit or just need some fresh air, you will have to stash your changes with git stash
. Also, you can add a message to your stash. It will save your changes for later use which you can get back by: git stash pop
. This is to apply the stashed changes to your current working branch. So, It can be used if you want to apply the same changes to several branches or switch branches in between.
You can also stash untracked or ignored files by git. If you have created a new file or updated a file mentioned in gitignore, those won’t be tracked by git. Using -u or –include-untracked like git stash -u
you can stash the untracked files.
Checking multiple stashes by git statsh — list
and you can pop the desired stash by git stash pop stash@{number
Essentials to know after starting
Rebase

This is one of the topics were where beginners stuck and StackOverflow has got questions. So, what exactly does Git Rebase mean? To move the base of your commit or sequence of the commit to a new base
Let’s say you created a feature branch from the master and the master branch progressed after that, to get the latest updates and keep your history clean, you rebase your feature branch with the master branch and thus change the base of your branch to the latest commit of the master.
git rebase master
will take all the commits in your working branch, but if you want to check before instead of blindly getting all the commits you can use rebase interactively, git rebase -i
or git rebase -interactive master
. There are other rebase commands please check the detailed doc here
When there is a team of developers working on a project, check the git logs of the main branch whenever you face production issues in the feature and want to check which commit is creating the issue git log
is advisable. But to keep the logs clean we are back to git rebase
.
Git rebases will have a clean git log history. One thing to remember is not to use git rebase on public branches. Also, if devs are working on the branch you want to rebase, step back, take a long breath and use git merge
.

Discarding
You have a list of files that are changed but while committing you to want to discard the changes in the file you can by: git checkout <fileName>
and want to discard all the changes by git checkout .
Reset
Check the list of your commitsgit log --oneline
You can reset recent commits by passing the number of commits, e.g.: git reset HEAD~(number of commits to reset
=> git reset HEAD~1
, this will reset the top 3 commits. Two things to note here, you cannot reset a particular commit with this command and this command can be used for local commits, not for the commits that are pushed to the remote repo.
There are three types of Rest commands:
1. Soft Rest: This is the state between git add
and git commit
. The soft reset will just uncommit the changes and keep your files staged. git reset --soft HEAD~1
2. Hard Reset: You got it from the name, it is a bit risky command. Hard Reset resets the index and the working tree, which means all your changes are discarded. git reset --hard HEAD~1
3. Mixed Reset: It is the default option. It will reset the index but not the working tree, meaning that your changes will stay but won’t be committed.git reset --mixed HEAD~1
Again, this is only for the local commits and does not reset the commits which are pushed. If you have already pushed a commit to your remote repo then resting a commit and pushing will create a conflict.
What if you already pushed your commits to the repo which were not meant to be? Revert is your saviour here.
Revert
Unlike Reset, you can revert any commit. Inside the hood, revert adds one more commit to your repo removing the changes by the commit you reverted. git revert <commit id>
. Revert will not temper the git history and logs. It will add a fresh commit which will accommodate the revet in your repo.
Check more about Reset, Revert and Checkout here.
Git Log
You might not know but checking git logs can be very interesting with several commands. I will try to provide a brief overview of the same.
We already came through git log --oneline
It displays all commits in descending order with commit ID and message (first line only).
--decorate
displays all the references like tags and branch names. It is useful when you want to check to which branch or tag the commit is associated. git log --oneline --decorate
.
Just like the terminal logs after taking a pull, `git log –stat` will present you with the short history stating the changes introduced, like the number of lines inserted and deleted.
git shortlog
is a useful command for the senior devs or managers to check who is working on what feature. It displays the logs grouped by the author’s name with the number of commits at the end of their name and message (first line).
There are several other formats to check git logs learn them from here.
This is part 1 of the “Git — Little more than basic” series, we will check about Git Hooks, Git Refs, Git Submodules and more in the next one. Thanks for making it here.
Drop your comments or tell me more at shreybanugaria10@gmail.com.
Let us connect:
LinkedIn: Shrey Banugaria
Twitter: BanugariyaShrey
#git #learninggit #gitbasics #github #gitbasics #gitforbegineers