Basics of Git

Anil KumarAnil Kumar
5 min read

A Git Cheat Sheet

Git is a distributed version control system. This means that a local clone of the project is a complete version control repository. These fully-functional local repositories make it easy to work offline or remotely. Developers commit their work locally, and then sync their copy of the repository with the copy on the server. This paradigm differs from centralized version control where clients must synchronize code with a server before creating new versions of code.

Git's flexibility and popularity make it a great choice for any team. Many developers and college graduates already know how to use Git. Git's user community has created many resources to train developers and Git's popularity makes it easy to get help when needed. Nearly every development environment has Git support and Git command-line tools run on every major operating system.

Why do I need Git?

But far more important is that it can be distributed, meaning you can host your Git repository on an external system like GitHub, GitLab, etc.

When you do this, you can give other developers, colleagues, teammates the option to develop asynchronous with you and see the changes you have made.

This makes Git a perfect system for developers to collaborate and work together on one codebase.

Within this series, we'll also look at branching, merging, and all those fancy terms which make Git even more powerful!

Git Commands

The following git commands must be used to submit your projects or documents to web storage services.

Setup

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

git config --global user.name "anil Kumar"

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

Start a Project

Create a local repo (omit)to initialize the current directory as a git repo

git init <directory>

Download a remote repository

git init <directory>

###Make a Change

Add a file to the staging

git add <file>

Stage all files

git add .

Commit all stage files to git

git commit -m "commit message"

Add all changes made to tracked files & commit.

$ git commit -am "commit message"

Basic Concepts main: default development branch.

origin: default upstream repository.

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.

$ git branch

Create a new branch

$ git branch <new-branch>

Switch to a branch & update the working directory

$ git checkout <branch>

Create a new branch and switch to it

$ git checkout -b <new-branch>

Delete a merged branch

$ git branch -d <branch>

Delete a branch, whether merged or not

$ git branch -D <branch>

Add a tag to the current commit (often used for new version release)

$ git tag <tag-name>

Merging Merge branch a into branch b. Add -no-ff option for non-fast-forward merge

Git Branch

Git Branch 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 features, keeping history clean.

Git Rebase

 $ git checkout feature


 $ git rebase main

Interactively clean up a branches commits before rebasing onto the main

$ git rebase -i main

Interactively rebase the last 3 commits on the current branch

$ git rebase -i HEAD~3

Undoing Things Move (&/or rename) a file & stage move

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

Remove a file from the working directory & staging area, then stage the removal

$ git rm <file>

Remove from staging area only

$ git rm --cached <file>

View previous commit (READ Only)

$ git checkout <commit-ID>

Create a new commit, reverting the changes from a specified commit

$ git revert <commit-ID>

Go to the previous commit & delete all commits ahead of it (revert it safer). Add --hard flag to also delete workspace changes (BE VERY CAREFUL)

$ git reset <commit-ID>

Review your Repo List new or modified files not yet committed

$ git status

List commit history, with respective IDs

$ git log --oneline

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

$ git diff

Show changes between two commits

$ git diff commit1_ID commit2_ID

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

$ git stash

As above, add a comment to it.

$ 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

Reapply stash without deleting it

$ git stash apply

Reapply 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 the last stash made

$ git stash drop stash@{1}

Delete all stashes

$ git stash clear

synchronizing Add a remote repository

$ git remote add <alias> <url>

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

$ git remote

Remove connection

$ git remote remove <alias>

Rename connection

$ git remote rename <old> <new>

Fetch all branches from remote repo (no merge)

$ git fetch  <alias>

Fetch specified branch

$ git fetch  <alias> <branch>

Fetch remote repo's copy of the current branch, then merge

$ git pull

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

$ git pull --rebase <alias>

upload local content to the remote repo

$ git push <alias>

upload to a branch

$ git push <alias> <branch>
0
Subscribe to my newsletter

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

Written by

Anil Kumar
Anil Kumar

I am Anil Kumar. Aspiring Full-Stack Developer learning new things everyday and exploring the world of programming and have been enjoying it so far. Looking to leverage my passion for building engaging products and user experiences. Skilled in HTML, CSS, JavaScript, Tailwind CSS and Bootstrap with excellent communication skills and always ready to learn new skill sets, I am also a great team player and a self-starter that likes to engage in a logical thinking approach to problem solving. I specialize in building websites and keep my code clean and readable. I enjoy working with complex user interfaces.