Linux Basics Linux Commands

Useful GIT Commands

git commands
useful git commands

Useful Git Commands

This post gives some useful Git Commands which will help you to play with a git repository.

What is Git?

Git is a program that helps to do version control. It is able to keep track of files. To keep track of file changes, GIT uses content snapshots, checksums and metadata. This is how Git providing version control. Git created by Linus Torvalds the one who created the Linux kernel.

Check below for some useful Git Commands:

How to clone a Branch:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git clone <url>
git clone <url>
git clone <url>
To create a branch:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout -b <branch_name>
git checkout -b <branch_name>
git checkout -b <branch_name>
To show all branches:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch
git branch
git branch
How to add a change in the working directory:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git add <file>
git add <file>
git add <file>
To move to the master branch:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout master
git checkout master
git checkout master
To pull updates: (Only Run from Master branch)
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git pull
git pull
git pull
To move to the newly added branch:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git checkout <branch_name>
git checkout <branch_name>
git checkout <branch_name>
To get the difference:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git diff <file>
git diff <file>
git diff <file>
To get the status:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git status
git status
git status
To save the changes:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git commit <file_name> -m "comment"
git commit <file_name> -m "comment"
git commit <file_name> -m "comment"

A commit in GIT is a state, actually the current state of files and the current commit is known as HEAD. Also, we can move the HEAD using different commands.

To commit all changes:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git commit -am "comment"
git commit -am "comment"
git commit -am "comment"
To push the changes:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git push origin <branch_name>
git push origin <branch_name>
git push origin <branch_name>

It will give a URL. Give that for merging.

To get the whole log:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git log
git log
git log
To pull all files other than the changed ones:

This needs to be done inside the branch

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git rebase master
git rebase master
git rebase master
Then, Push Your Changes:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git push -f
git push -f
git push -f
To delete a branch:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch -d <branch_name>
git branch -d <branch_name>
git branch -d <branch_name>
To delete a branch forcefully:
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
git branch -D <branch_name>
git branch -D <branch_name>
git branch -D <branch_name>

That’s it!

Also Read:

If you like the post Understanding top Command and wish to receive more articles from us, please like our FB page: GrepItOut

Your suggestions and feedbacks will encourage us and help to improve further, please feel free to write your comments. For more details on our services, please drop us an E-mail at info@grepitout.com

Topics