DEV Community

imbhanubista
imbhanubista

Posted on

If you're a programmer, these 10 git commands will save you hours of research🧵👇

Pull from remote overwriting local changes.

i) git stash (revert and stash changes locally)
ii) git pull (pull from remote normally)

tip: to retrieve changes, do 'git stash apply'

  • Delete only untracked files from working tree
    git clean -f -d

  • Unstage files from index (but keep the files locally)
    git rm --cached

  • Undo a merge
    i) git checkout branch-name
    ii) git log --oneline
    iii) git revert -m commit-id

  • Remove a file from remote
    i) git rm filename (remove file from git)
    ii) git commit -m "commit message"
    iii) git push

  • Undo last n commits
    git reset --soft HEAD^n (where n is the number of last commits you want to undo)

  • See diff between branches
    git diff branch_1..branch_2

  • Remove a tag from branch
    i) git push origin :refs/tags/tagname
    ii) git tag -d

  • Rename a branch locally & remote
    i) git checkout old-branch
    ii) git branch -m new-name
    iii) git push origin :old-name new-name

  • Move existing, uncommitted work to a new branch
    git switch -c (git 2.3 onwards)

Top comments (0)