Working with Git repository for the first time

4 minute read

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 with 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.


Top


Pre-requisite requirements


Top


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.


Top


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

Top


Initialize the folder as a Git repository

Now, let us initialize this subfolder as a Git repository

1
git init

Top


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

Top


Add and stage files in the repository

Use git add to add and stage all files in the current repository on local file system.

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 

Top


Commit staged files in the repository

Use 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 file system

1
git commit -m "My first commit to repository"

Top


Add a remote repository location

Use 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_URL as a variable. You will need to replace the $REPOSITORY_URL variable with your real project/repository web URL.

Note: Below are some of common service providers $REPOSITORY_URL examples.

  • https://github.com/YourUsername/YourRepositoryName.git
  • https://gitlab.com/YourUsername/YourProjectName.git
  • https://bitbucket.org/YourUsername/YourRepositoryName.git
1
2
git remote add origin $REPOSITORY_URL
git remote -v

Top


Push the committed files to remote repository

Finally, use git push to push the committed changes from your repository on local file system to the repository on remote service provider or self-hosting on-premise server.

1
git push origin master

Top


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 on-premise server.

1
git status

Top


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

Top


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

Top


Conclusion

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.


Top


References


Top



Top