Git, an open source distributed version control system has been helping developers over a decade now. It is a great tool for tracking source code changes during software development. However, there are a lot of git commands, and this article lists the most important ones that will help your team get work done efficiently. Git Branching Switching to a different branch checkout git 'branch_name' Creating a new branch and switching to it git checkout -b 'new_branch_name' If you want to switch to another branch by losing all the local changes. You should be careful while using this command git checkout -f 'branch_name' Deleting a local branch git branch -d git branch -D # Use this if the branch is fully merged to the upstream 'branch_name' # This will delete the branch regardless of its merge status. 'branch_name' Deleting a remote branch. Before using this command, you must first confirm the remote branch name with git branch -a. git push origin --delete 'remote_branch_name' Renaming a local branch. Remember, you cannot use this command to rename an empty branch. git branch -m 'old_name' 'new_name' Renaming a remote branch git push origin : 'old_name' 'new_name' Git Committing Committing local changes git commit -m git commit -a "A descriptive commit message" # Use this to automatically stage files that have been modified and deleted, but new files you have not told Git about are not affected. Undo the last local commit. Sometimes you can commit changes accidentally then later you realize you haven't added some other feature or you committed unwanted changes. This git command will help you reset the commit without losing your changes. git reset --soft HEAD^ git reset --soft HEAD~1 # Or you can specify with a figure To destroy the commit with all its data, use the same command above with the flag. Warning: --hard git reset --hard HEAD^ Delete / Undo the last remote commit The commands above are for local changes. Sometimes you realize a mistake after pushing the local changes to the remote branch. These commands can help you get rid of those changes completely. : Warning Make sure you have backed up your local changes. git reset --hard HEAD^ git push origin -f # Undo the local changes first # Perform a force git push to affect the remote branch Git Remote Repositories Adding a remote repository URL git remote add origin 'repository_url' Adding multiple remote repositories. In this case, you may have a repository hosted by both GitHub and Bitbucket. You push changes to both repositories. Let's set it up. Let's rename the GitHub's default origin. git remote rename origin github # Rename the origin Add another remote repository, say, from bitbucket. git remote add bitbucket 'bitbucket_repository_url' So, we have now two remotes defined; github and bitbucket. To verify the changes, run git remote command with the flag. -v # Verify changes git remote -v #> github (fetch) #> github (push) #> bitbucket (fetch) #> bitbucket (push) 'github_repository_url' 'github_repository_url' 'bitbucket_repository_url' 'bitbucket_repository_url' Now all the remotes have been set. After committing your changes, you will push to both remotes. git push github HEAD git push bitbucket HEAD # Pushing to GitHub # Pushing to Bitbucket You can also pull from the remotes git pull github git pull bitbucket # Pull from GitHub 'branch_name' # Pull from Bitbucket 'branch_name' Saving data to a different branch Sometimes, you can make changes in a wrong branch and deleting such changes would be costly. In this case, you can use for saving your local changes locally and then pick them later for use. git stash Stash the changes git stash Or you can save your stash with a descriptive name. git stash save # Give a descriptive name "My recent changes" #> Saved working directory and index state On 'branch_name': My recent changes After saving your stash, you can view the stash list using the flag. list git stash list # Stash list #> stash@{0}: On 'branch_name': My recent changes Now, all the your changes have been stashed. Use the the above commands for switching to a desired branch where you would like to save your changes then retrieve your changes using the command below. git stash pop stash@{0} # Retrieve the changes #> Dropped stash@{0} (stash_hash_ID) Conclusion The commands I have covered in this article may help in smooth collaboration in your team. There are lots of git commands in place. For more advanced commands, visit . Git Reference Happy Gitting! 👨💻😊