Alex de Pablos
Software engineering

Most common GIT commands

Alex de Pablos Lopez7 min

The best way to learn Git commands is to use them. There is no doubt about that. Although you look into the best of the guides, if you find that command that you need now, but you will never use it again… it is quite likely that your head will retain it for the shortest possible time.

That said, it never hurts to know what the most used or most common Git commands are because it has an infinite number of them with their different options and usages, and it can become overwhelm if you are just starting out into this world.

If you’re new to Git or need a refresher on its fundamentals, I suggest starting with my Introduction to GIT, where I cover the basic concepts and guide you step by step.

Go for it!

Most common GIT commands

Basic configuration

The ~/.gitconfig file contains Git configuration information. This configuration can be changed by editing the file or by running certain commands that Git provides.

Set user data

git config --global user.email "your_email@example.com"
That command tells Git that the email will be your_email@example.com.

git config --global user.name "John Doe"
That command tells Git that the name will be John Doe.

Set default branch

git config –global init.defaultBranch main
That command tells Git that the default branch will be main.

Set your favorite editor to write the commits

git config --global core.editor "code --wait"
That command tells Git that for operations like commits or tags, the editor to use is the VSCode editor.

List configuration data

git config --list
That Git command will display on the screen the data that has been configured so far.

Generate utility aliases

git config --global aliases.co checkout
That Git command creates the alias co to indicate that it will do what the actual checkout command does.

You can create as many aliases as you like to make your life easier when executing Git commands.

Start a repository

Start a new repository

git start
That Git command starts a Git repository at the command execution location.

Clone an existing repository

git clone
That Git command clones the repository from the url to the command execution location.

Add files to staging area

git add -A This Git command adds all the files with changes to the so-called stage or work area, from where the images will be generated later (commit).

Create a picture of the changes

git commit -m "Commit message"
That Git command generates the photo of the current state of the workspace and will put the indicated message on it.

Show file status

git status This Git command will display the status of the local repository on the screen, listing the files that have been changed, those that have already been added to the stage or work area, those that have not yet been added… etc.

git log --online That Git command will display on the screen the different commits made with your message.

Upload to remote the changes of our local

git push origin
That Git command will push the changes from your local repository to the remote branch.

Manage branches in Git

list branches

git branch That Git command lists the branches of the local repository.

git branch -a That Git command lists the local and remote repository branches.

create branch

git branch That Git command creates the branch in the local repository.

delete branch

git branch -D That Git command removes the branch from the local repository even though the branch has changes that haven’t been merged.

merge branches

git merge That Git command merges the indicated branch into the active branch.

Simple use case where a new branch is created, 2 commits are added and then they are integrated into the main branch.

# Start a new feature from the main branch git checkout -b new-feature main # Add the file file that has been edited to the work area and commit git add git commit -m "Start a feature" # Add the file file2 that has been edited to the work area and do the commit git add git commit -m "Finish a feature" # Switch to the main branch git checkout main # Merge the new-feature branch into the current branch (main) git merge new-feature # Remove the new-feature branch git branch -d new-feature

When making merges between branches, it is common for conflicts to appear that need human intervention to be resolved. Being something more advanced, it will not be developed here, but you can go to this Atlassian link where they explain more about the subject.

Manage remote repositories

View remote repositories

git remote -v That Git command lists the remote repositories along with their URLs.

View remote repository branches

git remote show origin That Git command lists the branches of the remote repository.

Connect local repository to remote server

git remote add origin That Git command adds the local repository to the server.

Switch remote repository

git remote set-url origin That Git command changes remote server, pointing to the new url indicated.

Disconnect local repository from remote server

git remote rm That Git command disconnects the local repository from the remote server.

Update local repository

git pull That Git command updates the local branch with any changes on remote.

It’s something we should always do before trying to remote push any changes.

Revert changes

Sometimes we need to revert changes we have already made. These commands have to be used carefully so as not to lose information that we did want. With the command below we make sure that this does not happen.

git revert That Git command will take the image of the specified commit, reverse the changes, and create a new commit.

The git revert command is an advanced undo operation that provides a safe method of reverting changes. Instead of deleting or orphaning commits in the commit history, a revert will create a new commit that reverses the specified changes.

Increase productivity using Git

I’m a bit of an idiot, and remembering what commands to use is a very, very difficult task for my poor memory.

So if you’re one of my kind, you might still be interested in reading this section, where I’m going to show you a little ‘hack’ to perhaps make your life a little easier when you use git.

It is about creating certain aliases that will make you not have to remember commands with long options. Aliases are something that can be configured as we saw in the Generate Utility Aliases section.

git config --global aliases.mainbranch "!git remote show origin | sed -n '/HEAD branch/s/.*: //p'" git config --global aliases.synced "!git pull origin $(git mainbranch) --rebase'" git config --global aliases.update "!git pull origin $(git rev-parse --abbrev-ref HEAD) --rebase" git config --global aliases.squash "!git rebase -v -i $(git mainbranch)" git config --global aliases.publish "push origin HEAD --force-with-lease" git config --global aliases.pub "publish"

Publish your branch changes to the repository remote

git publish or git pub would publish the changes present in your local branch to the remote repository. Remember that before publishing changes you should always have done a pull and resolve any conflicts.

Sync your local branch with remote main

git synced would keep your branch up to date with changes from remote’s main or master branch. I would do this by doing a fetch and then rebase, putting your branch commits after the last commit present on remote.

Sync your local branch with changes from the same remote branch

git update would bring to your local the changes present in the same remote branch.

Unify commits

git squash gives you the opportunity to rewrite commits into perhaps more sensible or defined messages before doing the final merge.