Skip to content

Latest commit

 

History

History
169 lines (113 loc) · 3.08 KB

git.md

File metadata and controls

169 lines (113 loc) · 3.08 KB

Git Commands

Stash superpowers

Great tutorial: https://www.atlassian.com/git/tutorials/saving-changes/git-stash

Knowing how to leverage git stash is a superpower and the following commands will allow you to do just that:

  • Basic use (will store current changes to the stash):
git stash
  • Save untracked files by:
git stash -u
  • View(list) your stash:
git stash list
  • Store a stash with a message:
git stash save "fire"
  • Pop a stash(will pop the recent most stash):
git stash pop
  • Pop a specific stash:
git stash pop <stash_id>
  • Apply a stash without removing it(will apply the recent most stash):
git stash apply
  • Apply a specific stash without removing it:
git stash apply <stash_id>

Regular

1. To stop tracking a file (which is present in .gitignore):

git rm --cached <file>

2. Keeping a fork upto date:

  • Add remote from original repository in your forked repository:

    cd into/cloned/fork-repo
    # remote name can be anything you want
    git remote add <remote_name> git://github.com/ORIGINAL-DEV-USERNAME/REPO-YOU-FORKED-FROM.git
    git fetch <remote_name>
  • Updating your fork from original repo to keep up with their changes:

    # for branch master (you can change this for any branch)
    git pull <remote_name> master
  • To update the remote fork on GitHub (or any other git hosting site):

    # this assumes that the remote forks name is origin (which is usually the case)
    # branch name can be changed up to your liking
    git push origin master

3. Amending a staged changed to a previous commit or updating the commit message:

  • Regular (will open selected editor for editing commit message):

    git commit --amend
  • Editing the commit message directly:

    git commit --amend -m "<your new message>"
  • Without editing commit message:

    git commit --amend --no-edit

4. Deleting tags (locally and remotely):

  • Delete local tag:

    git tag -d <tag>
  • Delete remote tag (for eg. on GitHub)

    git push --delete origin <tag>

5. Resetting a repo to a certain commit along with a remote(this will delete the changes made):

 git reset --hard <commit>
 git push -f <remote_name> master

6. Viewing the current code diff:

git diff

7. Reverting changes done in a folder:

  • If you have not yet indexed (git add) your changes:

    git checkout -- path/to/folder
  • If changes are indexed then you need to reset that first:

    git reset -- path/to/folder
    git checkout -- path/to/folder

8. Updating the local list of remote branches:

git remote update origin --prune

9. Renaming a local branch:

git branch -m <new_name>

10. Undoing your last commit:

git reset HEAD~