Physical Address

Sagipora Sopore Baramulla 193201  Jammu & Kashmir

featured image

Get Started with Git and GitHub – With Complete Cheatsheet

Best tutorial on git & github with latest and complete cheatsheet covering everything..

What is Git

Git is a version control system and command line tool used by developers worldwide to manage their projects and their team’s codebase.

Learn more about Git here.

What is Version Control

Version Control is a way to manage changes made to certain files in a repository, so when making changes to code, all the files are tracked and we can see a history of all the changes – multiple versions of our code. This lets us keep an eye out for bugs and if things go wrong, then we can go back to an older version of our codebase.

Also Read: GitHub Repos That You Won’t Believe Exist

What is a Repository

A repository is the collection of files that make up your project. All these files are stored remotely on platforms such as GitHub. So at all times, you will be able to access remote files and local files and the files that you cloned on your machine.

Learn more about version control here

What is Clone

A clone is a replica of a repository to which we can make changes locally on our machine and possibly push them back to our remote project.

What is GitHub

Github is a code hosting platform for version control that developers use to work together on projects from all over the world. In other terms, this is where you can store your projects and find like-minded people to work with.

Also Read: Important Terms In Web Development That You Must Know

Getting Started on GitHub

First of all go to github.com/signup and follow the steps to create your account, Github is really user friendly!

That was easy, right? Now that you have your own account, it’s time to create your first repository!

create new Github repository

To create a new repository simply press the green button as shown above in the image. It will then take you to the repository creation page.

Github repository creation page

Give your shiny new repo any name you want! and hit the green colored create repository button. Now it’s time to add files to our github repository.

Uploading Your Files to GitHub Repository

Now in order to use Git commands in our terminal, we will need to install Git on our local machine. So simply go to “git-scm.com/downloads” to download and install the latest version!

Next you will need a project to upload, so let’s say i have simple web page built that i want to upload to GitHub. Firstly, we need to initialize a repository, this tells Git that this folder “project-folder” is something that it needs to keep track of.

To do that we need to run a Git command in terminal:

Also Read: Kali Nethunter 3.0 Installation On Unrooted Android Device

git init

Immediately after doing this, a repository will be initialized. To add our files we will need to run more commands to push our files to the github repository.

git add .

git commit -m "Add files to repo"

git remote add origin https://github.com/nasyx-rakeeb/tesla-clone.git

git push -u origin master

Now check your github repository, you will see your uploaded files there. Now let’s see what these commands do!

Common Git Commands

1) git init

Use it to initialize an empty repository in the project’s root folder.

2) git add

Use this command to stage your changes, to prepare them for a commit, you can specify a file name or use a dot to add all changes.

3) git commit -m “message”

Use it after adding changes, to commit them, which creates a local snapshot of the current state of your branch. You can go back to any past commits that your make.

4) git push

Use this command to push all your commits to the remote branch that you’re working on. If you push to a new branch, you must also specify that you want to push your current branch upstream.

5) git pull

This command will pull any changes that might have been added on the remote branch that you’re working on.

6) git fetch

This will get the updates from the remote, including new branches

7) git checkout <branch-name>

Use it to switch to a different active branch or you can use git checkout -b <branch-name> to create a new branch from the currently active one

Also Read: Latest JavaScript Features Every Web Developer Should Know

8) git clone <repository-link>

This will clone an existing github repository to your local machine by providing the link to the repository on github.

9) git status

Use this one to check the current status of your working branch, to see details such as changed files, unstaged or staged files and so on.

Best Complete Git Cheatsheet

• Setup

Set the name and email that will be attached to your commits and tags.

git config --global user.name "test" 

git config --global user.email "myemail@gmail.com" 

Start a Project

Create a local repo (omit <directory> to initialize the current directory as a git repo.

git init <directory>

Download a remote repo

git clone <url>

Make a Change

Add a file to staging

git add <file>

stage all files

git add .

commit all staged files to git

git commit -m "message"

Add all changes made to tracked files and commit

git commit -am "message"

• Basic Concepts

main: default development branch

origin: default upstream repo

HEAD: current branch

HEAD^: parent of HEAD

HEAD~4: great-great grandparent of HEAD

Branches

List all local branches. Add -r flag to show all remote branches -a flag for all branches

Also Read: Best VSCode Extensions you should consider in 2022

git branch

create a new branch

git branch <new-branch>

switch to a branch and update the working directory

git checkout <branch>

create a new branch and switch to it

git checkout -b <branch>

delete a merged branch

git branch -d <branch>

delete a branch whether merged or not

git branch -D <branch>

add a tag to current commit (often used for new version releases)

git tag <tag-name>

Merging

Merge branch A into branch B. Add -no-ff option for no-fast-forward merge

git checkout b

git merge a

Merge and squash all commits into one new commit

git merge --squash a

Rebasing

Rebase feature branch onto main (to incorporate new changes made to main). Prevents unnecessary merge commits into feature. Keeping history clean.

git checkout feature

git rebase main

Interactively clean up branch commits before rebasing onto main

git rebase -i main

Interactively rebase the last 3 commits on current branch

git rebase -i Head~3

Undoing Things

Move (&/or rename) a file and stage move

Also Read: Scss vs Bootstrap vs Tailwind CSS

git mv <existing-path> <new-path>

Remove a file from working directory and staging areas then stage the removal

git rm <file>

Remove from staging area only

git rm --cached <file>

View a previous commit (READ ONLY)

git checkout <commid_ID>

Create a new commit reverting the changes from a specified commit

git revert <commit_ID>

Go back to a previous commit and delete all commits ahead of it (revert is safer). Add –hard flag to aslo delete workspace changes (BE VERY CAREFULL)

git reset <commit_ID>

Review your Repo

List new or modified files not yet committed

git status

List commit history, with respective ID’s

git log --online

Show changes to unstaged files. For changes to staged files, add –cached option

git diff

Show changes between two commits

git diff commit_ID commit2_ID

Stashing

Store modified and staged changes. To include untracked files, add -u flag. For untracked and ignored files, add -a flag.

git stash

As above, but add a comment

git stash save "comment"

Partial stash, Stash just a single file, a collection of files or individual changes from within files.

git stash -p

List all stashes

git stash list

Re-apply the stash without deleting it

git stash apply

Re-apply the stash at index 2 then delete it from the stash list. Omit stash@(n) to pop the most recent stash

git stash pop stash@(2)

Show the diff summary of stash 1. Pass the -p flag to see the full diff.

git stash show stash@(1)

Delete stash at index 1. Omit stash@(n) to Delete last stash made.

git stash drop stash@(1)

Delete all stashes

git stash clear

Synchronizing

add a remote repo

git remote add <alias>

View all remote connections. Add -v flag to view urls.

git remote 

remove a connection

git remote remove <alias>

Rename a connection

git remote rename <old> <new>

To Fetch all branches from remote repo (no merge)

git fetch <alias>

Fetch a specified branch

git fetch <alias> <branch>

Fetch the remote repo’s copy of the current branch then merge

git pull

Move (rebase) your local changes onto the top of new changes made to the remote repo (for clean, linear history)

git pull --rebase <alias>

Upload local content to remote repo

git push <alias>

Upload to a branch (can then pu request)

git push <alias> <branch>

That’s it for this article guys, i hope you learnt something new from here. If you need to say anything then leave a comment below.

Want more content like this – Click Here

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply

Your email address will not be published. Required fields are marked *

Programming & Tech Digest
Programming and Tech innovations delivered straight to your inbox for free.