Basic Git Commands

These are common Git commands used in various situations:

  • git init : Create an empty Git repository or reinitialize an existing one
  • git clone : Clone a repository into a new directory
  • git add : Add file contents to the index
  • git mv : Move or rename a file, a directory, or a symlink
  • git reset : Reset current HEAD to the specified state
  • git rm : Remove files from the working tree and from the index
  • git bisect : Use binary search to find the commit that introduced a bug
  • git grep : Print lines matching a pattern
  • git log : Show commit logs
  • git show : Show various types of objects
  • git status : Show the working tree status
  • git branch : List, create, or delete branches
  • git checkout : Switch branches or restore working tree files
  • git commit : Record changes to the repository
  • git diff : Show changes between commits, commit and working tree, etc
  • git merge : Join two or more development histories together
  • git rebase : Forward-port local commits to the updated upstream head
  • git tag : Create, list, delete or verify a tag object signed with GPG
  • git fetch : Download objects and refs from another repository
  • git pull : Fetch from and integrate with another repository or a local branch
  • git push : Update remote refs along with associated objects

Starting a working area

Git conceptually revolves around a repository. To begin working we need a repository on our local machine. We can either create a new repository or copy an existing repository.

top

git init

git init
To create a new git repository directory, (or to convert an existing source directory to a git repository), go to your desired directory in a terminal and provide the command git init.
This would create a .git directory inside the repository directory. If this directory is already a git repository, it would be re-initialized as a new repository.

top

git clone <repository_path> [directory]

git clone
To copy a repository from remote location to a new directory, we can make use of git clone command. This comes handy, when we want to contribute to an already existing code, on which other friends have already started working. Since this command connects to remote repository, it may require internet connection if the remote repository lies outside local network. Running it will create a directory as per the remote repo name.
If you want to clone repository in a custom directory name, you may pass that as an argument

Work on the current change

top

git add <file.path>

git add
Git maintains an index of all the files to be committed. If we are working on multiple files and want to add files selectively to the index, this command helps us in doing so.
A non-indexed file would show in red (default colour) and an indexed file would show in green when status is checked.
top

git mv <file.path> <new-file.path>

git mv
mv helps renaming or moving an existing file to new path.
top

git reset  <file.path>

git reset
It Resets current HEAD to the specified state. It is reverses the git add command.
top

git rm

git rm
rm Remove files from the working tree and from the index

examine the history and state

top

git bisect

git bisect
Bisect is a group of commands provided by git which use binary search to find a particular commit. E.g. if a commit caused an issue, but there are many commits after that and we want to find that particular commit, we can make use of this tool.
The first command is git bisect start . This starts the tool.
Now, we have to let this tool know about a commit which we are sure which is good.
git bisect good <commithashcode>
Now, we have to let this tool know about a commit which we are sure which is bad.
git bisect bad <commithashcode>
Now, git takes us to a commit, which we needs to check if this commit is good or bad and inform git by using command git bisect good/bad
This goes on, until we reach the commit that we are looking for.
To go back to original commit (from where we started bisecting) use the command git bisect reset.
top

git grep

git grep
This command looks for specified pattern in the working tree and prints the matching lines.
top

git log

git log
This command shows the commit logs from history
top

git show

git show
It shows various types of objects depending upon the input flags
top

git status

git status
It shows the working tree status

grow, mark and tweak your common history

top

git branch

git branch
It Lists, creates, or deletes specified branches
top

git checkout

git checkout
checkout Switch branches or restore working tree files
top

git commit

git commit
commit Record changes to the repository
top

git diff

git diff
diff Show changes between commits, commit and working tree, etc
top

git merge

git merge
merge Join two or more development histories together
top

git rebase

git rebase
rebase Forward-port local commits to the updated upstream head
top

git tag

git tag
tag Create, list, delete or verify a tag object signed with GPG

collaborate

top

git fetch

git fetch
fetch Download objects and refs from another repository
top
git pull

git pull

pull Fetch from and integrate with another repository or a local branch
top

git push

git push
push Update remote refs along with associated objects