Got a thought this morning that i need to pen down the most used git commands. This is a short one but really handy list, hope it helps few.

  • Clone Existing Repository.
    git clone https://github.com/{username}/{reponame}.git
  • Status of your commit.
    git status
  • Switch branch and create local branch from remote branch.
    git checkout -t origin/develop
  • Create local branch.
    git checkout -b feature/PreparingProject
    Change it to -B to force checkout and create
  • Connect your local branch to existing remote branch.
    git push --set-upstream origin feature/PreparingProject
  • Add a file for check-in.
    git add .gitignore
  • Add all files/directory for check-in.
    git add .
  • Commit locally with message.
    git commit -m "My first Commit"
  • Push your local changes to Remote.
    git push
  • Disconnected with remote branch and to push your local changes to Remote.
    git push
    you might get the following error. fatal: No configured push destination.
    Either specify the URL from the command-line or configure a remote repository using

    git remote add {remotename} https://github.com/{username}/{reponname}.git
    git push --set-upstream {localbranch} {remotebranch}
  • Get latest changes to local from remote.
    git pull
  • Deleting your local branch.
    git branch -d feature/PreparingProject1
    Change it to -D to force delete
  • Delete remote branch (handle this command with care).
    git push origin --delete feature/PreparingProject1
  • List local branches and show current branch.
    git branch
  • List remote branches.
    git remote show
  • Show remote URL
    git config --get remote.origin.url
    git remote show origin
  • List remote branches and local branch.
    git branch -a
  • Get remote branches list updated.
    git fetch
  • Switch to new local branch from remote branch after fetch.
    git checkout -t remotes/origin/{branchname}
  • Clone a specific branch (This will download all remote branch and checkout the specified branch)
    git clone -b {branchname} https://github.com/{username}/{reponame}.git
  • Clone & Download only specific branch (This will download only the specified remote branch)
    git clone --single-branch -b {branchname} https://github.com/{username}/{reponame}.git
  • Show your commit logs.
    git log
  • Create new repository from existing repository
    git clone --bare https://github.com/{username}/{old-reponame}.git

    cd old-reponame

    git push --mirror https://github.com/{username}/{new-reponame}.git

    cd ..

    rm -rf old-reponame
  • Overwrite your local changes with code from remote. Usually we will end up getting the below error when pull and have local changes.
    git pull
    error: Your local changes to the following files would be overwritten by merge: ……
    Then we will do the below command for resetting to the code from remote and ignoring all changes done locally which is not committed. WARNING : you will lose local uncomitted changes, so you do realize what you are doing here :).
    git reset --hard origin/develop
  • Show git help.
    git help or git help -a