Introduction to GIT
Git is a content management and tracking system. It is a distributed revision control that is performed on stored data. It is a Source Code Management (SCM) system. It allows you to track the history of files. We can create different versions of this collection.
What we will cover in this post:
- Basic commands of Git
- How to make changes in the files
- Sync my local repository with the remote repository
- Add/commit them
Below is the list of some basic Git commands to get you going with Git:
- Git Config: It configures the author name and email address that will be used with your commits.
git config –global user.name “Abc Xyz”
git config –global user.email abc@example.com - Git Init: It will create a new Local Repository.
git init - Git Clone: It will create a working copy of a local repository.
git clone /path/to/repository - Git Add: It adds one or more files to staging.
git add <filename> //to add a particular file
git add * //to add all the files - Git Commit: It commits the changes to the head but not yet to the remote repository.
git commit -m “Commit Comment” - Git Push: It will send the changes to the master branch of your remote repository.
git push origin master - Git Status: It will list all the files you’ve changed & those you still need to add or commit.
git status - Git Checkout/Branch: To create branches and to switch between them.
//Switch from one branch to another
git checkout <branch>
//Create a new branch and switch to it
git checkout -b <branch>
//List all the branches in your repository & tells you what branch you’re currently in
git branch
//Delete the feature branch
git branch -d <branch>
//Push the branch to your remote repository
git push origin <branch> - Git Pull: It will Fetch and merge changes from the remote server to your working directory.
git pull - Git Merge: It will merge a different branch into your current branch.
git merge <branch> - Git Add: After making changes to resolve conflicts manually, you mark the changed file.
git add <filename> - Git Diff: It will show you the merge changes.
//View all the merge conflicts
git diff
//View the conflicts against the base file
git diff –base <filename>
//Preview changes, before merging
git diff <source-branch> <target-branch>
The basic workflow of Git:
Step 1 − Modify Local files.
Step 2 − Add these files to the staging area.
Step 3 − Commit these files will move them from the staging area. After Push operation, it stores the changes permanently to the Git repository.
Step by Step commands to commit your code:
- Check status if you are in your branch or not.
git status
- If you are not in your branch which you want to commit (If you are already in your branch you can skip this step). Go to your branch using-
git checkout <your branch name>
- Add the files you want to commit. This will take files to the staging area, not on the repository.
git add –all //to add all files
git add . //to add all files
git add <filename> //particular file
- Now use below command, to push code to the repository.
git push origin <your-branch-name>
Step by Step commands to take a pull from the remote repository to your local:
- Check status whether your directory is clean or not. It means your directory should not contain any file that is not committed to your branch.
git status
- If it is not, use the above steps (Step by Step commands to commit your code) to commit your changes.
- If directory is clean, pull the code from remote repo
git pull origin master
- This will merge the master’s code to your local. You have to solve conflicts if occurred manually from local files. Then commit merged code to your branch-
git push origin <your-branch-name>
- Check status if you are in your branch or not.
git status - If you are not in your branch which you want to commit (If you are already in your branch you can skip this step). Go to your branch using-
git checkout <your branch name> - Add the files you want to commit. This will take files to the staging area, not on the repository.
git add –all //to add all files
git add . //to add all files
git add <filename> //particular file - Now use below command, to push code to the repository.
git push origin <your-branch-name>
Step by Step commands to take a pull from the remote repository to your local:
- Check status whether your directory is clean or not. It means your directory should not contain any file that is not committed to your branch.
git status - If it is not, use the above steps (Step by Step commands to commit your code) to commit your changes.
- If directory is clean, pull the code from remote repo
git pull origin master - This will merge the master’s code to your local. You have to solve conflicts if occurred manually from local files. Then commit merged code to your branch-
git push origin <your-branch-name>
Step by Step commands to create a new branch:
- Pull code from Remote repository using-
git pull origin master - Now, the common remote code will be on your local. Then create your new branch using-
git checkout -b “new_branch” - You will not see your branch on gitlab until you push your branch.
git push origin new_branch
Get certification for your knowledge in the fundamentals of Computer functioning by clearing the Computer Certification exam conducted by StudySection. After going through this Computer Certification exam, you will be able to evaluate your basic knowledge of computers.