Software Development

Useful Git Commands

Git is a most widely used and powerful version control system for tracking changes in computer files and coordinating work on those files among multiple people. It is primarily used for source code management in software development, but it can be used to keep track of changes in any set of files.

Git was developed by Linus Torvalds in 2005 as an distributed open source software version control software and of course it is free to use. As a distributed revision control system it is aimed at speed, data integrity, and support for distributed, non-linear work flows.

While other version control systems e.g. CVS, SVN keeps most of their data like commit logs on central server, every git repository on every computer is a full-fledged repository with complete history and full version tracking abilities, independent of network access or a central server.

However almost all IDEs support git out of the box and we do not require submit the git commands manually but it is always good to understand these commands. Below is a list of some git commands to work efficiently with git.

Git Help

The most useful command in git is
git help which provides us all the help which we require. If we type
git help in terminal, we will get

usage: git [--version] [--help] [-C <path>] [-c <name>=<value>]
           [--exec-path[=<path>]] [--html-path] [--man-path] [--info-path]
           [-p | --paginate | --no-pager] [--no-replace-objects] [--bare]
           [--git-dir=<path>] [--work-tree=<path>] [--namespace=<name>]
           <command> [<args>]

These are common Git commands used in various situations:

start a working area (see also: git help tutorial)
   clone      Clone a repository into a new directory
   init       Create an empty Git repository or reinitialize an existing one

work on the current change (see also: git help everyday)
   add        Add file contents to the index
   mv         Move or rename a file, a directory, or a symlink
   reset      Reset current HEAD to the specified state
   rm         Remove files from the working tree and from the index

examine the history and state (see also: git help revisions)
   bisect     Use binary search to find the commit that introduced a bug
   grep       Print lines matching a pattern
   log        Show commit logs
   show       Show various types of objects
   status     Show the working tree status

grow, mark and tweak your common history
   branch     List, create, or delete branches
   checkout   Switch branches or restore working tree files
   commit     Record changes to the repository
   diff       Show changes between commits, commit and working tree, etc
   merge      Join two or more development histories together
   rebase     Reapply commits on top of another base tip
   tag        Create, list, delete or verify a tag object signed with GPG

collaborate (see also: git help workflows)
   fetch      Download objects and refs from another repository
   pull       Fetch from and integrate with another repository or a local branch
   push       Update remote refs along with associated objects

'git help -a' and 'git help -g' list available subcommands and some concept guides.
See 'git help <command>' or 'git help <concept>' to read about a specific subcommand or concept.

Command
git help -a will us complete list of git commands

Available git commands in '/usr/local/git/libexec/git-core'

  add                       clone                     fetch                     interpret-trailers        notes                     remote-testsvn            submodule
  add--interactive          column                    fetch-pack                log                       p4                        repack                    submodule--helper
  am                        commit                    filter-branch             ls-files                  pack-objects              replace                   subtree
  annotate                  commit-tree               fmt-merge-msg             ls-remote                 pack-redundant            request-pull              svn
  apply                     config                    for-each-ref              ls-tree                   pack-refs                 rerere                    symbolic-ref
  archimport                count-objects             format-patch              mailinfo                  patch-id                  reset                     tag
  archive                   credential                fsck                      mailsplit                 prune                     rev-list                  unpack-file
  bisect                    credential-cache          fsck-objects              merge                     prune-packed              rev-parse                 unpack-objects
  bisect--helper            credential-cache--daemon  gc                        merge-base                pull                      revert                    update-index
  blame                     credential-store          get-tar-commit-id         merge-file                push                      rm                        update-ref
  branch                    cvsexportcommit           grep                      merge-index               quiltimport               send-email                update-server-info
  bundle                    cvsimport                 gui                       merge-octopus             read-tree                 send-pack                 upload-archive
  cat-file                  cvsserver                 gui--askpass              merge-one-file            rebase                    sh-i18n--envsubst         upload-pack
  check-attr                daemon                    hash-object               merge-ours                rebase--helper            shell                     var
  check-ignore              describe                  help                      merge-recursive           receive-pack              shortlog                  verify-commit
  check-mailmap             diff                      http-backend              merge-resolve             reflog                    show                      verify-pack
  check-ref-format          diff-files                http-fetch                merge-subtree             remote                    show-branch               verify-tag
  checkout                  diff-index                http-push                 merge-tree                remote-ext                show-index                web--browse
  checkout-index            diff-tree                 imap-send                 mergetool                 remote-fd                 show-ref                  whatchanged
  cherry                    difftool                  index-pack                mktag                     remote-ftp                stage                     worktree
  cherry-pick               difftool--helper          init                      mktree                    remote-ftps               stash                     write-tree
  citool                    fast-export               init-db                   mv                        remote-http               status
  clean                     fast-import               instaweb                  name-rev                  remote-https              stripspace

