How to Get Started with Git
In this blog post, you'd get to know about:
Why Use Git?
Download and Installation
Quick overview of Git operations
Why Use Git?
Download and Installation
Quick overview of Git operations
Branching and Merging
Important Git Commands
If you already have Git installed in your local machine, you can get the latest development version via Git itself through the following command:
Now use this command, to connect to your new repository and to be able to push to it.
Important Git Commands
Why use Git?
First of all, what in the world is Git? Why should we bother learning about it?
Git is a free and open-source distributed version control system, for tracking changes in the source code during software development. Every contributor of the project can have a working copy of the code and complete change history on their local machines.
Some of the most commendable features that Git provides are:
Some of the most commendable features that Git provides are:
- Cheap local branching
- Convenient staging areas
- Multiple workflows
Download and Installation
Git can easily be downloaded from Here. After that, the installation is pretty straight-forward. Once you're done with the installation, you'll be provided with a desktop shortcut for Git Bash, which is a command line application that you can use to write all your git commands.If you already have Git installed in your local machine, you can get the latest development version via Git itself through the following command:
git clone https://github.com/git/git
Quick overview of Git operations
In order to use Git effectively, it's good to understand the file status life-cycle and operations in prior. Refer to the image below for a better understanding.- If the status of a file is untracked, it simply means that we are not tracking the file using Git. Git has got nothing to do with our file at this status.
- If we add an untracked file, it goes to the staging area. Staging area is basically a stage where Git is keeping track of our file.
- If you are done with your project, but you are skeptical about one of the files in the project and think that there is a scope of improvement, then you can commit all the files except that one. It can be modified to your satisfaction, re-staged and then committed to your repository.
- After getting committed, the file status is unmodified, from there you can either edit the file and send it to the modified status, from where you can stage the file, or you can remove it.
Branching and Merging
Branching refers to diverging from the master(main line of development) and continuing to do work without messing with that master branch.
Let us understand branching and merging with an example. Suppose you are working on a web development project and you have all your files ready. Now you feel like adding some new functionality to one of your web pages, but you don't want to fiddle with the files currently in the project, because there is a chance that you might screw things up in the process of adding that functionality.
To achieve this, if you didn't know how to use Git, you would have to create a copy of your project for backup and start working to add the functionality. But with Git, you can simply create a new branch out of your master branch and start working on it without affecting the files in the master branch.
When you're sure that the project in the newly created branch works properly with the desired functionality, you can merge it with the master branch.
Important Git Commands
Here is a list of some extremely important Git commands to make your life easy with Git. First thing you should do is open the Git Bash terminal and go to the directory from where you'd like to add the files to your repository. These basic commands can be used to change your current directory and to go out of the current directory to the parent directory respectively. cd DIRECTORY_NAME
cd ..
Configure name and email in Git:
These commands are used to set the author name and email address to be used with your commits. Use the username and email registered with GitHub. git config --global user.name YOUR_USERNAME
git config --global user.name YOUR_EMAIL
Initialize a new local repository:
This command creates a new empty git repository for your use, in the directory you are currently in. git init
Add files to staging area:
Using this command, you can add files from your current directory to the staging area. git add FILE_NAME
The following command can be used if you want to add all the files from your current directory to the staging area in one go. git add *
Connect to a remote repository:
You can easily create a remote repository using GitHub. If you haven't signed up with GitHub yet, you can do it Here. To create a new repository, click the '+' button on the top left of your GitHub profile and select 'New Repository', then give a suitable name and optionally a description (you can ignore all other fields for now) to your repository and click 'Create'. And voila, your remote repository is all set.Now use this command, to connect to your new repository and to be able to push to it.
git remote add origin YOUR_REPOSITORY_URL
To list all the connected remote repositories, you can use this command. git remote -v
Commit changes:
This command is used to commit your changes to the head of your local repository (not to the remote repository). git commit -m "commit message"
The following command can be used if you want to commit any files that you have added with git add and any files that you have modified since then. git commit -a
View commit history:
To view all the commits you have done to your repository till now, you can conveniently use this command. git log
View file status:
To list the files you've changed and those you still need to add or commit, you can use this command.
git status
Push changes to remote repository:
To push the changes, that you've committed in your local repository, to the master branch of a remote repository, use the following command. git push origin master
Working with branches:
Use this command to create a new branch and switch to it.
git checkout -b BRANCH_NAME
To switch from one branch to another, use the following command.
git checkout BRANCH_NAME
To list all the branches in your repository and to view which branch you're currently in, use this command.
git branch
To delete a branch, use the following command.
git branch -d BRANCH_NAME
Use this command to push a branch to your remote repository.
git push origin BRANCH_NAME
To push all the branches to a remote repository, use the following command.
git push --all origin
To merge another branch in your currently active branch.
git merge BRANCH_NAME
Updating from the remote repository
Use the following command to fetch and merge changes from the remote repository to your current directory.
git pull
Undo local changes
In case you mess up, you can replace the changes in your working tree with the last committed content in head. Changes already added to the staging area, as well as new files, will be kept.
git checkout --FILE_NAME
If you have any queries, feel free to post them in the comments section. If you found this blog post helpful, share it with a friend! Happy Coding :)
Nicely Explained :)
ReplyDeleteThank you so much Vaishnavi :D
ReplyDelete