Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Git: How does checking out affect the repository?

Tags:

git

I'm not at all experienced with programming, and I'm just starting to learn Git. I don't understand how git checkout works.

According to this:

Checking out an old commit is a read-only operation. It’s impossible to harm your repository while viewing an old revision. The “current” state of your project remains untouched in the master branch.

Yet the paragraph below, they state this:

On the other hand, checking out an old file does affect the current state of your repository.

Seems to me to be completely contradictory. Could someone please explain this to me in layman's terms?

like image 893
TheRealFakeNews Avatar asked Sep 28 '15 23:09

TheRealFakeNews


People also ask

What is the impact of git checkout?

If you check out another branch while having uncommitted changes, Git will "float" these changes over. If it is not possible to apply the changes cleanly, Git will refuse to check out the other branch. Simply create and check out the new branch just as you mention and commit the changes as usual.

Does git checkout make changes?

Simply making changes to the working copy with a given branch checked out doesn't preserve them in any way. If your working tree is clean (that is, it has no uncommitted changes), git checkout will completely change the state of your working tree to another branch.

What is checking out a repository?

Nov 18, 2020. The git checkout command navigates between two different branches in a Git repository. Checkout is used to view and make changes to different branches. You can check out a past commit in a repository to view how your project appeared in that state.

How does Checkout work in git?

You git checkout, you specify a commit, and then -- a file. It updates the staging area to match the commit. And then it updates the working area to match the staging area. [00:05:17] Again, this operation overwrites, it will overwrite files in your staging area that are staged.


1 Answers

The second sentence could better be written as:

checking out an old file does affect the current state of your workspace

If you think of the repository as the history of all versions of files throughout the project's history, the workspace is a snapshot of files in some state.

checkout moves content from the repository into the workspace. It only reads from the repository, but it writes to the workspace. Therefore it cannot change the repo, but it can change your workspace.

like image 101
Drew Noakes Avatar answered Nov 15 '22 06:11

Drew Noakes