DEV Community


Posted on

Git Commands

If you are new to using Git and GitHub, this article might be helpful. Here, I am going to list the most frequently used Git commands that I use:

git init - Initialize git. The very first thing you do to initialize your local directory to a git repository.
git status - Lists any changed files within the directory.
git add . - Adds all the unstaged files that have been changed to staged status and makes the files ready to be committed. If you want to add a specific file only you can do so by using the git add <file-path/name-of-file> command.
git commit -m “<your message>” - Commits all the staged files with the message provided. It's important to write meaningful commit messages so that it can be easily understood as to what accepting the changes will do.
git commit -am “<your message>” - Add and commit all changed files at once.
git reset --soft HEAD~ - Undo the last commit.
git push - Pushes all commits to the main branch. If you want to push to a specific branch you can do so by using the git push <name of remote> <name of branch> command.
git branch - Shows all the branches.
git branch <branch name> - Creates new branch.
git checkout <branch name> - Switch to branch.
git checkout -b <branch name> - Create and switch to the new branch at once.
git branch -m <branch name> <new branch name> - Rename branch name.
git branch -D <branch name> - Deletes branch that is not merged yet.
git branch -d <branch name> - Deletes branch that is already merged.
git log - Shows all the commits.
git diff - Compares modified files.

This is a good starting place to learn about git commands, but there are many more commands that are not listed above that I haven’t used much. Besides these commands, I learned another command a few weeks back that I found very helpful if you use certain git commands frequently.

git config --global status - Creates an alias(shortcut) for git status. Once this is done, the next time you want to run git status you can just do st. It will save some time. If you want to create shortcuts for other commands you can do the same steps but instead of status you will need to add alias.<shortcut-name-you-want-to-give>< command-you-want-to-add-shortcut-to>. For example, if we wanted to add the shortcut gp for git push, we can do so by typing git config --global push.
I hope this is helpful. Have a good Sunday!

Top comments (0)