Top 15 Git Commands with Examples
$ sudo yum install git -y
$ sudo apt-get install git -y
Download from this link https://git-scm.com/download/win installs with the default configuration.
git-config — Get and set repository or global options.
Git Config sets the author name and email address to be used with your commits.
$ git config –global user.name “<name>”
$ git config –global user.email “<email address>”
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.
$ git init git-demo
git-clone — Clone a repository into a new directory
Git clone, Clones a repository into a newly created directory from the Git repository.
$ git clone <repo-URL>
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.
For new branch:
$ git branch <branch-name>
For listing branch:
$ git branch –list
For deleting branch:
$ git branch -d <branch-name>
git-checkout — Switch branches or restore working tree files
Git checkout used to move from one branch to another.
$ git checkout <your-branch-name >
Using this Git checkout command, we can create a new branch and also switches to that particular branch.
$ git checkout -b <branch-name>
git-add — Add file contents to the index
Git adds update the index (Add the files to staging area).
Add a single file:
$ git add <filename>
To add all files:
$ git add -A
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.
$ git status
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.
$ git commit -m “your commit message”
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.
$ git log
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.
$ git rm <filename>
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…,
$ git stash
git-push — Update remote refs along with associated objects
It will push your local changes to remote repository, after committing changes locally.
$ git push <remote-URL> <branch-name>
$ git push -u origin <branch-name>
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.
$ git pull <remote-URL>
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:
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.
$ git revert <hash-code>
$ git log (copy the hash code)
$ git revert b0a877b
Check for git log
$ git log
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.
$ git merge <branch-name>
Have joyful learning.!