And command
git help -g will us a list git concepts

The common Git guides are:

   attributes   Defining attributes per path
   everyday     Everyday Git With 20 Commands Or So
   glossary     A Git glossary
   ignore       Specifies intentionally untracked files to ignore
   modules      Defining submodule properties
   revisions    Specifying revisions and ranges for Git
   tutorial     A tutorial introduction to Git (for version 1.5.1 or newer)
   workflows    An overview of recommended workflows with Git

We can use
git help <command> or
git help <concept> command to know more about about a specific command or concept.

Git Configuration

DescriptionGit Command
Configure the author name to be used with your commits.git config --global user.name "Sam Smith"
Configure the author email address to be used with your commitsgit config --global user.email sam@example.com
Will remove user credential details from the repositorygit config --local credential.helper ""
List all currently configured remote repository URLsgit remote -v
If you haven’t connected your local repository to a remote server, To add a remote server to a local repositorygit remote add origin <repo_url>

Git Commit and Push

DescriptionGit Command
Create a file name README.md with Readme content contentecho "Readme content" >> README.md
List the files you’ve changed and those you still need to add or commitgit status
Add all or one file to staginggit add . OR git add file_name
Commit changes to head with messagegit commit -m 'message'
Commit any files you’ve added with git add, and also commit any files you’ve changed since thengit commit -a
Send all commits from local repository to remote repositorygit push
Do a git push and sets the default remote branch for the current local branch. So any future git pull command will attempt to bring in commits from the <remote-branch> into the current local branchgit push -u <remote-branch>
Send changes to the master branch of your remote repositorygit push origin master
Push a specific branch to your remote repositorygit push origin <branch_name>
Push all branches to your remote repositorygit push --all origin

Git Checkout And Pull

DescriptionGit Command
To create a new local repositorygit init
Clone a repository into a new directorygit clone repo_url
Clone a repository into a new directory and point to mentioned branch_namegit clone --branch branch_name repo_url
To create a working copy of a local repositorygit clone /path/to/repository
Download objects and refs from remote repository for master branchgit fetch origin master
To merge a different branch into your active branchgit merge <branch_name>
Fetch and merge changes on the remote server to your working directory:git pull
View all the merge conflicts, View the conflicts against the base file, Preview changes, before merginggit diff, git diff --base <filename>, git diff <sourcebranch> <targetbranch>

Git Branch

DescriptionGit Command
List all the branches in your repository, and also tell you what branch you’re currently ingit branch
Switch from one branch to anothergit checkout branch_name
Create a new branch and switch to itgit checkout -b branch_name
Delete the feature branch from local repositorygit branch -d <branch_name>
Delete a branch on your remote repositorygit push origin :<branch_name>

Git Cleaning

DescriptionGit Command
Fetch the latest history (objects & refs) from the remote server for master branchgit fetch origin master
Clean repository to initial stagegit clean -x -d -f
Reset local repository and point your local master branch to latest history fetched from remote servergit reset --hard origin/master
To bring all changes from remote repository to local repositorygit pull origin master

Other Git commands

DescriptionGit Command
You can use tagging to mark a significant change set, such as a releasegit tag 1.0.0 <commitID>
Commit Id is the leading characters of the change set ID, up to 10, but must be unique. Get the ID usinggit log
Push all tags to remote repositorygit push --tags origin
If you mess up, you can replace the changes in your working tree with the last content in head:Changes already added to the index, as well as new files, will be keptgit checkout -- <filename>
Search the working directory for foo()git grep "foo()"

Published on Java Code Geeks with permission by Naresh Joshi, partner at our JCG program. See the original article here: Useful Git Commands

Opinions expressed by Java Code Geeks contributors are their own.

Naresh Joshi

Naresh is a senior software engineer working in banking domain while having experience in health and insurance as well. He writes is programming blog ProgrammingMitra to share his knowledge with others
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button