Getting started with GIT – Part 3

This is the part 3 of tutorial getting started with git.


Now let’s see how to delete files.

  • We can use “git rm <files name>” command to delete files.


  • Let’s commit the changes we made.



Note: Here, you may notice that we did not add our changes to staging area. That is because when we use “git rm” command, it already adds the deleted file to the staging are.

Let’s learn how to rename and move files.

  • First, we are going to rename a file as we normally do. I am going to rename the “first.txt” file into “index.txt”. Let’s see what git thinks about the renaming we did.



  • You can see git thinks that we deleted the first.txt file and added another file called index.
  • So, we can act according to git. Now we are going to add the index.txt file and remove the first.txt file.


  • If we check the status now using “git status”, you will notice that git has realized what we actually did was renaming the file.


  • As always commit the changes


  • Yeah, it was kind of confusing.So, lets look at an easy way.

  • Let’s use the “git mv <existing file name> <new file name>” command to rename a file. Here the mv means move.


  • Go ahead and Commit the changes.
Note: Here, you may notice that we did not add our changes to staging area. That is because when we use “git mv” command, it already adds the changes to the staging are.

  • Now we are going to move our pizza.txt file into a new directory called “mostLovedThings”. So first we have to create a directory.


  • We are using “git mv <existing file name> <location you want to move the file>” command.


  • Commit the changes you made.
Note: You can move and rename a file at the same time. For an example if I want to move the pizza.txt into the folder mostLovedThings and rename it to burger, the command will look “git mv pizza.txt mostLovedThings/burger.txt (See below image).





Now, Let’s learn how to commit directly from your working copy to the main repository.


  • First, let me make some changes to a file. I am going to change index.txt file.




  • In previous parts of this tutorial what we did was add these changes to the staging area and then commit them. But if you think about the simple changes we made to the file there is no need to add them to the staging area because we clearly know that we want these changes in our final project.
  • So, lets go ahead and enter the “git status” command.


  • You can see it says that we modified the index.txt file.
  • Now, I an going to commit without adding them to the staging area. We can use “git commit -am “<message>”” command to achieve this.

 
  • Now try git status command.


  • You can see that our working copy is now exact same as the repository.
  • Most importantly I must say that this shortcut method can be applied in certain situation only.
  • Whenever you use this command, it grabs everything in your working copy commit them to the main repository. So, if there is only one file that is ready to be committed then you should not use this command.
  • You can only use this command whenever you are editing or modifying files. I you delete, rename and move files then don’t use this command.

Okay, it is the end of part 3. We discussed how to delete, rename. move and a shortcut to commit our fies without adding them to the staging area in this tutorial.
In the next part, i will look forward to work on a kind of real project.

Comments

Popular Posts

Getting started with GIT – Part 2

Getting started with GIT – Part 5