Beginner Guide to Git

Β·

3 min read

What is git?

Git is a version control system that helps to track the changes we make in files so that we can have a record of what has been done & we can revert back to any specific version whenever we want.

Initialize git

To initialize git use the following command

  git init

This will create a .git folder which will be responsible for managing and keeping track of all the changes in your files.

Now after initializing the git we have to add the files to the staging area so that git can know what files needs to be committed to the repository.
Before that, we can use git status to check what is in the staging area, which files have been staged, which haven't, and which files aren't being tracked by git.

To add files to the staging area use

  git add filename
  // OR
  git add .

Here, dot simply means add all files and folders that are present in the current directory.

After adding files to the staging area we have to commit the changes.
For that use,

git commit -m "Your Message"

After committing the changes we usually push all our changes to a remote repository like GitHub, GitLab, Bit bucket.

For pushing changes to Github.
create a new repository on Github and copy its URL

Note For pushing to the remote repository we have to configure the git so that it can push the changes to remote repository

git config --global user.name β€œ[Your name]”
git config --global user.email β€œ[your email]”

Note Above commands require only when you haven't already configured the git

Now for pushing the changes to remote repo use below command.

git remote add origin your remote repository url
git push -u origin master

This will push all your local tracked files & folders to the Github repository

Some more useful git commands

This command is useful we want to know what is changed but not yet staged.

git diff

Similarly, there is

/* This is useful when we want to know what is staged but not yet committed. */
git diff --staged

To list all available branches

// this will list all the branches that we have created
// * will appear next to the currently active branch
git branch

For creating a new branch

// this will create a new branch at the current commit
git branch branch-name

For switching to another branch

// this will switch to the specified branch
git checkout branch-name

For merging branch

/* this will merge the specified branch history to current one */
git merge branch

Thank you for reading :)

Β