Most Common Git Commands Every Developer Should Know | ADevGuide

Most Common Used Git Commands to make Developer’s life easier with a Cheatsheet

Git is everywhere. Git has made the lives of developers and DevOps so much easier. If you a developer, You must be familiar with these git commands. Here are the Most Common git commands:

Config commands

git config

The command is used to set the git config.

git config --global user.name “prabhu”
git config --global user.email “[email protected]

The below command will list all git config:

git config --list

Repository Creation Commands

git init

This command is used to start a new repository.

git init {repository name}

git init

git clone

This command is used to obtain a repository from an existing URL.

git clone {repo-url}

git clone https://github.com/iPraBhu/git-practice.git

git remote

git remote add {repo alias} {repo url}

This command is used to map a local repo to a remote repository.

git remote add origin https://github.com/iPraBhu/git-practice.git

Change Tracking Commands

git log

This command is used to show a log history of a branch or file.

git log
This command prints the entire version history of the branch.

git log - ADevGuide

git log –follow {file}
This command prints version history for a file, including the renaming of files.

git log –follow file1.txt

git log

git diff

git diff
This command compares all the file differences which are not yet staged.

git diff –staged
This command compares the differences between all the files in the staging area and the latest version present.

git diff {first branch} {second branch}
This command compares the differences between the two branches mentioned.

git diff a9f405ff1789a698637a1410718a39aa60939114 10e9de14db5c2e69132367bcd13b9fe2786707a0

git diff

git show

git show {commit}
This command shows the metadata and content changes of the specified commit.

git show a9f405ff1789a698637a1410718a39aa60939114

git show

git tag

Tags are used to mark a commit stage as relevant. We can tag a commit for future reference. Primarily, it is used to mark a project’s initial point like v1.1.

git tag {tag name}

git tag phase1

This command is used to tag the current branch

git add

git add {file}

This command adds the specified file to the staging area.

git add *

git add file1.txt

git add file2.txt

This command adds all files to the staging area.

git rm

git rm {filename}

git rm file1.txt

This command removes the file from your working directory and stages the deletion.

13 Most Basic Docker Commands Every Developer Should Know

git commit

This command records the file(s) in the version history.
git commit -m “{commit message}”

git commit -m “updating file2.txt”

git commit

-a switch with the commit command to automatically “add” changes from all known files and to automatically “rm” files in the index that have been removed from the working tree, and then perform the actual commit.

git commit -a -m “{commit message}”

git commit -a -m “updating known files”

git stash

git stash push
This command temporarily stores all the modified tracked files.

git stash list
This command lists all stashed changesets.

git stash apply
This command restores the most recently stashed files.

git stash

git stash apply [email protected]{2}

This command apply the mentioned stash.

git stash drop
This command discards the most recently stashed changeset.

git stash drop [email protected]{2}

This command discards mentioned stashed changeset.

git reset

git reset {file}
This command unstages the file, but it preserves the file contents.

git reset file1.txt

git reset

git reset {commit}
This command undoes all the commits after the specified commit and preserves the changes locally.

git reset cab3740

git reset commit

git reset –hard {commit}
This command discards all history and goes back to the specified commit.

git reset --hard cab3740

git revert

git revert {commit hash}

The command reverts the changes made in the mentioned commit and creates a new commit. It leaves the files in the same state as if the commit that has been reverted never existed. The log will preserve the commit.

git revert faad7b5

Branch Commands

git branch

This command lists all the local branches in the current repository.
git branch

git branch

This command creates a new branch with the name provided.
git branch {branch name}

git branch stage

This command deletes the specified branch.
git branch -d {branch name}

git branch -d stage

git checkout

This command is used to switch from one branch to the specified branch.
git checkout {branch name}

git checkout test

This command creates a new branch with the specified name and also switches to it.
git checkout -b {branch name}

git checkout -b prod

git merge

This command merges the specified branch’s history into the current branch.
git merge {branch name}

git merge

Synchronize Changes Commands

git fetch

git fetch

This command download commits, files, and refs from a remote repository into your local repo. Fetching is what you do when you want to see what everybody else has been working on.

git pull

git pull {Repository Link}
This command fetches and merges changes on the remote server to your working directory.

git pull https://github.com/iPraBhu/git-practice.git

git pull

git pull origin {branch}

git push

git push origin {branch name}
This command sends the committed changes of the master branch to your remote repository.

git push dev

git push origin –all
This command pushes all branches to your remote repository.

git push origin :{branch name}
This command deletes a branch from your remote repository.

git push origin :test

*origin is the remote repository alias.

git for-each-ref

git for-each-ref — sort=-committerdate refs/heads/

The command will list the most recently worked on branches from top to bottom.

CheatSheet

Most Common Git Commands Every Developer Should Know | ADevGuide

 

 

References

Git Documentation