Top 15 Git Commands with Examples
--
Let’s talk about the Top 15 Git Commands, that we will use regularly while working with Git.
Git Installation:
For Linux
$ sudo yum install git -y
For Ubuntu
$ sudo apt-get install git -y
For Windows
Download from this link https://git-scm.com/download/win installs with the default configuration.
Git Commands:
git config:
git-config — Get and set repository or global options.
Git Config sets the author name and email address to be used with your commits.
Syntax:
$ git config –global user.name “<name>”
$ git config –global user.email “<email address>”
git init:
git-init — Create an empty Git repository or reinitialize an existing one.
Git init creates an empty Git repository — like. git directory with subdirectories for objects, refs/heads, refs/tags, and template files.
Syntax:
$ git init git-demo
git clone:
git-clone — Clone a repository into a new directory
Git clone, Clones a repository into a newly created directory from the Git repository.
Syntax:
$ git clone <repo-URL>
git branch:
git-branch — List, create or delete branches
Git branch will list all the local branches and creating a new branch & deleting them in the current repository.
Syntax:
For new branch:
$ git branch <branch-name>
For listing branch:
$ git branch –list
For deleting branch:
$ git branch -d <branch-name>
git checkout:
git-checkout — Switch branches or restore working tree files
Git checkout used to move from one branch to another.
Syntax:
$ git checkout <your-branch-name >
Using this Git checkout command, we can create a new branch and also switches to that particular branch.
Syntax:
$ git checkout -b <branch-name>
git add:
git-add — Add file contents to the index
Git adds update the index (Add the files to staging area).
Syntax:
Add a single file:
$ git add <filename>
To add all files:
$ git add -A
git status:
git-status — Show the working tree status
With this command we can check whether it was staged, un-staged, and which files get modified or deleted.
Syntax:
$ git status
git commit:
git-commit — Record changes to the repository
This command used to save your changes to the local repository. A commit containing the current contents of the index and the given log message describing the changes.
Syntax:
$ git commit -m “your commit message”
git log:
git-log — Show commit logs
Git log is used to read a history of everything that happens to a repository, and log contains commit hash, commit author metadata, commit date metadata, commit message.
Syntax:
$ git log
git rm:
git-rm — Remove files from the working tree and from the index
Git rm command will remove the file from the repository. The git rm command removes the file not only from the repository but also from the staging area.
Syntax:
$ git rm <filename>
git stash:
git-stash — Stash the changes in a dirty working directory away
Git stash temporarily saves your data safely without committing, some options of git stash — Git stash save, Git stash list, Git stash clear, etc…,
Syntax:
$ git stash
git push:
git-push — Update remote refs along with associated objects
It will push your local changes to remote repository, after committing changes locally.
Syntax:
$ git push <remote-URL> <branch-name>
Or
$ git push -u origin <branch-name>
git pull:
git-pull — Fetch from and integrate with another repository or a local branch
git pull command acquires the latest changes from the remote repository to the local repository, and its combination of git fetch and git merge commands.
Syntax:
$ git pull <remote-URL>
git revert:
git-revert — Revert some existing commits
The Git revert command is used to undo the changes to our local repository or remote repo. Just give the hash code that you want to undo the changes.
Let’s take two branches:
Branch A
Branch B
git revert B will create a commit that undoes changes in B.
git revert A will create a commit that undoes changes in A, but will not touch changes in B
Note that if changes in B are dependent on changes in A, the revert of A is not possible.
Syntax:
$ git revert <hash-code>
Step1:
$ git log (copy the hash code)
Step2:
$ git revert b0a877b
Step3:
Check for git log
$ git log
git merge:
git-merge — Join two or more development histories together
Git Merge is used by git pull to incorporate changes from another repository and can be used by hand to merge changes from one branch into another.
Syntax:
$ git merge <branch-name>
If you want to learn more about git commands Or Start with DevOps Course. Join us at Backbench Academy.
Have joyful learning.!