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
To create Git repository locally
git init
Download/Clone repository from remote
git clone {{REPOSITORY_LINK}}
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
Create a new branch
git branch {{BRANCH_NAME}}
Delete a branch
git branch -d {{BRANCH_NAME}}
Switch to another branch
git switch {{BRANCH_NAME}}
Merge a branch to current branch
git merge {{BRANCH_NAME}}
Sync branches
git fetch
Cleanup all local branches
Before fetching, remove any remote-tracking references that no longer exist on the remote.
git fetch --prune
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
Add a file to stage
git add {{FILE}}
Add all files to stage
git add .
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}}*
Reset/undo file changes
git reset {{FILE}}
Reset/undo all changes
git reset
Git Update
Update changes to remotes
Commit changes
git commit -m {{"YOUR_COMMIT_MESSAGE"}}
Get changes from remote
git pull
Get changes from different branch
git pull origin {{BRANCH}}
Updates changes to remote
git push
Updates changes to different branch
git push origin HEAD:{{BRANCH}}
Git Undo
Undo/revert changes from git
Reset/undo local changes
git reset --hard
Reset to previous heads
git reset --hard HEAD~{{NUMBER}}
Revert changes
git revert head
Revert a commit
git revert {{COMMIT_ID}}
Git Helps
To get git command help
git help
To see commit history
git log
View commit log as a graph
git log --graph --oneline
View commit log as a graph of all branches
git log --graph --oneline --all
Show changes between commits, commit and working tree,
git diff
Remove tracked files from the current working tree
git rm {{FILE}}
Top comments (4)
Cheat sheets:
Thank you
I believe the git details are stored in home folder of user, by name .gitconfig
.npmrc stores the authorization token from npmjs.com
Yes, You are right @vishal Raj
Thank you for the correction