Most Useful Git Commands

Git is a distributed version control system. Every dev has a working copy of the code and full change history on their local machine, and can easily share and undo changes to subscribers all over the world. It is a critical part of the development cycle, and all developers will need to use it in some form. Starting out, git can be daunting as it requires terminal and typed commands. A high bar for new developers.

This article will introduce a few of the Git commands I found the most useful.

How to get started (Basic workflow)

Cloning a project

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 files
git checkout <main_branch>
git pull
git checkout <your_branch>
git rebase <main_branch>
git stash pop //re-apply all your changes

Using merge

git stash -u //stash all your working files
git checkout <main_branch>
git pull
git 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 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, 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

Discover more from AJB Blog

Subscribe to get the latest posts sent to your email.

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.