Git : some useful tricks

In this post, I would like to list some useful commands while you are working with git.

DISCARD UNSTAGED CHANGES

For all unstaged files:

git checkout .

For a specific file:

git checkout path/to/file/to/revert

AMENDING THE COMMIT MESSAGE

You can set the commit message directly in the command line with:

git commit --amend -m 'New commit message'

GET REMOTE URL OF THE LOCAL GIT REPO

If referential integrity has been broken:

git config --get remote.origin.url

If referential integrity is intact:

git remote show origin

SWITCHING REMOTE URLs FROM SSH TO HTTPS

SOURCE: https://help.github.com/articles/changing-a-remote-s-url/

    • Change the current working directory to your local project.
    • List you existing remotes in order to get the name of the remote you want to change
$ git remote -v
origin  [email protected]:USERNAME/REPOSITORY.git (fetch)
origin  [email protected]:USERNAME/REPOSITORY.git (push)
    • Change your remote’s URL from SSH to HTTPS with the remote set-url command.
$ git remote set-url origin https://github.com/USERNAME/OTHERREPOSITORY.git
    • Verify that the remote URL has changed
$ git remote -v
# Verify new remote URL
origin  https://github.com/USERNAME/OTHERREPOSITORY.git (fetch)
origin  https://github.com/USERNAME/OTHERREPOSITORY.git (push)

SWITCHING REMOTE URLs FROM HTTPS TO SSH

SOURCE: https://help.github.com/articles/changing-a-remote-s-url/

    • Change the current working directory to your local project.
    • List you existing remotes in order to get the name of the remote you want to change
$ git remote -v
origin  https://github.com/USERNAME/REPOSITORY.git (fetch)
origin  https://github.com/USERNAME/REPOSITORY.git (push)
    • Change your remote’s URL from HTTPS to SSH with the remote set-url command.
$ git remote set-url origin [email protected]:USERNAME/OTHERREPOSITORY.git
    • Verify that the remote URL has changed
$ git remote -v
# Verify new remote URL
origin  [email protected]:USERNAME/OTHERREPOSITORY.git (fetch)
origin  [email protected]:USERNAME/OTHERREPOSITORY.git (push)

REMOVE COMMIT FROM PROJECT HISTORY

SOURCE: https://www.atlassian.com/git/tutorials/undoing-changes/git-revert

If you are tracking down a bug and find that it was introduced by a single commit, instead of manually going in, fixing it, and committing a new snapshot, you can use

git revert <commit>

It generate a new commit that undoes all of the changes introduced in commit, then apply it to the current branch.
It’s important to understand that git revert undoes a single commit; it does not “revert” back to the previous state of a project by removing all subsequent commits. In git, this is actually called a reset, not a revert.

DON’T RESET PUBLIC HISTORY

SOURCE: https://www.atlassian.com/git/tutorials/undoing-changes/git-revert

You should never use git reset commit when any snapshots after have been pushed to a public repository. After publishing a commit, you have to assume that other developers are reliant upon it.
Make sure that you are using git reset commit on a local experiment that went wrong, not on published changes.

REBASE THE development BRANCH ONTO THE master BRANCH

SOURCE: https://www.atlassian.com/git/tutorials/merging-vs-rebasing/conceptual-overview

RESET A LOCAL COMMIT

If you committed some wrong changes of your code, but you haven’t already pushed your commit on the remote branch, you can delete the commit via

About Francesco Serafin

I am a PhD student at the Department of Civil, Environmental and Mechanical Engineering at the University of Trento. My two greatest loves: Computer Science and Water (take three with my Lenovo!:D). As a result, the aim of my life is to make both passions coexist. My gpg public key available at https://pgp.mit.edu

2 Replies to “Git : some useful tricks”

  1. Useful article!
    I would also add ‘git stash’ to revert the working directory to match HEAD, but saving the unstaged modifications which you can then apply with ‘git stash apply’, if needed. I found it very useful! 😉

    • Thanks Giovanni for your suggestion!
      Surely, git stash is a very useful command but I have never used it so much. What do you think to add it to the post? This one might be your very first contribution to the blog…hopefully followed by many many others.

Leave a Reply

Your email address will not be published.

*