Git is a version management system.Its an open source and is most widely versioning tool today.Here we will see how to manage the files in a Git repository.
To manage the files using Git we need to add the files to the Git Repository.We can interact with Git either through the GUI or Git Command line.We will be using the Git command line(also called Git bash) here.
Git command line
To start the git command line right click on the folder you want to manage with Git. If Git is installed on your machine then on right clicking on a folder options to open Git using UI and command line or git bash will be displayed.Select the “Git Bash here” option.
Git bash command prompt opens
To verify if Git bash has opened correctly just type git.A help displaying the important git commands is displayed.
A git repository is a working directory that contains all the history of the changes made to the directory.To verify if a directory is a git repository just see if the directory contains a .git subdirectory.Every git repository is supposed to contain a .git subdirectory and it is this subdirectory which is responsible for tracking all the changes in the repository.
It is important to understand that working directory or the directory with which we directly interact is nothing but the repository in Git.This is is unlike client server versions control systems such as SVN in which a central server contains the repository.The advantage is that the dependency of the users is removed from the central server and users can work with the repository without any connection to the server.
Following are the steps to create a new repository and add files to it.
Creating a new repository
A new repository can be created as
- Open Git bash as described above.
- Change the current directory to the working directory
- Type “git init” at the command line Message is displayed that an empty repository has been initialized.
You can verify that a new directory having the name “.git” is created in the repository we just initialized.
Add a file to the repository
- Below we added a new file in our directory. If you have an existing file you can use that file instead.
2. Type the following command at the command prompt
$ git add “Test Document.txt”
The above command will add the file to the Git Index also known as Staging area.
3. Verify if the new file has been staged
To verify if the file has been added to the Index type git status command at the command prompt.If the file has been successfully staged then the following message will be displayed.
To actually add the file to the repository from the Staging area we need to perform commit.To commit the file type the following command:
$ git commit “Test Document.txt”
Now we have added our file to the Git repository and Git will maintain the different versions of the file as we make changes and perform commits.