Git Basics

Git Basics

In this article we will briefly discuss about Git and how to get started with git.

What is Git?

Git is a version-control system for tracking changes in computer files and coordinating work on those files among multiple people. A Version Control System allows you to revert files back to a previous state, revert the entire project back to a previous state, review changes made over time, see who last modified something in the project. In git we generally use a term repository or repo for collection of source code.

Some basic git command and their use -

  • git add is a command used to add a file that is in the working directory to the staging area(area where git starts tracking and saving changes that occur in files).

  • git commit is a command used to add all files that are staged to the local repository(the area that saves everything i.e. all of your check points).

  • git push is a command used to add all committed files in the local repository to the remote repository. So in the remote repository, all files and changes will be visible to anyone with access to the remote repository.

  • git fetch is a command used to get files from the remote repository to the local repository but not into the working directory.

  • git merge is a command used to get the files from the local repository into the working directory.

  • git pull is command used to get files from the remote repository directly into the working directory. It is equivalent to a git fetch and a git merge .

  • git --version is used to check the version of git installed on your system.

Placing file in Git

Configuring user name and email.

This step is important as every Git commit will use this information to identify you as the author.

git config --global user.name "YOUR_USERNAME"

git config --global user.email "your_git_registered_email"

Initializing git repository

To initialize an empty git repository in the folder you want to place in git use this command.

git init

Add files to the Staging Area for commit:

Now to add the files to the git repository for commit use following commands:

git add .

this adds all the files in the local repository and stages them for commit

OR if you want to add a specific file use following:

git add name_of_file_you_want_to_commit

Checking which files are staged

To check list of all new or modified files to be committed use this:

git status

Commit Changes

Now to commit files you added to your git repo:

git commit -m "message"

The message in the " " is given so that the other users can read the message and see what changes you made

Add a remote origin and Push:

Each time you make changes in your files and save it, it won’t be automatically updated on GitHub. All the changes we made in the file are updated in the local repository. Now to update the changes to the master:

git remote add origin remote_repository_URL

sets the new remote (in this repo all your files will be pushed).

git push -u origin master

This pushes the changes in your local repository up to the remote repository you specified as the origin.

And now all of your work on local directory is pushed into Github repository!!

That's it for the basics.

Thank You!