Skip to main content

Git commands used by R2Devops’ team

· 5 min read

Starting git commands

First of all, let’s have a look at some useful starting commands that you will need to begin your git project!

git config

This command allows you to get and set a repository or global options.

Example:

git config --global user.name DarkVador

In this example, the command set the name that will be attached to your commits and tags.

git config --global user.email DarkVadorOff@StarWars.com

In the same way, the command sets the email that will be attached to your git account.

git init

This command allows you to create an empty git repository or reinitialize an existing one. It isn't frequently used, because most of the time we create projects directly from dev platform: GitLab, GitHub, Bitbucket….

Example:

git init DeathStar

If a project directory is provided, git will create a new directory name and will initialize a repository inside it. If a project directory is not provided, then the “.git” repository is initialized in the current directory.

git clone

This command allows you to clone a repository into a new directory.

Example:

git clone https://gitlab.com/StarWars/DeathStarProject.git DeathStar

It creates a copy of an existing git repository.

If only the url is given, the repository will have the same name as the repository given by the url. If a name is provided after the url, it allows to change the repository name.


Put changes to remote

Let’s continue with some commands to put changes to remote. These commands will helps you to push modifications directly to your remote repository.

git add

This command allows you to add your file contents to the index. The index is the list of changes that you want to commit (or add to your distant repository).

Example:

git add SuperLaser

In this example, the command adds the file provided to the index.

We recommend you to add modifications file by file to make sure you add only documents you wants to.

git commit

This command is used to record changes to the repository.

The entire command that we use mostly is:

git commit -m docs: correct power of SuperLaser

It allows you to validate your latest changes. It saves all changes from the staging area. If -m [msg] is provided, it also adds your log message describing the changes. For log message, we follow the Conventional commit convention.

git push

git push

This command is used to load the content from a local repository to a remote one.

Get changes from remote

Now that you’ve seen how to put changes to your repository, it’s time to see how to get them!

git pull

This command is used to merge all changes of the current branches on the remote repository into the local directory.

Example:

git pull DeathSquadron Executor

If a specific branch is provided, it fetches the branch copy given in the command. If not, this command fetch the current branch copy of the specified repository and immediately merge it into the local copy.

git merge

This command joins two or more development histories together.

git merge Lightsaber

It merges the current branch with the merge branch provided in the command.

git fetch

This command allows you to download objects and references from another repository.

Example:

git fetch DeathSquadron Executor

If [branch] is not provided, it will fetch all branches of the repository. It also downloads all necessary commits and files from the other repository. If [branch] is provided, it fetches only the provided branch.

git rebase

The git rebase command is one of the two git utilities specialized in the integration of changes from one branch to another (the second being git merge).

Example:

git rebase -i Star_Destroyer/Resolute

This command allows you to rewrite commits from one branch to the current branch. Essentially, git is deleting commits from one branch and adding them to another.


The most useful git commands

Now that you know the basics of git commands, let's focus on the commands that the R2Devops’ team finds the most useful!

git status

git status

This command shows the working tree status.

git checkout

This command allows you to switch branches or restore working tree files.

Example:

git checkout Lightsaber

This command allows you to go from your current branch to the branch provided in the command.

git branch

This command is used to list, create, or delete branches.

git branch Blaster

It allows you to create a branch named [Blaster]. This operation does not check out the new branch. If a specific branch is not provided, the command will list all the branches in your repository.

git branch -d Disruptor

This command is used to delete the specified [Disruptor] branch in the local repository. This is a "safe" operation, meaning git prevents you from deleting the branch when it contains unmerged changes.

git reset

git reset --hard

It permits to reset current HEAD to the previous commit.

git stash

This command allows you to save and discard the changes on the current branch.

Example:

git stash pop

In this example, the command applies the previously saved changes with a “git stash” to the current branch

git alias

This command allows you to define an alias.

Example:

git config --global alias.[alias] [command]

The [command] can be used as [alias].

git log

This command allows you to see commit logs.

Example:

git log

git diff

git diff commit1_id commit2_id

This command shows changes between commits, commit and working tree, etc.

Want more?

R2 on discord

Of course, this list doesn't contain all the existing Git commands. If you want to complete it, or want more information about Git commands, join us on Discord!