Here is a comprehensive list of important Git commands, organized by functionality:
Initialize a new Git repository:
git init
Note: When you do 'git clone' the git init automatically happens.
Configure user name and email:
git config --global user.name "Your Name"
git config --global user.email "youremail@vnrvjiet.in"
Note: These names are saved during every 'git commit' as metadata.
View current configuration:
git config --list
git clone <repository-url>
git remote add origin <repository-url>
git remote -v
git status
git add <file>
Add all files:git add .
git commit -m "Commit message"
git rm <file>
git mv <old-name> <new-name>
git branch
git branch <branch-name>
git checkout <branch-name>
git checkout -b <branch-name>
git merge <branch-name>
git branch -d <branch-name>
git pull
git push
git push --set-upstream origin <branch-name>
git merge --abort
git rebase --continue
git pull
Do?When you run git pull
, Git performs two actions:
By default, Git uses a merge strategy.
pull.rebase true
Do?When you set pull.rebase
to true
, Git will rebase your local commits on top of the fetched changes instead of merging them. This results in a cleaner, linear commit history.
Without Rebase (Default Merge Strategy):
A---B---C (remote/main)
\
D---E---F (local/main)
After git pull
(merge):
A---B---C---M (merged commit)
\
D---E---F (local/main)
With Rebase:
A---B---C (remote/main)
\
D---E---F (local/main)
After git pull
(rebase):
A---B---C---D'---E'---F' (rebased commits)
git log
Compact history:git log --oneline
git show <commit-hash>
git diff
git checkout -- <file>
git reset <file>
git reset --hard <commit-hash>
git stash
git stash list
git stash apply
git stash drop
git tag <tag-name>
git push origin --tags
git fetch
git push origin --delete <branch-name>
git blame <file>
git revert <commit-hash>
git cherry-pick <commit-hash>
To make your Git workflow faster, you can create aliases:
git config --global alias.st status
git config --global alias.ci commit
git config --global alias.br branch
git config --global alias.co checkout
git config --global alias.lg "log --oneline --graph --decorate"
This list covers most common Git commands. Let me know if you'd like an explanation of any specific command or concept!