Introduction
Most of us dislike Git on the first try, even after running the most basic Git commands. GIT is a distributed version control system. It helps you in effectively manage product versions. The purpose of this blog is to provide most basic commands to get you started.
Common Uses:
- Keep track of all the files in a project
- Record any changes
- Restore versions
- Compare versions
- Manage code from teams
What is Repository?
A repository is just a directory (a folder) in your project's root directory.
GIT Code Cycle
Below diagram shows the common cycle for code in git, read from top left in an anti-clockwise direction.
- The user pulls changes from Github Repository into its local.
- The user checks out and starts working on a new branch
- User push changes to his branch
- User raises a pull request to the remote for merging
Git Cycle
Basic GIT Commands
There are around ten most basic commands that assist you in getting to know the status and start working on the repository.
- Check the current status
Input:
git status
Output:On branch master Your branch is ahead of 'origin/master' by 48 commits. (use "git push" to publish your ocal commits) Nothing to commit, working directory clean
- Fork the repository in the github project and get the HTTPS URL
Input:
git clone https://github.com/YOUR-USERNAME/Spoon-Knife
Output:Cloning into `Spoon-Knife`... remote: Counting objects: 10, done. remote: Compressing objects: 100% (8/8), done. remove: Total 10 (delta 1), reused 10 (delta 1) Unpacking objects: 100% (10/10), done.
- Go to the directory of the forked project and check the fork information.
Input:
cd dirname git remote -v
Output:
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push)
- Go to the directory of the forked project and upstream which we can get from github project page HTTPS URL
Input:
cd dirname git remote add upstream https://github.com/octocat/Spoon-Knife.git git remote -v
Output:
origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (fetch) origin https://github.com/YOUR_USERNAME/YOUR_FORK.git (push) upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (fetch) upstream https://github.com/ORIGINAL_OWNER/ORIGINAL_REPOSITORY.git (push)
- Branch out from the master
Input:
git checkout -b branchName
Output:
Switched to a new branch "branchName"
- Basic Merge your changes needed to manage multiple branches for segregation of work
git checkout master git merge branchName
- Add New File or all new files
git add filename git add .
- Commit your work to your branch
git commit -m "Comment Title for Committing"
- Push changes to our repository master (enter username and password for your git account)
git push origin BranchName
- Switch and Update Master Branch
git checkout master git pull upstream master