Git Basic Commands
The git init Command
The git init command changes a directory into a Git repository.
Example
git init
The git clone Command
The git clone command clones a project from a remote git repository to your local repository.
Syntax
git clone <remote-repository-address>
Example: git clone https://github.com/tutorialsbuddy/keycloak-sample.git
The git checkout Command
The checkout command lets you switch between branches in your local git repository.
Syntax
git checkout <branch-name>
Example: git checkout develop
The git branch Command
The branch command lets you create a new branch in your local git repository.
Syntax
git branch <branch-name>
Example: git branch develop
The git checkout -b Command
The checkout -b command lets you create and switch to a new branch using a single command.
Syntax
git checkout -b <new-branch-name>
Example: git checkout -b user_update_feature
The git branch -m Command
The git branch -m command is used to rename a branch.
Syntax
git branch -m <branch-old-name> <branch-new-name>
Example: git branch -m user_signup_feature user_social_signup_feature
The git add Command
The add command saves a file update for the next commit.
Syntax
git add <file>
Example: git add Test.java
The above add command saves an update of a single file.
You can also save changes or updates of multiple or all files for the next commit using the add command followed by a dot "." as shown below:
Usage
git add .
The git commit -m Command
The git commit -m command is used to commit changes of files before pushing updates from the local repository to the remote repository.
Syntax
git commit -m 'commit message here
Example: git commit -m 'validation at signup'
The git push Command
The git push command is used to push updates from your local repository to your remote repository.
Syntax
git push origin <branch-name>
Example: git push origin user_signup_feature
The git pull Command
The git pull command is used to fetch and download the latest updates from the remote repository to your local repository.
Syntax
git pull origin <branch-name>
Example: git pull origin user_signin_feature
The git branch -d Command
The git branch -d command deletes a local Git branch.
Syntax
git branch -d <branch-name>
Example: git branch -d user_signin_feature
The git branch -D Command
The git branch -D command force delete a local Git branch.
Syntax
git branch -D <branch-name>
Example: git branch -D user_signin_feature
The git push -d Command
The git push -d command deletes a remote Git branch.
Syntax
git push -d origin <branch-name>
Example: git push -d origin user_signin_feature
The git reset HEAD Command
The git reset Head command removes a file update of the next commit.
Syntax
git reset HEAD <file>
Example: git reset HEAD test
The git stash Command
The git stash command is used to save the current state of the working branch and go back to previous clean working branch and work on something else so that later you can go back and re-apply the changes.
Syntax
git stash
Example: git stash
The git stash pop Command
The git stash pop command is used to re-apply the changes in the current working branch, previouly recorded using the stash command.
Syntax
git stash pop
Example: git stash pop
The git status Command
The git status command is used to check the status of the current working branch.
Syntax
git status
Example: git status
The git branch -a Command
The git branch -a command is used to list all local and remote branches.
Syntax
git branch -a<
Example: git branch -a
The git merge Command
The git merge command is used to integrate changes of one branch into another.
Syntax
git merge <branch-name-to-merge>
Example: git merge master
Here, the above command will merge master branch into the current active branch.