Hey everyone, In this article, we will see the most used Git Bash commands and give a quick start with a cheat sheet. We already discussed in the brief of Git commands earlier Important Basic Git Commands – Developers should know.
Git helps to track and work on the same workspace that developers or organizations use to collaborate on developing applications.
So, let’s start with the Git commands.
Setup
Set the name and email that will be attached to your commits and tags
1 2 3 4 5 | $ git config --global user.name "YOUR NAME" $ git config --global user.email "myemail@gmail.com" |
Start a Project
Create a local repo (omit <directory> to initialize the current directory as a git repo)
1 2 3 4 5 6 | $ git init <directory> // Download a remote repo $ git clone <url> |
Make a Change
Add a file to the staging
1 2 3 4 5 6 | $ git add <file> // Stage all files $ git add . |
1 2 3 4 5 6 | $ git commit -m "commit message" // Add all changes made to tracked files & commit $ git commit -am "commit message" |
Basic Concepts
main or master: default development branch
origin: default upstream repo
HEAD: current branch
HEAD^: parent of HEAD
HEAD~2: grandparent of HEAD
Branches
- List all local branches.
- Add -r flag to show all remote branches.
- Use -a flag for all branches.
1 2 3 4 5 6 | $ git branch // Create a new branch $ git branch <new-branch> |
- If you want to switch to a branch & update the working directory.
1 2 3 4 5 6 | $ git checkout <branch> // Create a new branch and switch to it. $ git checkout -b <new-branch> |
- Delete a branch
1 2 3 4 5 6 7 | // Delete a merged branch $ git branch -d <branch> // Delete a branch, whether merged or not $ git branch -D <branch> |
- Add a tag to the current commit (often used for new version releases)
1 2 3 | $ git tag <tag-name> |
Merging
Merge branch one into branch two. Add no-ff option for no-fast-forward merge.
1 2 3 4 5 6 7 | $ git checkout b $ git merge a // Merge & squash all commits into one new commit $ git merge --squash a |
Rebasing
Rebase feature branch onto main(to incorporate new changes made to main). Prevents unnecessary merge commits into the feature, keeping history clean.
1 2 3 4 5 | $ git checkout feature $ git rebase main |
It cleans up a branch commits before rebasing onto the main
1 2 3 4 5 6 | $ git rebase -i main // rebase the last 2 commits on current branch $ git rebase -i HEAD~2 |
Undoing Things
- Move (or rename) a file and stage move.
1 2 3 | $ git mv <existing-path> <new-path> |
- Remove a file from the working directory & staging area, then stage the removal.
1 2 3 | $ git rm <file> |
- Remove from staging area only
1 2 3 | $ git rm --cached <file> |
View previous commit (READ only)
1 2 3 | $ git checkout <COMMIT_ID> |
- Create a new commit, reverting the changes from a specified commit.
1 2 3 | $ git revert <COMMIT_ID> |
- Go back to a previous commit & delete all commits ahead of it (revert is safer). Add –hard flag to also delete workspace changes (Be very careful)
1 2 3 | $ git reset <COMMIT_ID> |
Review your Repo
- List new or modified files not yet committed.
1 2 3 | $ git status |
- List commit history, with respective IDs.
1 2 3 | $ git log --oneline |
- Show changes to unstaged files. For changes to staged files, add –cached option.
1 2 3 | $ git diff |
- Show changes between two commits
1 2 3 | $ git diff <COMMIT_ID1> <COMMIT_ID2> |
Stashing
Store modified & staged changes. To include untracked files, add -u flag. For untracked & ignored files, add -a flag.
1 2 3 | $ git stash |
1 2 3 4 | // If you want to add comment. $ git stash save "your comment" |
- Partial stash. Stash just a single or collection of files or individual changes within the files.
1 2 3 | $ git stash -p |
- List all stashes
1 2 3 | $ git stash list |
- Re-apply the stash without deleting it.
1 2 3 | $ git stash apply |
- Re-apply the stash at index 3, then delete it from the stash list. Omit stash@{n} to pop the most recent stash.
1 2 3 | $ git stash pop stash@{3} |
- Show the diff summary of stash 1. Pass the -p flag to see the full diff.
1 2 3 | $ git stash show stash@{1} |
- Delete stash at index 1. Omit stash@{n} to delete the last stash mode.
1 2 3 | $ git stash drop stash@{1} |
- Delete all stashes
1 2 3 | $ git stash clear |
Synchronizing
- Add a remote repo
1 2 3 | $ git remote add <alias> <url> |
- View all remote connections. Add -v flag to view URLs.
1 2 3 | $ git remote -v |
- Remove & Rename a connection
1 2 3 4 5 6 7 | // Remove $ git remote remove <alias> // Rename $ git remote rename <old> <new> |
- Fetch all branches from remote repo (no merge)
1 2 3 | $ git fetch <alias> |
- To Fetch a specific branch
1 2 3 | $ git fetch <alias> <branch> |
- Fetch the remote repository copy of the current branch, then merge
1 2 3 | $ git pull |
- Move(rebase) your localc changes onto the top of new changes made to the remote repo (for clean, linear history)
1 2 3 | $ git pull --rebase <alias> |
- Upload local content changes to remote repo
1 2 3 | $ git push <alias> |
- Upload to a branch (can then pull request)
1 2 3 | $ git push <alias> <branch> |
So, this is all about the quick cheat sheet of git bash commands.
I hope you enjoyed the article and if you found it useful, please share it with your friends and colleagues. If this post helps you, then spread this so that other people can also benefit.
If you have any queries please feel free to post them in the comments section or anything that you want to ask through mail contact.
Thank you😉
Also read,
- Important Basic Git Commands – Developers should know
- All about GitHub and its link with Git – A step-by-step guide
- Introduction to Linux – Ubuntu Basic Commands
- [7] Advanced and Useful Linux – Ubuntu Commands
Awesome content😊
Thanks
Heya i’m for the first time here. I found this board and I find It really useful & it helped me out much. I hope to give something back and aid others like you helped me.
Thanks so much for the blog.
Very neat article.Really looking take up to entre more. want more.
Hello, I enjoy reading through your post. I like to write
a little comment to support you.
Now all became clear, many thanks for the information. You have very much helped me.