9 Git Tips I Wish I Knew Earlier as a Software Engineer
Written by: Christina Liang, Agile Software Engineer, TribalScale
📫 Subscribe to receive our content here.
💬 Have any questions about Git tips? Click here to chat with one of our experts!
Git is one of the most commonly used version control systems. However, it can be daunting and tricky at first with its many commands and workflows. It can be easy to get lost in the world of Git, with lines of merge conflict errors and unintended changes, Git can really be a nightmare for those new to it.
This article will introduce a few of the Git commands I found the most useful.
How to get started (Basic workflow)
Cloning a project
Click the green “Code” button found on your repository page.
Copy the URL then navigate to the location you would like your repository stored in.
Enter the command below with <repo_url>
replaced with the URL just copied.
git clone <repo_url>
Making changes
Create a new branch to work on.
git checkout -b <new_branch_name>
Make required changes and stage all changes.
git add .
Commit these changes.
git commit
Push changes to the main branch.
git push -u origin <new_branch-name> // only needed for first commit
Note: For all subsequent commits, running git push
is sufficient.
How to pull changes from the <main_branch> to your branch
If you require the newest changes on the main branch integrated on your local branch, use git merge or rebase to update the branch. For more information on the difference between merging and rebasing, check out this article.
Using rebase
git stash -u //stash all your working filesgit checkout <main_branch>git pullgit checkout <your_branch>git rebase <main_branch>git stash pop //re-apply all your changes
Using merge
git stash -u //stash all your working filesgit checkout <main_branch>git pullgit checkout <your_branch>git merge <main_branch>git stash pop //re-apply all your changes
Dealing with merge conflicts
If you see merge conflicts, do not worry. All you need to do is go through each of the conflicted files and resolve each conflict. Simply select whether you want to keep the incoming or current changes. Also, be sure to delete all of the dividers.
<<<<<<< HEAD=======>>>>>>> branch
After all the conflicts are resolved, stage all changes and push the commit.
Edit your commit message
The command below will open back up the text editor and from there you can modify the commit message.
git commit — amend
Rename your branch
What if you named your branch wrong or wanted to change it to something more fitting? The command below allows you to rename your branch.
git branch -m <new_name>
Find your branch name
Sometimes you may be working with multiple branches and it is hard to remember the exact name of each one. Running the command below will list out all the branches in your repository. To exit out of the list and return back, simply type ‘q’.
git branch
Revert your last commit
You might find yourself in the situation where you have pushed a commit but realized you left some changes out. What do you do? Well fortunately, you can simply undo your last commit.
git reset head~1
This command reverts your last commit and you will see all the changes back in the staging area. You can commit these changes again later.
See project commit history
The command below shows you the commit history.
git log
See branch history
This command displays the changes in the working directory. It shows you what changes are staged, unstaged, or untracked.
git status
Christina is an Agile Software Engineer at TribalScale. With her team, she works on React and React Native cross-platform development. In her free time, she loves to explore new cities, paint, and play basketball.
TribalScale is a global innovation firm that helps enterprises adapt and thrive in the digital era. We transform teams and processes, build best-in-class digital products, and create disruptive startups. Learn more about us on our website. Connect with us on Twitter, LinkedIn & Facebook!