Most used Git Commands

Git is a free and open-source distributed version control system that’s responsible for everything GitHub related that happens locally on your system.

GitHub is a web-based platform where you can share your work with other developers and showcase your learnings.

Most used Git Commands

git config
-> Configures user information used across all local repositories.
set a name that is identifiable for credit when review version history
-> git config –global “username”

set an email address that will be associated with each history marker.
-> git config –global “email”

git init
Initializes an existing directory as Git repository (.git directory).
-> git init

git clone
-> git clone [url]
retrieve an entire repository from a hosted location via URL.
-> git clone –branch[url]

git add
add a file to your next commit(stage).
-> git add [file]

git rm
Deletes the file from project and stages the removal for commit. -> git rm [file]

git mv
Changes an existing file path and stages the move.
-> git mv [existing-path][new-path]

git commit
Commits the staged contents as a new commit snapshot.
-> git commit -m “your commit message goes here”

git branch
List, Create or, Delete Branches(branches looks like a acyclic graph)
List branches
-> git branch
Create a new Branch at the current commit
-> git branch [branch-name]
Delete a Branch
-> git branch -d[branch-name]

git status
Shows the paths of modified files in working directory.
-> git status

git diff
Show changes between commits.
to see diff of what is changed, but not staged
-> git diff
to see diff of what is staged, but not commited
-> git diff –staged
to see diff between 2 branches
-> git diff BranchA…BranchB

git log
Shows the commit history for the currently active branch.
-> git log
Shows the commits on branch-A that are not on branch-B.
-> git log branchA..branchB

git checkout
Switch Branches.
Switch to another branch and, check it out into your working directory .
-> git checkout [branch-name]

Switch to another branch(create if does not exist)
-> git checkout -b [branch-name]

git merge
For joining two or more development histories together.
-> git merge [branch]

git fetch
Fetch branches and/or tags from one or more other repositories.
-> git fetch [alias]
you can use git fetch to know the changes done in the remote rep/branch since your last pull.

git pull
Fetch and merge any commits from tracking remote branch.
-> git pull

git push
For making changes in your repository
-> git push origin [branch-name]

Transmit local branch commits to the remote repository branch.
-> git push [alias]

git rebase
Applies any commits of current branch ahead of specified one.
It is used if you need to rewrite the history of a project.
-> git rebase [branch-name]

git revert
Reverts some existing commits.
-> git revert

git reset
If you want to delete the commits.
-> git reset [hashcodeId]
you can get “haschodeId” by running git log
(NOTE: the commits deleted will be in unstaged area)

Resets current HEAD to the specified state.
Unstages a file while retaining the changes in working directory.
-> git reset [file]
(NOTE: the commits deleted will be in unstaged area)

Clears staging area , rewrite working tree from specified commit.
-> git reset –hard[commit]

git stash
Temporarily stores modified, tracked files in order to change branches.
it’s used when you don’t want to commit your working files but want to store

Save modified and staged changes
-> git stash

List StackOrder of stashed file changes
-> git stash list

write working from top of stash stack or to bring your temporarily stored files.
-> git stash pop

Discard the changes from top
-> git stash drop

For deleting temporary stored, modified, tracked files.
-> git stash clear

git remote
For adding the URL of your repository to your project
-> git remote add origin [URL]
remote means you’re working with URLs and origin is the name of the url.

Shows all the URLs attached with your project
-> git remote -v

Leave a Reply