Let’s learn Git

As a developer we all had to deal with large code base . Most of the time we work in a team and that is why every fresher should know about git before stepping into software industry.
In this post we will learn about some basic commands in git.
Lets get started
let’s clone a project first . You can clone it from my github repo
before that let’s add ssh key in our github repo
Open your terminal and type
ssh-keygen
it will generate two files
- id_rsa
- id_rsa.pub
copy the content of id_rsa.pub we need to add this code in our github account
go to github account -> settings -> SSH and GPG keys

press on New SSH key and pastes the key and save it .
Now you will not need to give you id and pass every-time you push something in your repo.
Basic commands of git
| Cloning a git repo
as we added ssh key in our github repo we can copy the ssh url
git clone git@github.com:devShahriar/golang-microservies- tuitorial.git
|Check status of your changes
git status
| Push changes to the remote repo
git add .
git add <file-name>
git commit -m "commit message"
git push origin <branch-name>
|Initialize a git repo
git init
|Add or connect you local repo with the remote repo
git remote add origin <repo link>
|Create branch
git branch <branch-name>


When we create a branch we will inherit or we will get same file structure same code of the branch we are currently in
Suppose i am in (1-handlers) branch so if i create a new branch named (feature/login) from here we will get same code same file structure like (1-handlers) branch in our newly created branch (feature/login)
|Check which branch you are in.
git branch#check all branches local branch and remote branchgit branch -a #to check all branchesgit branch -r #to check remote branches
|Change your current branch
git checkout <branch-name>

Note: Suppose you are working on a (feature) branch and developer2 has push some changes in the remote repository .
In that case you need to pull from the remote repository and update your local repo before pushing it into the remote repository
|Pull changes from a remote repo in Git:
git pullor git pull <branch-name>
#it will only pull update from the branch which name is passed as a parameter
|Git Fetch
It will fetch update from the remote repo but it will not merge .
git fetch
|Check differences or changes you made
git diff #check changes between branches git diff <branch1 name>..<other-feature-branch-name>

Here I changed the port name from 9000 to 9999
So inserted line will shown in green and previous line will be shown in red
#You can put a file in the parameter to only see changes on a specific filegit diff <filename>
|Merge a branch
Suppose we have two branch
- Branch one named : main
- Branch two named : develop
Now we want to merge develop branch into main branch
So,
Step1 . Change your branch from your current branch to the branch you want to merge
in our case we want to merge develop branch with main branch
Suppose currently we are in the develop branch

So we need to checkout to main branch
git checkout main
Step2 . Now we need to write the merge command
git merge <the branch which we want to merge>
We want to merge develop branch with the main branch
So our command will be
git merge develop
|To remove the changes
git stash
|How to revert unstaged changes in Git:
git checkout filename
|How to abort a conflicting merge in Git:
git merge --abort