Coding with Titans

so breaking things happens constantly, but never on purpose

GiT Cheat Sheet – Commands

GiT is a marvelous tool. It’s like a developer’s tool shaped into Swiss knife. It pays back to have it, yet you still need a bit of training not to make yourself hurt (like lose whole day’s work!). Or most importantly training to know, what is the command’s name for the task you are going to do. Those commands are not very obvious, somehow Mercurial and SVN did a better job here, that’s why I am providing my own list in this short tutorial. I dislike being forced to know everything by heart. Feel free to copy and use it as you like! Or share with me your own set of commands (here or via twitter).

Let’s start, how to:

  1. Remove a local branch, that was never pushed on server?

    git branch -d <branch_name>
    

    or if it was never merged into any other branch use -D instead, to force the deletion and forget about the code.

  2. Remove branch from the server, as it was pulled from remote, already merged and is no more needed?

  • delete locally:

    git branch -d <branch_name>
    
  • apply changes to remote (like a push of nothing to specified branch)

    git push origin :<branch_name>
    

    Server will then reply with a message similar to:

    - [deleted] <branch_name>

    Of course replace <branch_name> with respective name of the branch. And since every person, who cloned the repository has a full local copy, each of them has to remove the reference to remote, non-existing branch by typing (otherwise, they could accidentally push this branch again):

    git pull
    git remote prune origin
    

    UPDATE 2020-03: New simpler syntax is described here.

  1. Change last commit

    git commit --amend
    

    Additionally use --author="<name> <email>" to even override the author’s name. If there are any files in stash area, their new content will be applied as part of this commit and originally committed data will be lost. Of course the recommended moment to perform this operation is before pushing commits to server. Otherwise it will create a blast number of conflicts and other bad consequences.

  2. Reverse and forget about last commit

    git reset --hard HEAD~1
    

    This one also should be executed if needed only on local commits. Playing with commits sent back to server might start issuing conflicts.

  3. Revert last commit and keep changes in stash area

    git reset --soft HEAD~1
    

    It will simply undo the last git commit execution. Type again to remove selected file from stash area to keep it away of next commit:

    git reset <file_name>
    
  4. Remove tag from server (when placed accidentally)

  • remove it locally

    git tag -d &lt;tag_name&gt;
    
  • ask the server to perform the same operation

    git push origin :refs/tags/&lt;tag_name&gt;
    

Let the force be with You!