WalzoneInterview Prep
📞 Interviewing soon? Practice with a realistic AI mock phone interview — it calls you, then scores you. First 15 min FREE →

Git · Advanced · question 56 of 100

What is the Git ’replace’ command, and what are some use cases for it?

📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

The git replace command is a feature in Git that allows developers to replace an object in the Git object database with another object, such as a commit, tag, or tree. This replacement is not permanent and can be undone at any time.

The git replace command can be useful in a number of scenarios. For example, it can be used to fix mistakes in a commit or a series of commits without having to rewrite history. It can also be used to create a temporary fix while waiting for a proper fix to be implemented. Additionally, git replace can be used to create custom views of the repository, such as a tree that reorganizes the directory structure.

Here is an example of how to use git replace to replace a commit with a new one:

    $ git replace 123abc new-commit

This command replaces the commit with the SHA-1 hash "123abc" with the new commit specified. From this point on, Git treats the replacement commit as if it were the original commit.

To undo the replacement, you can use the –delete option:

    $ git replace --delete 123abc

This command removes the replacement object and restores the original object in the Git object database.

It’s worth noting that git replace is a powerful command that should be used with care. In particular, replacing a commit can have unintended consequences if other branches or commits rely on that commit. For this reason, it is generally recommended to avoid using git replace on public repositories or in collaboration with others.

Reading is step one. Saying it out loud is the interview. Our AI interviewer calls your phone and runs a realistic Git interview — then scores it.
📞 Practice Git — free 15 min
📕 Buy this interview preparation book: 100 Git questions & answers — PDF + EPUB for $5

All 100 Git questions · All topics