Git is one of the most popular version control systems used by software developers today. It allows teams to work collaboratively on a proje...
Git is one of the most popular version control systems used by software developers today. It allows teams to work collaboratively on a project, keep track of changes, and manage versions of their code. Git has a command-line interface (CLI) that can seem intimidating at first, but once you master the basics, you can accomplish a lot more with Git.
In this article, let’s take a look at the Git commands you should know to master Git CLI, these commands cover the most useful Git commands that are often used.
1. git init -- git initialize
The commands turn the empty directory into an empty git repository.
Command: git init
I am using the git bash in the windows.
2. git config -- git configuration
The Git config command is used to address the name and email of the user to commit the code from the local machine to the remote branch,
In this method, we can identify the committed user name in the GitHub
Commands: git config --global user.name “username”
git config --global user.email “email address”
--global option used for global configuration.
3. git clone:
Cloning the git remote repository to the local machine.
command: git clone git repo URL
Example: git clone git@github.com:techieview/techiev-devopsclass.git
where git@github.com:techieview/techiev-devopsclass.git is a git repository URL
4. git remote:
It is used to add the remote repository(GitHub) to my local machine.
Command: git remote add origin “git URL”
5. git add: In this command, add the files to the staging area of the git. In git, before committing to GitHub you need to add the files using the git add command. Command: git add filename Adding the particular file for the commit git add . Adding all the files and folders inside the directory
6. git status: This command is used to check the current state of the local branch If any files are updated or changed, it will show the file. So if we want to commit the changes or else stash the changes If there were no changes in the files, return the messages like nothing to commit, and the working directory is clean command: git status
7. git diff
The git diff command is used to show the difference between edited files.
Command: git diff filename
8. git commit:
Commit the local branch files to the remote branch(GitHub repository)
For the commit changes always use a short message.
Command: git commit -m “message”
9. git push: Pushing the committed codes from the local to the remote branch or GitHub repository. Command: git push
10. git pull: Pulling the updated code from the remote branch to the local branch. Command: git pull
10. git branch:
This command is used to check the current working branch as well as all the branches.
Command: git branch
(a) Creating a new branch
Type1: git branch branch name
Type2: git checkout -b branch name
The above two commands are used to create the branch in the local repo.
(b) Switching to another branch Command: git checkout branch name git checkout is used to checkout or switch the branches.
(c) Delete the particular branch
Command: git branch -d branchname
The -D option to delete branches without asking for any prompt.
Command: git branch -D branch name
If any unmerged changes in the current branch will be lost using the -D option.
11. Rename the branch
Command: git branch -m techiev12. git merge:
Merging the files from one branch to another branch.
command: git merge branch name