Showing posts with label GIT Delete. Show all posts
Showing posts with label GIT Delete. Show all posts

Friday, 28 December 2018

GIT: How can I delete a commit in Git?

Git offers a couple of tools to delete (or undo) older commits. The first step, therefore, is to find out which tool is best for your scenario.

Restoring an old version of a project

Do you want to restore an older revision of your project - just as it was at a certain point in time?
In that case, the "git reset" command is what best suits your needs. Read more in our detailed post about how to reset to a previous revision.

Undoing only a specific commit

A different scenario is when you want to revert the effects of a certain commit - without deleting any commits that came afterwards.
This is a case for the "git revert" command. Interestingly, the command doesn't deleteanything; it creates a new commit that introduces changes which revert the effects of the specified commit. Our post explains the details of how to go about reverting commits.

Delete or change specific commits

Another use case might be to delete a commit "in the middle" of your history, without resetting your whole project to a previous revision.
In that case, we'll have to bring out the big guns: Git's "Interactive Rebase" tool is what we need here. Please note that this is not only the most powerful, but also the most dangerous of the three commands listed here. This is because it allows you to change your commit history quite drastically - which is a dangerous process.
Read more about Interactive Rebase, including what to watch out for.

GIT: How can I delete branches in Git?

Before we look at deleting remote branches, let's discuss the syntax for deleting a local branch in Git.

Deleting local branches in Git

$ git branch -d feature/login
Using the "-d" flag, you tell "git branch" which item you want to delete.
Note that you might also need the "-f" flag if you're trying to delete a branch that contains unmerged changes. Use this option with care because it makes losing data very easy.

Deleting remote branches in Git

To delete a remote branch, we do not use the "git branch" command - but instead "git push" with the "--delete" flag:
$ git push origin --delete feature/login

Deleting both a local and a remote branch

Just a side note: please keep in mind that local and remote branches actually have nothing to do with each other. They are completely separate objects in Git.
Even if you've established a tracking connection (which you should for most scenarios), this still does not mean that deleting one would delete the other, too!
If you want any branch item to be deleted, you need to delete it explicitly.

Thursday, 27 December 2018

GIT: See which commit deleted a file

The git log command allows you to determine which commit deleted a file. You can use the -- option in git log to see the commit history for a file, even if you have deleted the file.
# see the changes of a file, works even
# if the file was deleted
git log -- [file_path]

# limit the output of Git log to the
# last commit, i.e. the commit which delete the file
# -1 to see only the last commit
# use 2 to see the last 2 commits etc
git log -1 -- [file_path]

# include stat parameter to see
# some statics, e.g., how many files were
# deleted
git log -1 --stat -- [file_path]