Get Started with Git(A version control System)

Version control systems are a category of software tools that help a developer team to manage changes to source code over time. These systems keep track of every modification to the code in every single file. If a mistake is made, developers can turn back the clock and compare earlier versions of the code to help fix the mistake while minimizing disruption to all team members. These systems become necessary if a team is working on a single project.’ Among many Version control systems git is most widely used and as per my view best in the category.

1. Installing Git

For downloading git installer, you can visit Git official website. and install it to your system. Also, read :

2. Creating a Git Repository

So now you are done with the installation part. If so, you are ready to create your first git repository. Creating a Git Repository is not a complex task. You just create a directory on your system. Open the directory and run the following command –

git init

3. Configure user detail

Next, we need to configure our name and email. You can do it with the following commands

NOTE: Replace name and email with your original detail. If you do not set your name and email, git will use certain default values.

4. Check the Status of Your Repository

To check the status of your repository run following command-

git status

5. Adding files to Your Repository

If you have created new file/files to your repository then you have to add these files to the repository. Otherwise, you will not be able to stage the changes of these files. To add files you can use the following command

git add [filename]

If you want to add multiple files you can do it using-

git add [filename1] [filename2] [filename3]

If you have a number of files to be added you can use (*) instead of mentioning every files name.

git add *

git add [filename1] [filename2] [filename3]

6. Removing files from Repository

If you have added files but don’t want to keep these files or want to delete these files you can run

git rm --cached [file_name]

NOTE: It will permanently remove the file from the git repository as well as from your file system.

7. Committing Changes

Once you have staged your files, you can commit them into Git. You can imagine Commit as a breakpoint where you can return back anytime.

git commit -m "Your commit message"

8. Adding/Removing remote

For fetching a branch or pushing a branch to your remote server you need to add a remote of your remote server. For adding remote, you can use-

git remote add origin https://github.com/sdaityari/my_git_project.git

You can add multiple remote on the same git repository. From adding multiple remote run above command multiple time. Here origin is the name of your remote. To see remote you added you can use

git show remote

To remove a remote you can use

git remote remove remote_name

9. Fetching/Pushing branch

After adding remote you can easily exchange branch with your server. For fetching branch-

git fetch remote_name remote_branch_name: new_name

eg.

git fetch origin master:my_first_branch_on_local

To push the branch to your remote server use

git push remote_name branch name on your local:branch name by which you want to send

eg.

git push origin my_first_branch_on_local:my_first_branch_on_serve

10. Switching Branch

For switching between branches you can use

git checkout branch_name

11. Cloning a branch

You can make a copy of a branch easily with git. To do it run

git branch original_branch:clone_branch_name

If you want to make a copy of branch on which you are currently checked-out you can use

git branch clone_branch_name

12. Deleting a branch

If you want to delete a branch you can use

git branch -D branch_name

NOTE: You will not be able to delete a branch if you are currently checked out on it. So before deleting a branch make sure you are not checked-out on it.