MOST USED GIT COMMANDS WITH EXAMPLES
What is GIT?
Git is a distributed version control system and which supports distributed environments.
there are two kinds of version control system softwares available
Central version control system
Distributed version control system
Git is a software which supports distributed version control system and is completely open source.
What is Architecture of GIT?
Working Directory: Working directory is a location where actual project files are present. So workspace means whenever we are implementing the project or we are writing the code so all the files by default will be there in the working directory so which is called Workspace. And whatever files are available inside the working directory or workspace we can call them untracked files.
Staging: whatever files are in the workspace we cannot directly commit them to the local repository so they should go through Staging. So staging is another layer which is a virtual layer between our workspace and the git local repository. So whatever files we have in the workspace we have to add those files to a staging area or index through the "git add." command. Then we can call them tracked files.
Local repository: In this whatever files are in the staging area we can commit them into the local repository by using the "commit" command. Now, what are the files in the local repository we can call them committed files.
Remote repository: From the local repository, we can push our files into GitHub or a remote repository by using the "push" command. From there, other developers or other team members can get those files. Here Github is acting as a Remote r is a distributed version control system and which will supports a distributed environment.
This is the Initial step before starting your git commands.
command: git ---version
It shows you the current version of git.
1) git config: The git config command is used to set configurations like the name, email id, etc.
This information should be provided as soon as it is installed. Since it is used by git at every commit.
command: git config ---list
2) git clone: The git clone command is used to create a copy of the target repository or create a clone in a new directory at a new place.
copy that link from GitHub and paste it here and follow these commands
command: git clone <link>
3) git init: The git init command is used to initialize a blank repository. It create a .git folder in the current working directory.
command: git init
4) git status: The git status command is used to display the state of the current repository and the staging area.
I have created one txt file as "first.txt"
command: git status
5) git add: The git add command is used to add changes in the current directory to the staging area.
command: git add.
6) git commit: The git commit is used to save the changes to the local repository. The command helps you keep record of all the changes made.
command: git commit -m "message"
7) git push: The git push command is used to upload the content from the local repository to the remote repository.
some times while pushing our code to a remote repository it asks us for Authentication. so we have to give our Github user id and password. some times we have to give a key as a password. So for that, we have to generate a new key through settings.
command: git push
git push -u origin master
the file "first.txt" pushed from local to remote repository. The file "first.txt" is empty file.
Now I made some changes in remotely that is ,I wrote one sentence as "Hello world" in the "first.txt" file.
8) git pull: The git pull command is used to fetch the new commits and merge them into the local.
It is always safe to commit the local changes and then go for a git pull, because git pull downloads the remote changes and immediately merge them into the local repository. The conflicted files will be flagged to resolve manually. If there are uncommitted changes, the merge part of the git pull command will fail and the local branch will be untouched resulting in not being able to get the remote changes into the local branch.
command: git pull
now we will check the changes whatever made on remotely.
9) git branch: A branch refers to an independent life of development. The git branch command is used to 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 view available branches
command: git branch
To create new branch
command: git branch <branch name>
Here "master" is the active branch
10) git checkout: The git checkout command works together with the git branch command. The command enables the navigation between the branches.
command: git checkout <branch name>
Here "br1" is the active branch
Using checkout command we can switch to one branch to another branch.
creating and switching to branch
command: git checkout -b <branch name>
11) git log: The git log command is used to view the previous commits that have taken places in the git project. When the list appears on the screen, it show the reverse chronological order.
command: git log ---oneline
12) git merge: The git merge command is used to integrate different branches in to a single branch.
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 feature branch to master branch first you need to switch to the main branch using the command.
command: git merge <branch name>
These are few of the commonly used Git commands, there is more to learn, I hope you found this blog useful to get started with your Git Journey.
Thank you for reading.