
Git Command Guide
Some interesting Git commands to guide you in versioning your projects
If there's one thing that's easy to forget, it's Git commands. This is because it's in moments of greatest distress that we need to remember a specific command we rarely use to solve the problem. Therefore, I decided to write this article to serve as a guide to basic commands for those looking for a quick and simple solution to avoid wasting time and focus better on the project.
What is Git
Git is a distributed version control system designed to manage software projects of all sizes and complexities. The main goal of Git is to help developers collaborate on software projects, allowing them to track changes in the source code, coordinate teamwork, and revert to previous versions when necessary.
With Git, it's possible to maintain a complete record of all changes made to the source code, including information about who made the changes, when they were made, and why. This is fundamental for teamwork, as it allows developers to work in their own development branches, combine their changes with those of other developers (merge), and review each other's changes.
Furthermore, Git also offers features such as:
-
Code branching: Developers can create separate development branches to work on specific features or bug fixes without interfering with the work of other developers.
-
Code merging: When developers finish their work in a development branch, they can merge the changes into other development branches or the main project branch.
-
Code rollback: If something goes wrong during development, developers can easily revert to a previous version of the source code and fix the problem.
-
Change history: Git keeps a complete record of all changes to the source code, allowing developers to see exactly when and why a change was made.
These features are extremely valuable for team software development, helping developers work more efficiently, in an organized and collaborative way. Therefore, Git has become a fundamental tool for developers and development teams worldwide.
Configuration Commands
Below are three commands to configure Git on your machine. The first is used to define the username that will make commits, the second is used to configure the commit email. Other possible configurations can be listed using the third command.
git config --global user.name "Sávio Andres"
git config --global user.email mail@savio.pw
git config --list
Basic Commands
- git init: Creates a new empty Git repository.
- git add: Adds a file or directory to be tracked by Git.
- git add .: Adds all files.
- git add filename.xyz: Adds a specific file.
- git add directory: Adds an entire directory (folder).
- git commit: Creates a new commit in the repository with all the changes that were added using the git add command.
- git commit -m "Comment": Creates the commit with a comment about the changes that were added.
Remote Synchronization Commands
- git remote: Adds a remote repository.
- git remote add origin <address>.git: origin was the name given to the remote repository, but it can be different.
- git remote rename origin project-x: renames the remote repository from "origin" to any other name, in this case, "project-x".
- git remote rm project-x: removes the remote repository from the local repository.
- git fetch: retrieves new updates from the remote repository and updates the local branch and tag log.
- git push: sends commits to a remote repository.
- git push origin master: name given to the remote repository and the name of the branch to which you want to send the commits.
- git pull: retrieves changes from the remote repository and merges them with local changes.
- git pull origin master: name given to the remote repository and the name of the branch to which you want to retrieve the changes.
- git clone: creates a local copy of a remote repository.
- git clone <address>.git: address of the remote repository.
Change Commands
- git status: Shows the current state of the repository, including modified files, added files, and pending commits.
- git diff: Displays the differences between the current code and the last commit made on the current branch.
- git diff commitA commitB: Compares the differences between two specific commits.
- git diff branchA branchB: Displays the differences between two different branches.
- git log: Displays a complete log of all commits in the repository.
- git log --author="Author Name": Filters the commit history by the specified author.
- git stash: Temporarily saves changes that are not ready for a commit.
- git stash save "Stash Name": Creates a new stash with a descriptive name.
- git stash list: Lists all stashes created in the repository.
- git stash apply: Applies the most recent stash to the working directory, keeping the stash available for future use.
- git stash apply stash@{2}: Applies a specific stash (identified by index or name) to the working directory.
- git stash pop: Applies the most recent stash to the working directory and removes it from the list of stashes.
- git stash drop stash@{1}: Removes a specific stash from the list of stashes.
- git stash branch branch_name: Creates a new branch from the most recent stash and applies the stash to the new branch.
- git reset: Removes changes from the working directory and index, but preserves the commit history.
- git reset --soft HEAD~1: Undoes the most recent commit, moving the changes to the working directory, leaving the changes in the index.
- git reset --hard HEAD~3: Undoes the three most recent commits, completely discarding changes from the working directory and index.
- git reset --mixed HEAD~2: Undoes the two most recent commits, removing changes from the working directory but keeping changes in the index.
- git revert
: Creates a new commit that undoes the changes made by the specified commit, reverting the modifications to the previous state. - git revert HEAD: Undoes the most recent commit, creating a new commit that reverts the changes made by it.
- git tag: Creates, lists, or deletes tags. "git tag" lists all existing tags in the repository.
- git tag tag_name: Creates a new tag with the specified name. For example, git tag v1.0.0 creates a tag called "v1.0.0".
- git tag -d tag_name: Removes the specified tag from the local repository.
- git tag tag_name commit: creates a tag on the specific commit.
Branch Commands
- git branch: creates, renames, or deletes branches. "git branch" lists all branches present in the repository.
- git branch branch_name: creates a new branch with the specified name.
- git branch -d branch_name: removes the specified branch from the local repository.
- git checkout: switches to a different branch or to a specific commit.
- git checkout -b new-branch: creates a new branch named "new-branch".
- git checkout branch_name: switches branches.
- git merge: merges the changes of one branch into another.
- git merge branch_name: merges the specified branch into the current branch;
Example Commands
Let's say you want to create a local repository (on your own machine) and then push the commits to a remote repository, which could be GitHub, GitLab, or any other Git repository on a server.
git init
git add .
git commit -m "Comentário"
git remote add origin <endereco>.git
git push origin master
After configuring the remote, you don't need to enter the command "git remote add origin <address>.git" again. When necessary, simply specify the chosen name for this remote, which in this case was "origin".
Basic Demonstration
Below is a short video demonstrating the use of some basic Git commands:
Git Ignore
To have Git ignore certain files so they are not committed, simply create a file named To use the .gitignore file, add the names of the files, directories, or files of a specific extension to be ignored. To ignore files of a specific extension, simply write *.xyz. The asterisk acts as a wildcard character, informing gitignore that any character before .xyz should be considered.