Top 10 git commands for developers
git pull —rebase
It is so easy to get merge conflicts in a professional environment where one codebase is being touched by 2-5 developers at any given time. This command is a savior and I always start any task on a shared codebase by running this command first. Now if you ask me why can't we do git pull? To confess, as a beginner to git( which I still am!), I used to do this a lot and spent or rather wasted a lot of my time resolving the merge conflicts that this command has caused me!
Just remember
git pull = git fetch + git merge
git pull --rebase = git fetch + git rebase
To understand more on the differences, check out this article by Atlassian.
git log
This command is so useful in finding out the current status of the codebase in your local workspace. I regularly use this command to check if I have the latest commits from the main branch and track my past commits as well.
git status
This is an indispensable git command to know the current status of your code changes especially while you are actively making code changes in your workspace. Many times we make code changes to a file and completely forget about it. Git status reminds me which files to add to stage and for which files, I need to revert my code changes, if I need to. I don't think I can ever create a git commit before running git status. :D
git add?
Obviously, because how else are we going to push the code to the codebase! This command is for adding all your changes to the staging area.
git commit —m "meaningful commit message"
This command saves your changes to the local repository with a commit message. Also, how do you write your commit messages? Thankfully, I'm beyond 'this is my initial commit' messages :D. Throughout my professional experience, I learned the importance of meaningful commit messages and I stick to following a commit message template of "what changed, why changed and how you ensured the change worked". It's easier for my peers to read through the commits and get a gist of the code changes that I made.
git commit —amend —no-edit
This is my favorite. Its a good practice to not have multiple commits in our code reviews. The commits should either be squashed or never have more than one commit. This command saves me a lot of time. When I have already created a commit but again go ahead and make more changes in my local workspace which can be a part of this commit, instead of making another commit, I amend the existing one. But I don’t want to change my commit message. So, I can just use this command. Saves me a lot of time!
git push
This command moves the changes from the local repository to a?remote server and brings you to a final step to have your code in production.
git rebase -i HEAD~n followed by pick and squash
Say you want to squash few of your commits. Why? Because it's a good practice to maintain clean commit history. When your project becomes increasingly large and complex, this becomes very important. Having multiple commits in your pull requests is messy.
git branch
Shows all your branches. (the asterisk denotes the current branch). git branch -a shows all remote branches.
git checkout -b new_branch_name
This is used to create a new branch for your feature. It’s a good practice to name your branch appropriately. For example, if you are creating a homepage for your website, you create a branch so that you do not mess up with your main branch. You can name it, homepage.
git diff
I know I said 10, but I couldn't miss this one. Simply put, git diff is used to show the changes made by you in your the current workspace.
What are the top git commands you use?
MITACS Globalink Research Intern '24 @ University of Guelph ???? | TEEP Scholar '24 @ Asia University, Taiwan ???? | Founding Member, INKHUB Tattoos | JU IEE25 | Trader | Coder | Entrepreneur
2 年Amazing ??
Senior SDET- AI/ML @ Reltio
2 年Awesome