In this blog post, I will be sharing basic Git commands that I frequently
use and hope that it is an useful knowledge for anyone who just started working
git for their first time. This is my own version of summarized cheat
sheet to help anyone on common tasks. If you have never used
git before, let
me share this with you to get you onboard this cool band wagon.
What is Git? Git is a distributed version control system created by Linus Torvalds and developed or maintained by the community. To find out more about Git, please refer to here.
Getting Started with Basic Git Commands
To get started, I assumes you have a project or repository from any of the public service provider (eg. GitHub, GitLab, BitBucket or etc) or self-hosting on-premise (eg. Gogs, Trac, Phabricator or etc) solution. Of course, the list of service providers and self-hosting on-premise solutions that are available in the market goes on.
Initialize a project or repository on local file system
In this example, I will demonstrate on how you can initialize a new repository on local file system and synchronise with your remote repository on the server or service provider.
Create a folder for your repository
Firstly, we will create a folder
C:\Repositories that will contain all your
individual repository and a subfolder
MyFirstRepository to work with. Next,
we will change our current working location to that subfolder location.
1 2 mkdir \Repositories\MyFirstRepository cd \Repositories\MyFirstRepository
Initialize the folder as a Git repository
Now, let us initialize this subfolder as a Git repository
1 git init
Create file in the repository folder
For demonstration, we will create a
HelloWorld.md markdown file in the
subfolder but you can skip this if you already have files that you wanted to be
in the repository.
1 2 echo "# Hello World" > HelloWorld.md echo "My First Repository" >> HelloWorld.md
Add and stage files in the repository
git add to add and stage all files in the current repository on local
1 git add .
Or you could add and stage specific files by specifying each file seperated by blank space.
1 git add HelloWorld.md GoodbyeWorld.md
Commit staged files in the repository
git commit to commits the staged files and include
-m to add a message
to describe what you are preparing to commit changes to the repository on local
1 git commit -m "My first commit to repository"
Add a remote repository location
git remote add to add a remote repository URL that you obtain from a
service provider or self-hosting on-premise server to your repository on local
file system where your repository will synchronise with as distributed version
control primary source. Once you have added the remote repository URL, use
git remote -v to validate the remote repository URL is configured.
Note: In order to make this easier for everyone to understand, I have chosen to use
$REPOSITORY_URLas a variable. You will need to replace the
$REPOSITORY_URLvariable with your real project/repository web URL.
Note: Below are some of common service providers
1 2 git remote add origin $REPOSITORY_URL git remote -v
Push the committed files to remote repository
git push to push the committed changes from your repository on
local file system to the repository on remote service provider or self-hosting
1 git push origin master
Check the repository status
You can use
git status to check the status between your repository on local
file system against the repository on remote service provider or self-hosting
1 git status
Cloning a project or repository to local file system
If you are working on an existing project or repository that is already on remote service provider or self-host on-premise server, you will just have to clone the project or repository from the remote source to your local file system.
1 git clone $REPOSITORY_URL
Viewing the repository history of changes
Once you have your Git repository ready on your local file system, you can use
git log to view the history of changes
1 git log
It is just that simple to start having a Git repository to work with your files and track those changes. Remember the key benefit of having Git repository is that Git tracks content not files.
Having a remote source from a remote service provider or self-host on-premise server to store those Git repositories allows others to work collaboratively on the repository with Git managing and tracking those changes.
There you have it, start gitting.