Skip to main content

Git Commands

Add

  • New changes

    git add <file.ext> # To add a specific file

    git add . # To add all the files in the current directory
  • New branch

    git branch <new name> # and remain in the current branch 

    git checkout -b <new name> # and switch to the new branch

    git checkout -b <new name> <another branch> # From another branch

  • New remote repository

    git remote add <shortname> <url>
  • Annotated tag

    git tag -a v1.4 -m "my version 1.4"
    git push --tags

Cherry-pick

  • An commit from the origin branch into my working branch

    git cherry-pick <commit-hash> <commit-hash>

Push

  • Push changes to remote repo

    git push <remote> <branch>
  • force the push even if it results in a non-fast-forward merge

    git push <remote> --force # Use the flag in case you know what you’re doing.
  • Push all of your local branches to the specified remote.

    git push <remote> --all

Clone

  • Existing repo into a new directory

    git clone <repo-url> <directory> # Replace "directory" with the directory you want
  • Existing repo into the current directory

    git clone <repo-url> . # The current directory is represented with a "."
  • existing repo along with submodules into the current directory

    git clone --recurse-submodules <repo-url> . 
  • submodules after cloning the existing repo

    git submodule update --init --recursive 

Commit

  • commit all local changes in tracked files

    git commit -a
  • commit all staged changes

    git commit -m <message> # Replace <message> with your commit message.
  • commit without any changes

    git commit --allow-empty -m <message> # Replace <message> with your commit message.

Compare two commits

  • and output results in the terminal

    git diff <sha1> <sha2> # the sha hash of the commits you want to compare.
  • and output result to a file

    git diff <sha1> <sha2> > diff.txt

Configure

  • name and email address

    git config --global user.name "username" 

    git config --global user.email "email address"
    # Your username and email address should be the same as the one used with your git hosting provider i.e. github, bitbucket etc
  • default editor

    git config --global core.editor "vim"
    # Use "code --wait" to set VS Code as default editor
  • external diff tool

    git config --global diff.external "meld"
    # You can change "meld" to "emerge" or "kompare"
  • default merge tool

    git config --global merge.tool "meld"
    # You can change "meld" to "emerge", "gvimdiff", "kdiff3", "vimdiff", and "tortoisemerge"
  • color

    git config --global color.ui auto # Enables colorization of CLI output
  • add the GPG key

    git config --global user.signingkey <your-secret-gpg-key>
    # If you’re taking work from others on the internet and want to verify that commits are actually from a trusted source.

Delete

  • Branch

    git branch -D <branch name>
  • Tag

    git tag -d v<tag version>
  • Remote

    git remote rm <remote>
  • Untracked files

    git clean -<flag>
    # replace -<flag> with:
    # -i for interactive command
    # -n to preview what will be removed
    # -f to remove forcefully
    # -d to remove directories
    # -X to remove ignored files
  • Files from index

    git rm --cached <file or dir>
  • Local branches that don't exist at remote

    git remote prune <remote-name>

Merge

  • Another branch to current branch

    git merge <branch-name>
  • Merge a single file from one branch to another.

    git checkout <branch name> <path to file> --patch

Modify

  • last/latest commit message

    git commit --amend
  • Repo's remote url

    git remote set-url <alias> <url> # <alias> is your remote name e.g origin
  • Change date and time of the commit

    git commit --amend --date="YYYY-MM-DD HH:MM:SS

Pull

  • Pull the specified remote’s copy of the current branch and merge it into local

    git pull <remote>
  • Gives output during a pull (displays the pulled content and the merge details)

    git pull --verbose
  • Pull changes and prevent merge conflicts

    git pull --ff-only # applies the remote changes only if they can be fast-forwarded

Rebase

  • An origin branch into working branch

    git pull --rebase origin <branch name>
  • Local branch into my working branch

    git rebase <branch name>
  • And skip commits

    git rebase --skip
    # In case of conflicts use this command to discard of your own changes in the current commit
    # and apply the changes from an incoming branch
  • And continue after resolving conflicts

    git rebase --continue
    # Use it whenever conflicts detected therefore you can resolve these conflicts manually and use this command to continue your rebase operation

Rename

  • Branch

    git branch -m <new name> # while working in the branch
    git branch -m <old name> <new name> # from outside the branch
  • Remote

    git remote rename <oldname> <newname>

Reset

  • a specific commit

    git revert <commit-hash> # Get a commit hash by using `git log`
  • a specific file

    git checkout <repo>/<branch> <filename>
  • To last commit

    git reset --hard
  • To last commit on remote branch

    git reset --hard <repo>/<branch>
  • Remove/reset all commits

    git update-ref -d HEAD

Squash

  • commits in pull request into single commit

    git rebase -i <branch name>
  • last n number of commit into one

    git reset --soft HEAD~N # N for number of commits you want to squash
    git add .
    git commit -m <message>

Stash

  • Create stash (Tracked and Untracked files)

    git stash
  • Create a new branch and apply stash

    git stash branch <branch name> <stash id>
  • Delete

    git stash clear # all stashed changes
    git stash drop <stash id> # specific stash
  • View the contents of a stash

    git stash show -p <stash id> #Leave stash ID to see the latest stash
  • Apply

    git stash apply
    git stash apply <stash id> # stash id can be gotten when you run git stash list
    git stash pop <stash id> # Stash id optional. Add it if you want to apply and delete a specific stash otherwise leave to pop the latest stash
  • View list of stashed changes

    git stash list

View

  • Status of project

    git status
  • Commit(s) log

    git log    # View all logs
    git log -n # for last n number of commits
    # to exit you have to press (q)
  • uncommitted changes

    git diff
  • Committed changes

    git diff
  • repo's remote url

    git remote -v
  • repo's remote url

    git branch # The active branch is prefixed with *
  • repo's remote url

    git tag