Codementor Events

Simple CRUD With Git

Published Nov 16, 2017Last updated May 15, 2018

In this article, I will show you how to perform basic operations with Git that will improve the productivity of a developer. Git is an essential tool to learn and master, as many projects depend on it. This article will show some of the basic operations that come handy in day to day life while dealing with Git.

Create a Repository

The first thing we need to do is to create a remote repository on github.com. For that, you need to log into github.com. After a successful login, you need to create a repository called "test" using the "create a repository" button. This is the repository we are going to work with.

Add New Files

By default, the repository is empty except for a README.MD file.

In order to add a new file, first you need to clone this "test" repository to your local disk. In order to do that, run the following command:

git clone https://github.com/unnivm/test.git

This will download and create a folder called "test" on your local disk. If there are any files in it, all of them will be downloaded into the "test" folder.

Now add a new file called "test.txt" with some contents in it under the "test" folder. Then run the following command:

git add .

The above command will add the new files to your repository.

The next command is to commit this new file.

git commit -m "this is my first file"

The -m switch is the message that you want when you commit the changes.

Now we need to send this change to the remote repository. For that, run the following command.

git push origin master

This command will lead to authenticate the user once again and send your file to new repository.

Let us take a look at the above command in detail:

  • push - Sends the files or changes to remote repository.

  • origin - The default name of the repository given by Git. 

  • master - The default branch name in the repository. This is where all your changes will be pushed into unless a different branch name is mentioned.

Update Files

Now you are going to modify the files you created in the above step. Let us modify the "test.txt" file we created in the above step. We need to perform the following operations one by one:

git add .
git commit -m "I have modified my file"
git push origin master

Once you execute the above commands one after another, your files will be updated in the git remote repository.

Delete Files

Now we want to delete either a single file or multiple files from the remote repository. In order to do this, issue the following commands:

git rm <name of your file>
git commit -m "the file has been deleted"
git push origin master

These commands will help you in delete the file in your git repository as well as from your local file system.

In order to delete multiple files, you need to mention the files to be deleted. For example, if you want to delete 2 files, namely, file1.txt and file2.txt, then issue the following command:

git rm file1.txt file2.txt

Summary

We have seen the minimal necessary commands while working with a Git repository. In all the CRUD operations which we have seen, the following commands are mandatory:

git commit -m "your commit message"
git push origin master

You can do much more with Git, but this article is just beginner level for anyone to understand the basic concepts of Git. 

Discover and read more posts from Unni Mana
get started
post commentsBe the first to share your opinion
Show more replies