DEV Community

Robin Hooda
Robin Hooda

Posted on

Guide to Git commands

A blog on guide to git and its important commands that are used daily in the developer's life

Here is a list of 15 Git commands that you may not know yet, but hopefully they will help you out on a journey to master this tool.

  1. Modify The Most Recent Commit

git commit --amend
—-amend allows to append staged changes (e.g. to add a forgotten file) to the previous commit. Adding —-no-edit on top of that will amend the last commit without changing its commit message. If there are no changes, -—amend will allow you to reword the last commit message.

For more: git help commit

  1. Interactively Add Selected Parts of Files git add -p -p (or —patch) allows to interactively select parts of each tracked file to commit. This way each commit contains only related changes.

For more: git help add

  1. Interactively Stash Selected Parts of Files git stash -p Similar to git-add , you can use --patch option to interactively select parts of each tracked file to stash.

For more: git help stash

  1. Stash with untracked
    git stash -u
    By default, when stashing, the untracked files are not included. In order to change that bevahiour and include those files as well you need to use -u parameter. There is also -a (—all) which stashes both untracked and ignored files altogether, which is probably something you usually won’t need.

  2. Interactively Revert Selected Parts of Files
    git checkout -p
    --patch can be also used to selectively discard parts of each tracked file. I aliased this command as git discard

For more: git help checkout

  1. Switch to Previous Branch
    git checkout -
    This command allows you to quickly switch to the previously checked out branch. On a general note - is an alias for the previous branch. It can be used with other commands as well. I aliased checkout to co so, it becomes just git co -

  2. Revert All Local Changes
    git checkout .
    If you are sure that all of your local changes can be discarded, you can use . to do it at once. It is, however, a good practice to always use checkout --patch.

  3. Show changes
    git diff --staged
    This command shows all staged changes (those added to the index) in contrast to just git diff which only shows changes in the working directory (without those in the index).

For more: git help diff

  1. Rename Branches Locally
    git branch -m old-name new-name
    If you want to rename the currently checked out branch, you can shorten this command to the following form:
    git branch -m new-name
    For more: git help branch

  2. Rename Branches Remotely
    In order to rename a branch remotely, once you renamed your branch locally, you need to first remove that branch remotely and then push the renamed branch again.
    git push origin :old-name
    git push origin new-name

  3. Open All Files with Conflicts at Once
    Rebasing may lead to conflicts, the following command will open all files which need your help to resolve these conflicts.
    git diff --name-only --diff-filter=U | uniq | xargs $EDITOR

  4. What changed?
    git whatchanged —-since=‘2 weeks ago’
    This command will show a log with changes introduced by each commit from the last two weeks.

  5. Remove file from last commit
    Let's say you committed a file by mistake. You can quickly remove that file from the last commit by combining rm and commit --amend commands:
    git rm —-cached
    git commit —-amend

  6. Find Branches
    git branch --contains
    This command will show all branches that contain a particular commit.

  7. Optimize the repository locally
    git gc --prune=now --aggressive
    For more: git help gc

Top comments (0)