简体   繁体   中英

Git: Switch to local repository from working directory

As far I have understood, there are three stages in git workflow -

  1. Working directory which is actual local project workspace with.git file.
  2. Staging area where files are added from Working directory to be commited.
  3. Local repository where files are commited from the Staging area.

Suppose I have commited several files from the working directory. Those commited files will be in the local repository. Now, I need verify if all the required files are commited by running the project. How can I switch to local repository for running the code to verify commited changes before pushing them to remote?

For example, consider there are 3 files in the working directory - one.js, two.js, three.js and all the files are modified, but only two of them - one.js and two.js needs to be commited which contains inter-dependent changes. But suppose I only commit one.js and forgot to commit two.js and because of this the app will be broken in the local repository. For this case, I need to run and verfiy the app from local repository which contains only commited changes and not all the changes unlike working directory. How to achieve this?

Assuming git status does not say:

On branch Master nothing to commit, working tree clean ,

meaning you have some uncommited changes, then if you want to see the state of only the commited content, you can stash the currently made modifications with

git stash ,

then the working directory is clean and the stash contains the unsaved changes.

To get those modifications back do

git stash pop

for more information on stash look here .

You're correct that git has three storage areas - the work tree, the staging area (sometimes called the index), and what you refer to as the local repository[1] (the collection of objects, including COMMIT s, that make up the project history - normally called the database).

But of those, the only one you can compile, run, or load into a text editor is the working directory. So rather than "switch to the local repository" as you put it, what you would need to do to run the committed version is update the worktree (or, at least a worktree - there can be more than one) to match the committed version.

How exactly to do that depends on circumstances, and the situation you outlined is actually a little odd (and likely to cause you other, unrelated problems), which makes it hard to give a detailed answer.

First of all, the idea that "I've changed 3 files but only 2 of them need to be committed"... That's usually a sign of a problem waiting to happen. If the third file is not in the repo at all and is listed in .gitignore , it can be ok; but all else being equal, you are better off when your worktree is as close as possible to being just a reflection of the content you're committing.

If that third file isn't committed / is ignored, then you can use git status to confirm whether you've committed everything else. It will tell you which files have staged changes, which have unstaged changes, which have never been committed, etc. You can use git diff to get more detail about what changes have not been staged (or git diff --staged to see those that have been staged but not committed).

But to really know that what you've committed works, you need to "check out" the committed version.

If you don't have uncommitted changes you need to preserve, this is easy - git status will tell you how to revert to the committed state. (Generally git reset --:/: and then git checkout --:/: would work.)

If you do have uncommitted changes you need to preserve, you could stash them (but there are edge cases where that may not do exactly what you intend - eg if you have both staged and unstaged changes, you may end up in a situation where you get them all back unstaged). See https://git-scm.com/docs/git-stash

Or you can use a separate clone of the repo for testing. This is typical for build servers, but can also be done locally. A slightly lighter-weight solution is to create a second worktree attached to your existing repo. Docs supporting those options:

https://git-scm.com/docs/git-stage https://git-scm.com/docs/git-worktree


[1] The "repository" usually is a term for the whole thing - all three storage areas.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM