DEV Community

Cover image for Git commands: basic to advanced
Jason Hornet
Jason Hornet

Posted on

Git commands: basic to advanced

I know there are thousands of articles like this out there, but how could I do a series of posts about Git and GitHub without talking about the commands?


In our day-to-day life these commands are used very often.

Basic Commands

  • git config

When you are using Git for the first time in a new installation or in a collaborative project, this command is essential to configure your user identity, the name and email information will be used in each commit.

$ git config –global user.name “name”

$ git config –global user.email “email”


  • git init

The command will create a new blank repository and from there you will be able to store your source code, change it, save changes, etc.

$ git init <repository>


  • git clone

This Git command creates an exact copy of an existing repository.

$ git clone <project URL>


  • git add

This Git command adds the specified files to your repository, whether they are new files or previous files that have changed. Offers different syntax possibilities.

$ git add <file name> this command will add the specific file to the repository

$ git add this command will add all new and/or modified files to the repository


  • git commit

It's critical to establish a difference between git add and git commit: git add will put your modified files to the queue to be committed later. The files have not been committed yet.

It is possible to combine the two commands into a single one: $ git commit -a or $ git commit -m "message"


  • git branch

It is common most of the time to have multiple variations in your Git repository, called branches.

At first it might seem easy to get lost in many paths, but the git branch command makes it easy to manage it all. With different parameters, it is possible to list, create or delete branches.

$ git branch (lists all branches)

$ git branch <name> (creates a branch with the specified name)

$ git branch -d <name> (deletes the branch with the specified name)


  • git checkout <nome_do_branch>

Still on branches, this Git command can be used to switch from one branch to another.


Intermediate Commands

  • git remote add <name> <url>

This command establishes a connection between your local repository and a remote repository.


  • git push -u <remote> <branch>

This command is used to upload your modifications to a remote repository previously connected with git remote.


  • git fetch

When you need to download changes created by other members of your collaborative project, you need the Git fetch command. From this command, you will receive all commit information to evaluate before applying these changes to your local version of the repository.


  • git pull <URL>

The Git pull command pulls the contents of what has changed in the remote repository to your local repository and updates it to the latest version.


  • git stash

This Git command temporarily stores your modified files in an area called a stash, without interacting with the other files until needed.

To list them all we use:

$ git stash list

When it's time to apply the contents of the stash to a branch, we use:

$ git stash apply


  • git show <commit_hash>

Want specific details about a commit that the log doesn't show? The Git show command is the answer.


  • git rm <file_name>

To remove files from your folder, you can use the git rm command.


  • git merge <branch>

This Git command integrates changes from two different branches into a single branch. It needs to be started from an already selected branch, which will be merged with another one, with the name passed as a parameter.


Advanced Commands

  • git rebase <base>

Git rebase at first seems to do the same thing as merge: it merges two branches into a single branch. However, this command retraces the commit history, making it linear. It is best suited for consolidating multiple branches.


  • git pull –rebase

This is a variation of the pull command shown earlier. From this instruction, Git will do a rebase (not a merge) after using a pull command.


  • git cherry-pick <commit_hash>

This is a powerful command that lets you select a specific commit from one branch and apply it to another branch, without needing to do a full merge. The operation is added to the history.


  • git archive –format zip HEAD > archive-HEAD.zip

This Git command combines multiple files into a single file, much like a zipped file. This package can be opened later and the contained files can be extracted individually.


  • git blame <file_name>

The blame command helps to determine which user made which change in a given file.


  • git tag -a <tag_name>

Tags are a good option to mark a branch and avoid changes, especially in public releases.

For best practices a tag is always created as a version $ git tag -a vX.X.X


  • git diff

To compare two git files or two branches before they undergo a commit or a push, it is important to run this Git command.

comparing active repository with local repository: $ git diff HEAD <file_name>

comparing two branches: $ git diff <origin branch> <target branch>


  • git citool

This Git command provides a "graphical interface" to commit.


  • git whatchanged

This command provides log information, in raw format.


Extra

  • git help <command_that_you_need_help>

There are countless commands in Git, many more than those in this list, each with its own function, parameters and characteristics. Fortunately, Git itself has a help command to bring up help directly in the terminal.

Top comments (0)