The essential Git commands
- 2021年5月14日
- 技術情報
Why you should learn Git?
Git is the most useful version control system. Git tracks the changes you make to files and collaboration easier and allows multiple people to work on the same files at the same time.
For a new or experienced people using Git is full potential and you need to know Git commands. Here you will learn the essential Git commands that will take you from one level to another.
If you are a new developer, start to learn here Basic Git commands to more Advanced Git commands step by step.
In this section, you will learn the essential foundation Git commands.
git config
When you are using Git first-time, you need to customize your Git environment. This command will set up your user name and email address. This is important because every Git commit use this information.
Usage
$ git config --global user.name “Your name”
$ git config --global user.email “Your email”
git version
It’s just to check what Git version you are using.
Usage
$ git version
git init
It creates an empty Git repository for the first command to start a new project in Git. And then you can store your source code inside this repository.
Usage
$ git init
git clone
The git clone command will use when you need to make a copy of an existing Git repository.
Usage
$ git clone <your project URL>
git add
The git add command will add a new file and modified files, an changes in the working directory to the staging area.
$ git add <your_file_name> (it will add a single file to your staging area)
$ git add * ( it will add all the modified and new files to the staging area)
git commit
This Git commit will add your changes to the local repository.
Here is the usage of the Git commit command.
$ git commit “your useful commit message”
git status
The git status command displays the state of the working directory and the stating area. It lets you see which changes have been staged, which haven’t, and which files aren’t being tracked by Git.
Usage
$ git status
git branch
The Git branch command lets you to manage your branches effectively like create, list, rename, switch and delete branches.
Usage
To list all branches
$ git branch
To create a new branch
$ git branch <branch_name>
To delete a branch
$ git branch –d <branch_name>
git checkout
The Git checkout command lets you switch between branches.
Usage
$ git checkout <branch_name>
You can checkout and create branch at the same time with a single command, like that
$ git checkout –b
git remote
Remote repositories are versions of your project that are hosted on the Internet or network somewhere. Collaborating with others involves managing these remote repositories and pushing and pulling data to and from them when you need to share work.
Usage
$ git remote add <shortname> <url>
git push
With the help of the git remote command the git push command is used to upload local repository content to a remote repository. Pushing is how you transfer commits from your local repository to a remote repo.
Usage
$ git push -u <short_name> <your_branch_name>
git fetch
The git fetch command downloads commits, files, and refs from a remote repository into your local repo. Git fetch is a safer alternative because it pulls in all the commits from your remote but doesn’t make any changes to your local files.
Usage
$ git fetch
git pull
The Git pull command downloads the content and immediately updates your local repository with the latest content.
Usage
$ git pull <remote_url>
git stash
This Git command temporarily stores your modified files. You can work in stashed with the following Git command.
Usage
$ git stash
you need a apply a stash to a branch, simply use apply
$ git stash apply
git log
This git log command lets you to review and read a history of everything that happens to a repository. Generally, the git log is a record of commits.
Usage
$ git log
git rm
This git rm command lets you to remove files from both the staging index and the working directory.
Usage
$ git rm <your_file_name>
git merge
This git merge command merge two branches you were working on.
Usage
$ git merge <branch_name>
git diff
Diff command is used in git to track the difference between the changes made on a file.
Usage
to compare the working directory with the local repo:
$ git diff HEAD <filename>
to compare two branches
$ git diff <source branch> <target branch>
git mv
This git mv command lets you to move files or directories from one place to another . It supports moving single files, multiple files and directories.
Usage
$ git mv <old-file-name> <new-file-name>
By Ami
asahi at 2021年05月14日 10:00:57