Learning Git and Github

Abishek BebitoAbishek Bebito
5 min read

This blog gives an overview of what I learn and what the challenges I face. and explain with my understanding

What is Git?

  • It is a distributed version control system—>it means different developers have different versions of

    code.

  • helps to track changes

How will it be without Git?

  • It's tough to track if multiple people work on the same project.

  • history is unclear

  • Even if I was a single developer, I don’t have history... I need to save each file change as a different version on my local computer. For example, if the project was 100 MB and I had 1000 versions, it would consume lots of storage.

Why do I use Git?

  • collaboration

  • Backup

  • Branching & merging

  • can track which team member did which change in a project


simple commands for a common workflow:

setup user —>this helps to identify who is commited the code

  • git config —global user.name “userName“

  • git config —global user.email “userEmail”

git init

  • turns the current folder into git repository

  • .git folder created (hidden)

git status —> status of working directory and staging area

git add —> add the files to be ready for next commit.
git commit —> save changes in local repository

before pushing code to your remote repo u need to create one in github, after that only you can add url to push ,add,pull and so on that related to remote command.

git push —> save changes in remote repository(remote repo means repo that is stored in github)

git log —> gives the history of commits

git checkout —> inorder to change the branch or a specific commit using commit hash

  • gets into a detatched head state(head is the pointer points to latest commit)

  • to get back to desired branch git checkout [branchname]


GitHub

while git is a version control system for your local repository ,meanwhile github is a cloud platform that can be used to store your local repository in online to collaborate with others.

to push code to github we have to first connect to the remote

Local RepositoryRemote Repository
while commit command the code changes in local repowhile push command the code changes in remote repo
solo devlopmentteam collabration
Eg: git commit, git log, git statusEg:git push, git pull, git fetch

challenging commands for me

origin is the alias the remote repository url.we can have other names as well as..it is a default word given by git while cloning a repository from the github remote repository.For example, if we want to push a repo to a remote repo we dont have to give the url each time we push or pull.

git branch -M main

  • change the branch from the default master branch to main branch.

  • by default git uses master as default but for github it will be main branch

git remote add origin <gitHub repo url>

  • this will help to link the local repo to remote repo.

  • remote —>command for adding remote repo to link local repobrancj=hin and remote repo

  • origin —>alias for that repo

  • we can have multiple remote repo with differnet alias names.

git push -U origin main

  • push the code to remote repo

  • main branch is where our local repo codes available

  • origin is to which remote repo url we want to push


Branching & Merging

Common Commands:

git branch <branch name>

  • create a new branch

  • branch will be created from the current branch you r in..and not from the root branch

  • git branch <new-branch> <current-branch> —>this will create the branch from the desired branch we want

git checkout <branchname>

  • changes to desired branch want to go

  • checkout can be done by new commands like switch for changing branches but cant be done changing to the commits.

git pull origin main

git saviour commands

git checkout <commithash>—>can be able to get the code from the commits.

git reset—>remove bad commits using this command

  • soft reset—>remove commits but staged, keep code in working directory

  • mixed reset—> remove commits unstaged but keep code in working directory

  • hard reset—> remove commit and code will also be removed in working directory(be cautious while doing this)

stagedunstaged
after git add commandbefore git add command

git revert—>create new commit if we remove commits..more like rewriting a commit.

git stash

  • if i want to save a uncommited changes lets take example i was in branch named branch2 currently.

  • and go to a working branch named main branch and see some code commits

  • and getting back to branch2 and start working on them where i left.


Git rebase is easy as merge but dont touch without knowing it.

  • It is another way to integrate changes from a branch, just like merge.

  • But the merge command has history, but rebase doesn't.


using Git by GUI

I was using the IntelliJ GUI for git commands since it’s comfortable for a java developer

It was easier to use.

Tips:

  • Commit message should be imperative.it should say what would happen if i merge this code.For example: git commit -m “modify readme.md”

  • dont do rebase in remote only rebase in local if u dont know everything about the project you are working on.

Below are the reference to where I learned from:

Exercise from w3Schools

GitHub Cheat Sheet

practice to learn—>highly recommended

learn rebase here

10
Subscribe to my newsletter

Read articles from Abishek Bebito directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Abishek Bebito
Abishek Bebito