Git commands used by R2Devops’ team
Today, we want to share with you some git commands we use in our development’s daily tasks. We think some of these commands can help you on problems you may have. So here you can find some of them that we use for R2Devops!
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!
This command allows you to get and set a repository or global options.
$ 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 [email protected]
In the same way, the command sets the email that will be attached to your git account.
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….
$ 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.
This command allows you to clone a repository into a new directory.
$ 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.
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).
$ 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.
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
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!
This command is used to merge all changes of the current branches on the remote repository into the local directory.
$ 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.
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.
This command allows you to download objects and references from another repository.
$ 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.
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).
$ 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
This command shows the working tree status.
This command allows you to switch branches or restore working tree files.
$ git checkout Lightsaber
This command allows you to go from your current branch to the branch provided in the command.
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 --hard
It permits to reset current HEAD to the previous commit.
This command allows you to save and discard the changes on the current branch.
$ git stash pop
In this example, the command applies the previously saved changes with a “git stash” to the current branch
This command allows you to define an alias.
$ git config --global alias.[alias] [command]
The [command] can be used as [alias].
This command allows you to see commit logs.
$ git log
$ git diff commit1_id commit2_id
This command shows changes between commits, commit and working tree, etc.
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!