Collection of Handy Git Commands
- Published on
- • 4 mins read
Intial setup after installing Git on the machine :
setup the git user details for the currently logged in user on your machine using --global
flag.
# set a name of git user
git config --global user.name “<Firstname Lastname>”
# set an email address that will be associated with each history marker
git config --global user.email “<Your Email ID>”
# set automatic command line coloring for Git for easy reviewing
git config --global color.ui auto
Initialize git repository :
git init
Cloning remote git repository to local :
git clone <repo_url>
Preventing unintentional staging or commiting of files :
create .gitignore file in the repo directory to exclude unnecessary or secret files from commiting
Example -
/node_modules
# debug
*.log
npm-debug.log*
yarn-debug.log*
yarn-error.log*
# .env
.env
Working with snapshots and the Git staging area:
# show modified files in working directory, staged for your next commit
git status
# add a file as it looks now to your next commit (stage)
git add <file> # "git add ." will add everything to staging area
# commit your staged content as a new commit snapshot
git commit -m "<descriptive message>"
# unstage a file while retaining the changes in working directory
git reset <file>
# diff of what is changed but not staged
git diff # adding "--staged" flag will only show what is staged & not committed
Note: Not explaining git reset --hard
commands, cause it's a danger zone.
Work with those at your own risk & wisdom!!
Branching in Git :
# list your branches. a "*" will appear next to the currently active branch
git branch
# create a new branch at the current commit
git branch <branch_name>
# switch to another branch and check it out into your working directory
git checkout <branch_name>
# shorthand command to create a new branch as well as checkout to it
git checkout -b <branch_name>
# merge the specified branch’s history into the current one
git merge <branch_name>
# show all commits in the current branch’s history
git log
Temporarily store modified, tracked files in order to change branches :
# Save modified and staged changes
git stash
# list stack-order of stashed file changes
git stash list
# write to working from top of stash stack and pop it out of the stash
git stash pop
# discard the changes from top of stash stack
git stash drop
# delete/empty the complete stash stack
git stash clear
Working with Remore and local repositories :
# add a git URL of remote repo as an alias
git remote add <alias> <remote_url> # e.g. git remote add origin <github_url>
# fetch down all the branches from that Git remote
git fetch <alias>
# merge a remote branch into your current branch to bring it up to date
git merge <alias>/<branch>
# Transmit local branch commits to the remote repository branch
git push <alias> <branch>
# fetch and merge any commits from the tracking remote branch
git pull
Renaming a file/directory within git repository :
Simply renaming by IDE or file manager will not work because git will not recognize the change.
You can do it with the following command:
git mv readme.md README.md
Then follow with the usual git workflow:
git add .
git commit -m "Rename readme.md to README.md"
Git Squash :
When we say squash
in Git, it means to combine multiple continuous commits into one.
Here's the syntax to squash the last X commits using interactive rebase:
git rebase -i HEAD~X
# Note: this command opens vi/vim editor, you can change 'pick' to 'squash' for whichever commits you want to squash.
# Now to push the commit changes to the remote repo
git push -f origin <branch_name>
Git Cherry-Pick :
This lets you apply a commit from one branch to any other branch. Useful for undoing the accidently commited changes.
git cherry-pick <commit-id>
That's it! Now you can move ahead with your version control journey.
Happy commiting!