GIT Commands… What is that? If you are a software developer, then you already know that software development is all about developing solutions that follow requirements and processes for a customer or project within your organization. If you were the only developer assigned to a project, you might be able to live without version control and be able to manage the solution on your own development computer.
The problem is managing complex code over time where the team is made up of multiple developers. Being able to track changes and merge parts of the solution without big time-consuming parts is crucial for a successful project. Another issue you might run into (if deployed to production) is that you sometimes have to roll back a solution to the previous version due to errors or bugs.
When it comes down to writing the code you can all sit down and do that on your computer, but the management of the code is a very important thing to consider. Source code management is not all about storing the code in a safe place, but also being able to track changes, do pull requests when new code is added to a project, resolving conflicts that might arise when merging parts from other developers.
In this short article / GIT guide, I will be sharing some of the GIT commands I use the most during a normal workday. If you are a programmer too, you might also be in a position where these commands are needed. You can also use this shortlist as a go-to guide for easy-to-use commands.
A very short introduction to GIT
From my understanding, GIT is a version control system for managing the source code inside your application/solution. With other words – It’s a piece of software that is capable of keeping track of changes of files, that allows people to work collaboratively and manage their files during software development. It can be used for other things than software, but that is the most common usage when talking about GIT.
As per GIT-SCM: Git is a free and open-source distributed version control system designed to handle everything from small to very large projects with speed and efficiency.
The GIT Commands for every software developer
Below is a list of the commands and below that is a demonstration of how to use that command in your command-line interface (CLI).
- Git Init
- Git Clone
- Git Add
- Git Config
- Git Add
- Git Remove
- Git Commit
- Git Pull
- Git Push
- Git Reset
- Git Status
- Git Branch
- Git Diff
- Git Checkout
- Git Merge
- Git Stash
- Git Log
This command is used to initialize a new GIT repository. All you have to do is navigate to the folder where you would like the git repository and run the command.
git init git init [your repository name]
These GIT commands are used to create a local copy of a remote GIT repository by supplying a URL for the online repository. If you are a new developer on a team and the solution has already been made, this is the command to use to get access to the source code locally.
git clone [GIT Repo URL] git status
git status is used to get the status of the GIT repository. I normally use this to check if I’m up-to-date or if there have been any pushes by other developers.
An example of that in my CMD for a project would look like this:
C:\Users\chs\source\repos\solution.Api>git status On branch Development Your branch is up to date with 'origin/Development'. Changes not staged for commit: (use "git add <file>..." to update what will be committed) (use "git restore <file>..." to discard changes in working directory) modified: Api/Controllers/DashboardController.cs modified: Api/Controllers/ErpController.cs Untracked files: (use "git add <file>..." to include in what will be committed) Infrastructure/Migrations/20211209115949_UpdateImportShipment.Designer.cs Infrastructure/Migrations/20211209115949_UpdateImportShipment.cs no changes added to commit (use "git add" and/or "git commit -a")
The files will be marked with red in the console.
When you push new code to a repo you have to supply some details about who you are. This can be configured by using these GIT commands.
git config -global user.name “[chrschou]” git config -global user.email “[email@example.com]” git config -local user.name “[chrschou]” git config -local user.email “[firstname.lastname@example.org]”
When you create new files inside your project, you would have to add them to the GIT repository you are working on.
git add [name_of_file]
Add all modified files in a project
This command is useful when you have been modifying a lot of files and have to push the updates to the repository. These GIT commands will add the touched files to the staging area, where you are able to add a commit message.
git add -A git add . git add *
It happens that we have to remove some files from our repository and to do that, we have the
git remove command. With this command, you can remove a file or folder from your working directory and stage deletion.
git rm [your_file_name] git rm -r [your_file_name]
-m is used to add a message to the command and
-a is used to commit all the files you have added via GIT or simply modified.
When working in collaboration with other developers, we need to pull and merge the latest commit from the remote server, to which they are also pushing their commits. This can be done with the
git pull command.
git pull git pull [the_branch_name] git pull orgin [the_branch_name]
As mentioned in the GIT Pull section, we also have something called a push. When we have committed our changes with a message, we need to share them with the other developers. This is done using the
git push command where we target a branch.
git push git push -u origin [the_branch_name] (ex. master, development, etc...)
Then push the changes to the branch.
git push -all
It happens from time to another that we have to reset an uncommitted or committed file. Below is a few examples of how to reset an uncommitted file, a committed file, and how to get back to a specified commit.
Reset an uncommitted file
This command will only reset a single uncommitted file.
git reset [the_file_name]
Undo all commits after x commit
This command will reset all commits after a certain commit and then preserve the changes in the current working directory.
git reset [the_commit]
Get back to a specified commit
If you need to revert to a specific commit, this can be done using the below GIT commands. You only have to know the name of the commit.
git reset --soft [the_commit] git reset --hard [the_commit]
I used this command in my GIT Clone example. But what this command is for is only getting a status of all files and then listing pending commits.
When working with multiple branches, this command comes in handy. To get a basic overview of all local branches, you can use this command:
If you would like to list all branches local but also remotely, you can append
-a to the command.
git branch -a
If you need to create a new branch, this can be done by appending a name to the command. This is useful when adding new functionality and you don’t want to interfere with the already existing code. It’s also easier when working with pull requests, as a branch can be related to a pull request and might contain multiple commits. It will keep the management part cleaner.
git branch [the_branch_name]
To delete a branch you can append
-d and then the branch name, like shown below:
git branch -d [the_branch_name]
If you need to delete a remote branch (a branch located on the source control server), you can do that using
origin --delete. Please note that it’s a push command.
git push origin --delete [the_branch_name]
If you would like to see the difference between files that have not yet been committed, you can use this command. It will only be the files that you have staged for the commit that will be shown.
git diff -staged
To preview changes between two branches before you merge them, you can use the following command.
git diff [first_branch_name] [second_branch_name]
Checkout is used if you would like to switch from one branch to another.
git checkout [the_branch_name]
If you would like to change back to the last checked out, you can use the following:
git checkout - [the_file_name]
The one I use a lot… When you have written new code inside one branch and need to merge that with another branch, this is the command. The example below will merge the current branch into the one specified in the parameter:
git merge [the_branch_name]
What if I’m not at a branch at the moment? Well, you can just specify the source branch and the target branch that you would like to merge, like shown below:
git merge [the_source_branch] [the_target_branch]
What is GIT stash? You can use
git stash when you want to record the current state of the working directory and the index, but want to go back to a clean working directory. Nice huh?! Below is an example:
And to remove all stashed entries, you can append clear to the stash:
git stash clear
As a developer is a great thing to be able to view the version or the change history of a branch.
If you would like a summary with details, you can append
--summary to the command.
git log --summary
And if you would like a brief log, simply use
git log --oneline
I hope you learned a thing or two? Above is a list of the GIT commands I personally use the most and that I think other developers also are making use of a lot. Of course, there are other GIT commands and this list is not a complete list of everything that you need to know about GIT, but they will help you get started using GIT in your software development.
Please share what GIT commands you use the most in the comments. As always – Have a nice day and Happy Coding to everyone!