GIT is a version control system, it is used to collaborate code and used to keep track code.
Nowadays it is very difficult to manage code as your system grow, sometimes we need to share code with our developer partners or with others and need to track changes on code, so GIT is very useful to manage these changes, which makes programmer life very easy.
Nowadays every single programmer must know about GIT and GITHUB.COM to manage code and share code with others.
In this tutorial we learn about all the basic and important GIT commands to manage code.
It is a central storage location where different version of files can be stored, mainly it is used by version control system.
To copying project from remote site.
In GIT staging area is like as DRAFT save, which a file or multiple files can be stored and can be used in next commit of code changes.
commit is the process to save code changes from local project to repository.
Where to use GIT commands
GIT commands can write in window shell, or GIT BASH.
Check GIT version
Set global user name and email for GIT
git config --global user.name "Capscom Technology"
git config --global user.email "Capscomtech@gmail.com"
In this tutorial we learn GIT using some world real life examples
We will create the HTML project and will try to track code changes with GIT
First of all create new project folder or navigate to targeted project folder, as you reach there just right click on that folder inside and select git bash here, new small git bash window will open.
Now we are ready to create GIT repository (it is a place where GIT will store our code to track)
with this command GIT will create new repository on that folder, if that folder is hidden, you can unhide or can show hidden files.
Now git is ready to track code, and we can create project ahead by adding new files.
For example we are creating new page saved as index.html
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
Now new code added on project, and git know about this file and this code is not available on git repository.
use git status command to check git status
Add file or files in staging area
Add specific file
git add index.html
Add all files
git add .
git add --all
git add -A
As you think your code has been done at specific point and want to save in git so that other can use
git commit -m "Message define what you have done till this special commit"
Display list of files
Check the status of repo
git status --short
The short flags are
- A - Files added to stage
- M - Modified files
- D - Deleted files
- ?? - Untracked files
Check the history of commits of a repo.
- git log
- git log --oneline
Check the current branch
Create new branch
git branch css-design
Check out from a branch
git checkout css-design
Create and switch to new branch
git checkout -b fix-errors
- First checkout to master branch
- then merge with branch "css-design"
git merge css-design
Delete merged branch
git branch -d css-design
Push local repo to github
git remote add origin repo_url
Push master branch to the origin repo_url
git push --set-upstream origin master
Pull updated repo(Fetch and merge with existing local repo)
Provide all the changes history
git fetch origin
Check the difference between changes
git diff origin/master
Merge origin repo with local repo
git merge origin/master
Fetch and merge origin repo
git pull origin
Push new changes to origin
git push origin
To view all local and remote branches
git branch -a
git branch -r (for remote braches only)
Push only database-implementation branch to origin
git push origin database-implementation
git revert Head
git revert HEAD --not-edit
Move back to previous commit and ignore all the changes made after the commit.
git reset 9cd82a6
git commit --amend -m "New message for commit"