Git Basics for Beginners
When you are working with a team Git is an important part of daily programming.Since there are many things you can explore in Git,I explained few basic things to get started with Git.
What is Git?
Git is a Version Control System (VCS), allows us to keep track of when and who did what changes to our files. Those can be code, config,images or whatever else we need to track.
What is Git Hub?
Git hub is a Git repository hosting service that provides a web-based graphical interface (GUI). It helps every team member work together on a project from anywhere, making it easy to collaborate.
To get started with Git create a Git hub account using email id.
In your machine download the latest version of Git 32/64bit based on your system specification. Once the file downloaded install it in your local system. Once installed,select Launch the git bash and select finish. Now the Git Bash is launched.
Check the git version using the command
For any help use the command
To set the user name and email address to be used with your commits use
git config –global user.name “[name]” git config --global user.email "email@example.com"
The email address should be the one you used to create the account in Git hub
To initialize the new repository use the command
git init <repository name>
This will create a new Git repository in your local system.
Once the initial set up is done you can work with your local and remote repository!
The basic Git Commands
Git clone is a command for downloading existing source code from a remote repository.
If you want to download a project from Github, click on the green button called code or clone and copy the URL in the box, then go to the folder where you want to clone the repository (repo in short) right click -->Show more options -->Git bash here,the CLI will be opened and paste the URL after the git clone command.
$ git clone <url of the repository>
This will make a copy of project in your local work space.
The git status command tells us all the information about the current branch
This command provides the current working branch. If the files are in the staging area, but not committed, it will be shown by the git status. Also, if there are no changes, it will show the message no changes to commit, working directory clean.
The git add command is used to add all the untracked files to the staging area. Git add command is used to add any new or modified files.
$ git add <file name> -to add single files $ git add * - to add one or more files
The git commit command makes sure that the changes are saved in local repository.
$ git commit -m 'Commit message'
Commit message helps everyone to understand what is added or modified in the project.After the commit you can check the status using git status command, it will show working tree clean.
After committing all your changes the next thing you want to do is push or transfer your local changes to remote repository.The git push command will update your remote repository.
$ git push origin <branch name>
However, if your branch is newly created, then you also need to upload the branch with the following command:
git push --set-upstream <remote> <name-of-your-branch>
git push -u origin <branch name>
A branch represents an independent line of development. The git branch command lets you create, list, rename, and delete branches.
By creating new branch,we can experiment without breaking what already works, several developers can work in parallel on the same project simultaneously.
To list all the existing branch we can use the command
To create the new branch the command is
git branch <branch name>
To switch to the corresponding branch
git checkout <branch name>
To create a new branch and switch to it we can use the command
git checkout -b <branch name>
In your specific branch you can do changes then you can add, commit and push
When you completed your task in your branch if everything works fine the final step is to merge your branch with the parent branch (main/master/dev).
Before merging you need to switch to the corresponding branch in which you are going to merge. For example if you want to merge your test branch to main branch first you need to switch to the main branch using the command
git checkout main
Then you need to update the local main branch using
Finally you can merge the branch using
git merge <branch name>
Make sure your main branch has the latest version before merging to avoid any conflicts.
Some times we want to undo the changes we have made, the safer way to do this is using 'git revert' command.But this to be used before pushing your commit to remote repository.
Sometimes we can use "git reset –hard [commit]" This command discards all history and goes back to the specified commit.
These are few of the commonly used Git commands, there is more to learn, I hope you found this useful to get started with your Git Journey.
Happy learning! Keep exploring!!