DEV Community

Cover image for GIT Commands
Vineeth.TR
Vineeth.TR

Posted on

GIT Commands

Some easy useful git commands

Git configuration

You can configure git by creating a .gitconfig file in user folder
or use commands


git config --global user.name {{YOUR_USERNAME}}

git config --global user.email {{YOUR_EMAIL}}

git config -l

Enter fullscreen mode Exit fullscreen mode

To create Git repository locally

git init
Enter fullscreen mode Exit fullscreen mode

Download/Clone repository from remote

git clone {{REPOSITORY_LINK}}
Enter fullscreen mode Exit fullscreen mode

Git branch

Git branches are effectively a pointer to a snapshot of your changes. When you want to add a new feature or fix a bug—no matter how big or how small—you spawn a new branch to encapsulate your changes

View all branches

git branch
Enter fullscreen mode Exit fullscreen mode

Create a new branch

git branch {{BRANCH_NAME}}
Enter fullscreen mode Exit fullscreen mode

Delete a branch

git branch -d {{BRANCH_NAME}}
Enter fullscreen mode Exit fullscreen mode

Switch to another branch

git switch {{BRANCH_NAME}}
Enter fullscreen mode Exit fullscreen mode

Merge a branch to current branch

git merge {{BRANCH_NAME}}
Enter fullscreen mode Exit fullscreen mode

Sync branches

git fetch
Enter fullscreen mode Exit fullscreen mode

Cleanup all local branches

Before fetching, remove any remote-tracking references that no longer exist on the remote.

git fetch --prune
Enter fullscreen mode Exit fullscreen mode

Git stage

Before we make a commit, we must tell Git what files we want to commit (new untracked files, modified files, or deleted files) this is called staging.

To know the file status/changes

git status
Enter fullscreen mode Exit fullscreen mode

Add a file to stage

git add {{FILE}}
Enter fullscreen mode Exit fullscreen mode

Add all files to stage

git add .
Enter fullscreen mode Exit fullscreen mode

Add only certain files starts with

With the asterisk command , you can add all files starting with 'index' in the staging area.

git add {{FILE_STARTS_WITH}}*
Enter fullscreen mode Exit fullscreen mode

Reset/undo file changes

git reset {{FILE}}
Enter fullscreen mode Exit fullscreen mode

Reset/undo all changes

git reset
Enter fullscreen mode Exit fullscreen mode

Git Update

Update changes to remotes

Commit changes

git commit -m {{"YOUR_COMMIT_MESSAGE"}}
Enter fullscreen mode Exit fullscreen mode

Get changes from remote

git pull
Enter fullscreen mode Exit fullscreen mode

Get changes from different branch

git pull origin {{BRANCH}}
Enter fullscreen mode Exit fullscreen mode

Updates changes to remote

git push
Enter fullscreen mode Exit fullscreen mode

Updates changes to different branch

git push origin HEAD:{{BRANCH}}
Enter fullscreen mode Exit fullscreen mode

Git Undo

Undo/revert changes from git

Reset/undo local changes

git reset --hard
Enter fullscreen mode Exit fullscreen mode

Reset to previous heads

git reset --hard HEAD~{{NUMBER}}
Enter fullscreen mode Exit fullscreen mode

Revert changes

git revert head
Enter fullscreen mode Exit fullscreen mode

Revert a commit

git revert {{COMMIT_ID}}
Enter fullscreen mode Exit fullscreen mode

Git Helps

To get git command help

git help
Enter fullscreen mode Exit fullscreen mode

To see commit history

git log
Enter fullscreen mode Exit fullscreen mode

View commit log as a graph

git log --graph --oneline
Enter fullscreen mode Exit fullscreen mode

View commit log as a graph of all branches

git log --graph --oneline --all
Enter fullscreen mode Exit fullscreen mode

Show changes between commits, commit and working tree,

git diff
Enter fullscreen mode Exit fullscreen mode

Remove tracked files from the current working tree

git rm {{FILE}}
Enter fullscreen mode Exit fullscreen mode

Top comments (4)

Collapse
 
arvindpdmn profile image
Arvind Padmanabhan
Collapse
 
vineethtrv profile image
Vineeth.TR

Thank you

Collapse
 
vishalraj82 profile image
Vishal Raj

I believe the git details are stored in home folder of user, by name .gitconfig
.npmrc stores the authorization token from npmjs.com

Collapse
 
vineethtrv profile image
Vineeth.TR

Yes, You are right @vishal Raj
Thank you for the correction