Monday, July 23, 2018

Basic Git Commands | Beginner to Advance | Learn Git & GitHub

Hello friends Welcome again to TechPunch.
In this tutorial I will show you the basic Git commands to learn Git & Github.
Hope you have referred my previous blog for Installing the Git Bash on your Local PC/Laptop.
As GitHub is widely used platform so you should know how to use it. Here are some basic about Git and Github you must need to know before we staring.
GitHub is primarily a web platform, which hosts code repositories along with distributed version control. Today, almost every developer working on a group project or individually uses GitHub as most important tool due to its capability of making version control easier.
Repository – A repository is a location where all the files for a particular project are stored, usually abbreviated to “repo.” Each project will have its own repo, and can be accessed by a unique URL.
Revisioning – Git keeps an eye on each revision you make with the option of ‘reverting’ to a previous version, should the need arise.
Collaboration – Multiple people can pull and push code to the repository, making it a centralized source
Forking – “Forking” is when you create a new project based off of another project that already exists. This is an amazing feature that vastly encourages the further development of programs and other projects. If you find a project on GitHub that you’d like to contribute to, you can fork the repo, make the changes you’d like, and release the revised project as a new repo. If the original repository that you forked to create your new project gets updated, you can easily add those updates to your current fork.
Github is a great way to handle and maintain code for a developer in the sense that it keeps everything organized with the ability to undo any unwanted actions. You can also push directly to servers so there is more consistency with that side of the transfer. In general, Github, and Git, are great tools for any developer/designer and relatively easy to learn.

Basic Git Commands

Configuring the Git :
When you come to a bank for the first time and ask to store your money there, they give you a bunch of paperwork to fill out. Before you can use banking services, you have to register with the bank. Git wants you to do the same (register with Git) before you start using a repository.
To tell Git who you are, run the following two commands:
git config --global user.name "Your Github Username"

git config -- global user.email "Your Github Email"
To check your submitted Github username and email :
git config --list

Start a New Local Repository :
The “init” command stands for initialize. Once you run “git init”, Git will initialize a hidden directory called “.git” in the project’s root directory.
git init

 Status of your Project :
You might want to know the status of your project. does it store anything yet? To know the Git status, you’ll need to run:
git status
List the files you’ve changed and those you still need to add or commit.

Adding files in track :
By using the git status we knows that some of the files are untracked yet. So adding these files into the track we are using the git add command.
Adding a Particular File
git add branchname
For adding all or multiple files
git add .

Committing changes to Git :
Record the changes made to the files to a local repository. For easy reference, each commit has a unique ID.
It’s best practice to include a message with each commit explaining the changes made in a commit. Adding a commit message helps to find a particular change or understanding the changes.
Commit a File
git commit -m "Your message here"
Adding & Commiting all files at one stroke
git commit -a -m "Your message here"

Check out a repository :
Create a working copy of a local repository:
git clone /path/to/repository
For a remote server, use:
git clone username@host:/path/to/repository

Branch :
The default branch in which you are working is master branch and if you want to create a new branch for your project then use this command
git branch 
Create a new branch and switch to it
git checkout -b branchname
Switch from one branch to another
git checkout branchname
List all the branches in your repo, and also tells in whic branch you are currently working
git branch
Delete the branch
git branch -d branchname
Push the branch to your remote repository, so others can use it
git push origin branchname
Push all branches to your remote repository
git push --all origin
Delete a branch on your remote repository
Push all branches to your remote repository
git push origin :branchname

Merge :
Merge the changes made in another branch into the current branch
git merge branchname
To check the all branches
git branch -a
Merge branch  into the current branch, but do not make a new commit automatically
git merge --no-commit branchname
Fetch and merge changes on the remote server to your working directory
git pull

Tags :
You can use tagging to mark a significant changeset, such as a release
git tag 1.0.0 commitID
check logs of all the commits
git log
Only check the commits
git log --oneline
Push all tags to remte repository
git push --tags origin

Undo local changes :
If you mess up, you can replace the changes in your working tree with the last content in head:
Changes already added to the index, as well as new files, will be kept
git checkout -- filename
Scrap uncommitted state and return the working tree to the last committed state
git reset --hard HEAD
Delete the latest commit, and return to the one previous (one before HEAD)
git reset --hard HEAD~1

Remove files :
To remove a file from the working index (cached)
git rm --cached filename
To delete a file (force)
git rm -f filename
To remove an entire directory from the working index (cached)
git rm -r --cached directory_name
To delete an entire directory (force)
git rm -r -f filename
Some of the useful Resources :

Thanks for Visiting..
Keep Supporting (Tech Punch)

No comments:

Post a Comment