Sitemap

Github basics for beginners

6 min readJul 22, 2024

--

Press enter or click to view image in full size
Image acquired from LinkedIn connection

Git is just a progress tracking for coding purposes especially during software development. It is particularly useful for collaborative process between developers to contribute inside a same project. It is a distributed version control system.

Installation

Git installation is pretty easy and straightforward. It can be installed through TERMINAL command or by DOWNLOADing from the official website.

Let’s drive right into the main essentials of git.

Initializing a new repository:

Press enter or click to view image in full size

You may have seen the given messages while creating new repository in GitHub or any other version control tools.

Let’s break it down for clear concept.

1. New repository:

  1. New repository:

git init:

This command initializes git inside a folder you are in. It creates .git folder where all the git metadata and informations are stored.

git add README.md

It adds README.md file if you have created to staging area. Staging area refers to next point or step before committing the changes.

git commit -m “first commit”

The “first commit” is the message for the changes you are committing to.

git branch -M main

This renames the current branch to main. Previously name ‘master’ was used for branch names.

BRANCH is the idea to have multiple works to be executed simultaneously which can later be merged together.

git remote add origin https://github.com/roitpak/Test.git

This will add origin of the given url. The url is the url of the repository you have created in the GitHub or any other version control tools.

git push -u origin main

This command will now push the changes you have made to the Github repo in the cloud.

Now your changes will be available in the Repo. If you have added README.md file then only that file will be present there.

present there.

2. Existing repository:

This means If you already have a project running and want to push the project to the Github cloud.

git remote add origin https://github.com/roitpak/Test.git

Inside the root directory of your project running this command will add the remote url of your repository.

git add .

git commit -m “first commit”

Gotcha: This step is not mentioned in the image.

Mind that developer should carry out above two steps that is adding and committing before pushing it to the GitHub repo.

git branch -M main

git push -u origin main

These steps are similar to above explained steps.

Note: You should have already initialized, added and committed your code.

3. Cloning repository

Sometimes you need to fetch running projects. In that case on GitHub repo get GitHub url . There will be two options http or ssh

http: It will ask for username and password of the user with access.

Ssh: If the ssh key of the device is added to the GitHub user with access to repo then this url will work.

Command:

Git clone <hhtp-or-ssh-url>

Scenarios:

Creating new branch and merging

git checkout -b new-branch-name

This is shortcut to creating and switching to new branch.

Long steps are:

git branch <branch_name>

it checkout <branch_name>

git push -u origin <branch_name>

Branch name needs to be specified while pushing to repo in a similar way while pushing to main branch.

Merging new branch to main

After you have finalized your work and is ready to merge to the main branch, you can either create a pull request / merge request in GitHub or you can do it locally.

git checkout main

git pull origin <branch_name>

This will automatically pull and merge to main branch. For instance if you team member have pushed new branch to repo then you can directly call above command to merge it.

Long steps are:

git fetch

git pull origin <branch_name>

Reverting the changes

Sometimes there can be scenarios where some commits needs to be deleted. In that case we need to get the commit hash and then delete it.

git log

This will list the previous commits. Get the commit you want to delete and copy the hash code.

git reset — hard <commit-hash>

Now your commit will be removed.

git push -uf origin main

Adding f typically refer to force push. Hard reset requires force push to the repo.

Get Rohit Pakhrin’s stories in your inbox

Join Medium for free to get updates from this writer.

Soft vs hard reset

1. git reset — soft <commit-hash>: If you accidentally committed too early but want to make additional changes before committing again.

2. git reset — hard <commit-hash>: If you want to completely remove the last commit and discard all changes made in that commit.

Stashing current changes:

Sometimes you need to set aside your current changes for a time being to checkout other commits.

git stash

To stash current uncommitted changes .

git stash pop

To recover the stashed changes

git stash push -m “your-message”

To stash with message

git stash list

List out the stashes that were done before.

git pop stash@{2}

To recover number 2 stash . Stash number will be indexed in front of commit message.

Merge vs Rebase:

This is an interesting topic. Merge is moving INTO and Rebase is moving ONTO.

Basically Rebase will generate a straight line of history line without branches whereas merge will show all the branches that are merged into like branches of a tree.

(Extra) Cherry pick:

It generally refers to selecting individuals commits and applying to current work. Its usage is when a progresss in one branch requires in other.

git cherry-pick <commit-hash>

Topics to explore:

  1. Accessing multiple GitHub accounts form single computer
  2. Github Pages
  3. Own customized Read Me.

TLDR:

Create and Merge Branch:

• git checkout -b new-branch-name: Create and switch to a new branch.

• git push -u origin new-branch-name: Push the new branch to the remote repository.

• git checkout main && git pull origin new-branch-name: Merge new branch into main.

Reverting Changes:

• git log: View commit history.

• git reset — hard <commit-hash>: Revert to a specific commit.

• git push -uf origin main: Force push changes to the remote repository.

Stashing Changes:

• git stash: Stash current changes.

• git stash pop: Apply stashed changes.

• git stash list: List all stashes.

• git stash pop stash@{2}: Apply a specific stash.

Merge vs Rebase:

• Merge: Combines branches, showing all branches in the history.

• Rebase: Creates a linear history by applying changes from one branch onto another.

Cherry-Picking:

• git cherry-pick <commit-hash>: Apply a specific commit from one branch to another.

Wrapping up:

Being a developer one should also have good knowledge of git as it powers the fundamentals of every developer.

TLDR

Create and Merge Branch:

• git checkout -b new-branch-name: Create and switch to a new branch.

• git push -u origin new-branch-name: Push the new branch to the remote repository.

• git checkout main && git pull origin new-branch-name: Merge new branch into main.

Reverting Changes:

• git log: View commit history.

• git reset — hard <commit-hash>: Revert to a specific commit.

• git push -uf origin main: Force push changes to the remote repository.

Stashing Changes:

• git stash: Stash current changes.

• git stash pop: Apply stashed changes.

• git stash list: List all stashes.

• git stash pop stash@{2}: Apply a specific stash.

Merge vs Rebase:

• Merge: Combines branches, showing all branches in the history.

• Rebase: Creates a linear history by applying changes from one branch onto another.

Cherry-Picking:

• git cherry-pick <commit-hash>: Apply a specific commit from one branch to another.

--

--

Rohit Pakhrin
Rohit Pakhrin

Written by Rohit Pakhrin

Incase I don't see ya, Good Morning, Good Afternoon and Goodnight