Message ID | 20181129215850.7278-8-pclouds@gmail.com (mailing list archive) |
---|---|
State | New, archived |
Headers | show |
Series | Introduce new commands switch-branch and restore-files | expand |
On Thu, Nov 29, 2018 at 2:03 PM Nguyễn Thái Ngọc Duy <pclouds@gmail.com> wrote: > > "git checkout" doing too many things is a source of confusion for many > users (and it even bites old timers sometimes). To rememdy that, the > command is now split in two: switch-branch and checkout-files. The "checkout-files" here....(will comment more on this below) > good old "git checkout" command is still here and will be until all > (or most of users) are sick of it. > > See the new man pages for the final design of these commands. The > actual implementation though is still pretty much the same as "git > checkout". Following patches will adjust their behavior to match the > man pages. > > Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> > --- > .gitignore | 2 + > Documentation/git-checkout.txt | 5 + > Documentation/git-restore-files.txt | 167 ++++++++++++++++ > Documentation/git-switch-branch.txt | 289 ++++++++++++++++++++++++++++ > Makefile | 2 + > builtin.h | 2 + > builtin/checkout.c | 84 ++++++-- > command-list.txt | 2 + > git.c | 2 + > 9 files changed, 543 insertions(+), 12 deletions(-) > create mode 100644 Documentation/git-restore-files.txt > create mode 100644 Documentation/git-switch-branch.txt > > diff --git a/.gitignore b/.gitignore > index 0d77ea5894..c63dcb1427 100644 > --- a/.gitignore > +++ b/.gitignore > @@ -143,6 +143,7 @@ > /git-request-pull > /git-rerere > /git-reset > +/git-restore-files ...and "restore-files" here. Should be consistent with whatever name you pick. > /git-rev-list > /git-rev-parse > /git-revert > @@ -167,6 +168,7 @@ > /git-submodule > /git-submodule--helper > /git-svn > +/git-switch-branch > /git-symbolic-ref > /git-tag > /git-unpack-file > diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt > index 25887a6087..25ec7f508f 100644 > --- a/Documentation/git-checkout.txt > +++ b/Documentation/git-checkout.txt > @@ -406,6 +406,11 @@ $ edit frotz > $ git add frotz > ------------ > > +SEE ALSO > +-------- > +linkgit:git-switch-branch[1] > +linkgit:git-restore-files[1] > + > GIT > --- > Part of the linkgit:git[1] suite > diff --git a/Documentation/git-restore-files.txt b/Documentation/git-restore-files.txt > new file mode 100644 > index 0000000000..03c1250ad0 > --- /dev/null > +++ b/Documentation/git-restore-files.txt > @@ -0,0 +1,167 @@ > +git-restore-files(1) > +==================== > + > +NAME > +---- > +git-restore-files - Restore working tree files > + > +SYNOPSIS > +-------- > +[verse] > +'git restore-files' [-f|--ours|--theirs|-m|--conflict=<style>] [--from=<tree-ish>] <pathspec>... Suggesting that you can use both --ours and --from? Or -f and --from? That seems bad; see below for more on this... > +'git restore-files' [--from=<tree-ish>] <pathspec>... Isn't this already inferred by the previous line? Or was the inclusion of --from on the previous line in error? Looking at the git-checkout manpage, it looks like you may have just been copying an existing weirdness, but it needs to be fixed. ;-) > +'git restore-files' (-p|--patch) [--from=<tree-ish>] [<pathspec>...] > + > +DESCRIPTION > +----------- > +Updates files in the working tree to match the version in the index > +or the specified tree. > + > +'git restore-files' [--from=<tree-ish>] <pathspec>...:: <tree-ish> and <pathspec>? I understand <commit-ish> and <pathspec>, or <tree-ish> but have no clue why it'd be okay to specify <tree-ish> and <pathspec> together. What does that even mean? Also, rather than fixing from <tree-ish> to <commit-ish> or <commit>, perhaps we should just use <revision> here? (I'm thinking of git rev-parse's "Specifying revisions", which suggests "revisions" as a good substitute for "commit-ish" that isn't quite so weird for new users.) > + > + Overwrite paths in the working tree by replacing with the > + contents in the index or in the <tree-ish> (most often a > + commit). When a <tree-ish> is given, the paths that > + match the <pathspec> are updated both in the index and in > + the working tree. Is that the default we really want for this command? Why do we automatically assume these files are ready for commit? I understand that it's what checkout did, but I'd find it more natural to only affect the working tree by default. We can give it an option for affecting the index instead (or perhaps in addition). > ++ > +The index may contain unmerged entries because of a previous failed merge. > +By default, if you try to check out such an entry from the index, the > +checkout operation will fail and nothing will be checked out. > +Using `-f` will ignore these unmerged entries. The contents from a > +specific side of the merge can be checked out of the index by > +using `--ours` or `--theirs`. With `-m`, changes made to the working tree > +file can be discarded to re-create the original conflicted merge result. > + > +'git restore-files' (-p|--patch) [--from=<tree-ish>] [<pathspec>...]:: > + This is similar to the "check out paths to the working tree > + from either the index or from a tree-ish" mode described > + above, but lets you use the interactive interface to show > + the "diff" output and choose which hunks to use in the > + result. See below for the description of `--patch` option. > + > +OPTIONS > +------- > +-q:: > +--quiet:: > + Quiet, suppress feedback messages. > + > +--[no-]progress:: > + Progress status is reported on the standard error stream > + by default when it is attached to a terminal, unless `--quiet` > + is specified. This flag enables progress reporting even if not > + attached to a terminal, regardless of `--quiet`. > + > +-f:: > +--force:: > + Do not fail upon unmerged entries; instead, unmerged entries > + are ignored. You just copied this from the checkout manpage, but this is ambiguous and/or wrong; using git-checkout (since your patch-series doesn't apply cleanly to either master or next for me): $ sha1sum counting c0ed0e34b0fbef4274ef59480e0a0a1cb2776870 counting $ git checkout counting; echo $? error: path 'counting' is unmerged 1 $ git checkout -f counting; echo $? warning: path 'counting' is unmerged 0 $ sha1sum counting c0ed0e34b0fbef4274ef59480e0a0a1cb2776870 counting Maybe printing a warning counts as "ignored", though it doesn't seem like it. However, even worse is: $ git checkout -f HEAD~1 counting $ sha1sum counting 612ca68d0305c821750a452e9d5bf050e915824f counting Now the unmerged entry wasn't ignored; it was updated in the working tree and overwritten in the index. Perhaps -f and --from should be incompatible and throw an error if both are specified? Also does "printed a warning message for" actually count as "ignored" or should the documentation for this option be updated? > +--ours:: > +--theirs:: > + Check out stage #2 ('ours') or #3 ('theirs') for unmerged > + paths. > ++ > +Note that during `git rebase` and `git pull --rebase`, 'ours' and > +'theirs' may appear swapped; `--ours` gives the version from the > +branch the changes are rebased onto, while `--theirs` gives the > +version from the branch that holds your work that is being rebased. > ++ > +This is because `rebase` is used in a workflow that treats the > +history at the remote as the shared canonical one, and treats the > +work done on the branch you are rebasing as the third-party work to > +be integrated, and you are temporarily assuming the role of the > +keeper of the canonical history during the rebase. As the keeper of > +the canonical history, you need to view the history from the remote > +as `ours` (i.e. "our shared canonical history"), while what you did > +on your side branch as `theirs` (i.e. "one contributor's work on top > +of it"). Total aside because I'm not sure what you could change here, but man do I hate this. > + > +--ignore-skip-worktree-bits:: > + In sparse checkout mode, update only entries matched by > + <paths> and sparse patterns in > + $GIT_DIR/info/sparse-checkout. This option ignores the sparse > + patterns and adds back any files in <paths>. This doesn't make any sense now that you've removed the "`git checkout -- <paths>` would" from the original. > + > +-m:: > +--merge:: > + When checking out paths from the index, this option lets you > + recreate the conflicted merge in the specified paths. > + > +--conflict=<style>:: > + The same as --merge option above, but changes the way the > + conflicting hunks are presented, overriding the > + merge.conflictStyle configuration variable. Possible values are > + "merge" (default) and "diff3" (in addition to what is shown by > + "merge" style, shows the original contents). > + > +-p:: > +--patch:: > + Interactively select hunks in the difference between the > + <tree-ish> (or the index, if unspecified) and the working > + tree. The chosen hunks are then applied in reverse to the > + working tree (and if a <tree-ish> was specified, the index). > ++ > +This means that you can use `git restore-files -p` to selectively > +discard edits from your current working tree. See the ``Interactive > +Mode'' section of linkgit:git-add[1] to learn how to operate the > +`--patch` mode. > + > +--[no-]recurse-submodules:: > + Using --recurse-submodules will update the content of all initialized > + submodules according to the commit recorded in the superproject. If > + local modifications in a submodule would be overwritten the checkout > + will fail unless `-f` is used. If nothing (or --no-recurse-submodules) > + is used, the work trees of submodules will not be updated. > + Just like linkgit:git-submodule[1], this will detach the > + submodules HEAD. > + > +<tree-ish>:: > + Tree to checkout from (when paths are given). If not specified, > + the index will be used. Again, I'd really rather use <revision> here. > + > +EXAMPLES > +-------- > + > +. The following sequence checks out the `master` branch, reverts > +the `Makefile` to two revisions back, deletes hello.c by > +mistake, and gets it back from the index. > ++ > +------------ > +$ git switch-branch master <1> > +$ git restore-files --from master~2 Makefile <2> > +$ rm -f hello.c > +$ git restore-files hello.c <3> > +------------ > ++ > +<1> switch branch > +<2> take a file out of another commit > +<3> restore hello.c from the index > ++ Why is the switch-branch command labelled but not the rm command? It made sense in the original checkout manpage to label all checkout commands, but here since only restore-files is being discussed it seems the switch-branch should lose its label. > +If you want to check out _all_ C source files out of the index, > +you can say > ++ > +------------ > +$ git restore-files '*.c' > +------------ > ++ > +Note the quotes around `*.c`. The file `hello.c` will also be > +checked out, even though it is no longer in the working tree, > +because the file globbing is used to match entries in the index > +(not in the working tree by the shell). Sounds good. > ++ > +If you have an unfortunate branch that is named `hello.c`, this > +step would be confused as an instruction to switch to that branch. > +You should instead write: > ++ > +------------ > +$ git restore-files hello.c > +------------ Isn't the point of this command to allow us to remove paragraphs like this last one? > + > +SEE ALSO > +-------- > +linkgit:git-checkout[1] > + > +GIT > +--- > +Part of the linkgit:git[1] suite My single biggest worry about this whole series is that I'm worried you're perpetuating and even further ingraining one of the biggest usability problems with checkout: people suggest and use it for reverting/restoring paths to a previous version, but it doesn't do that: git restore-files --from master~10 Documentation/ <edit some non-documentation files> git add -u git commit -m "Rationale for changing files including reverting Documentation/" In particular, now you have a mixture of files in Documentation/ from master~10 (er, now likely master~11) and HEAD~1; any files and sub-directories that existed in HEAD~1 still remain and are mixed with all other files in Documentation/ from the older commit. You may think this is a special case, but this particular issue results in some pretty bad surprises. Also, it was pretty surprising to me just how difficult it was to implement an svn-like revert in EasyGit, in large part because of this 'oversight' in git. git checkout -- <paths> to me has always been fundamentally wrong, but I just wasn't sure if I wanted to fight the backward compatibility battle and suggest changing it. With a new command, we definitely shouldn't be reinforcing this error. (And maybe we should consider taking the time to fix git checkout too.) > diff --git a/Documentation/git-switch-branch.txt b/Documentation/git-switch-branch.txt > new file mode 100644 > index 0000000000..d5bf5cb37d > --- /dev/null > +++ b/Documentation/git-switch-branch.txt > @@ -0,0 +1,289 @@ > +git-switch-branch(1) > +==================== > + > +NAME > +---- > +git-switch-branch - Switch branches > + > +SYNOPSIS > +-------- > +[verse] > +'git switch-branch' [-q] [-f] [-m] <branch> > +'git switch-branch' [-q] [-f] [-m] --detach [<commit>] > +'git switch-branch' [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>] You label the options as -b/-B here, but -c/-C below. Should be consistent. > + > +DESCRIPTION > +----------- > +Switch to a specified branch and update files in the working tree to > +match it. > + > +'git switch-branch' <branch>:: > + To prepare for working on <branch>, switch to it by updating > + the index and the files in the working tree. Local > + modifications to the files in the working tree are kept, so > + that they can be committed to the <branch>. > ++ > +If <branch> is not found but there does exist a tracking branch in > +exactly one remote (call it <remote>) with a matching name, treat as > +equivalent to > ++ > +------------ > +$ git switch-branch -b <branch> --track <remote>/<branch> > +------------ If we're making --detach explicit (which I think is good), shouldn't this also be made explicit? I think I saw Junio arguing for this in another thread. Also, this is another case where you used -b instead of -c. Finally, --track wasn't mentioned in the synopsis but it is shown the first time -b or -c is used? > ++ > +If the branch exists in multiple remotes and one of them is named by > +the `checkout.defaultRemote` configuration variable, we'll use that > +one for the purposes of disambiguation, even if the `<branch>` isn't > +unique across all remotes. Set it to > +e.g. `checkout.defaultRemote=origin` to always checkout remote > +branches from there if `<branch>` is ambiguous but exists on the > +'origin' remote. See also `checkout.defaultRemote` in > +linkgit:git-config[1]. So switch-branch will be controlled by checkout.* config variables? That probably makes the most sense, but it does dilute the advantage of adding these simpler commands. Also, the fact that we're trying to make a simpler command makes me think that removing the auto-vivify behavior from the default and adding a simple flag which users can pass to request will allow this part of the documentation to be hidden behind the appropriate flag, which may make it easier for users to compartmentalize the command and it's options, enabling them to learn as they go. > + > +'git switch-branch' -c|-C <new_branch> [<start_point>]:: > + > + Specifying `-c` causes a new branch to be created as if > + linkgit:git-branch[1] were called and then switched to. In > + this case you can use the `--track` or `--no-track` options, > + which will be passed to 'git branch'. As a convenience, > + `--track` without `-c` implies branch creation; see the > + description of `--track` below. Can we get rid of --track/--no-track and just provide a flag (which takes no arguments) for the user to use? Problems with --track: * it's not even in your synopsis * user has to repeat themselves (e.g. 'next' in two places from '-c next --track origin/next'); this repetition is BOTH laborious AND error-prone * it's rather inconsistent: --track is the default due to auto-vivify when the user specifies nothing but a branch name that doesn't exist yet, but when the user realizes the branch doesn't exist yet and asks to have it created then suddenly tracking is not the default?? I'm not sure what's best, but here's some food for thought: git switch-branch <branch> switches to <branch>, if it exists. Error cases: * If <branch> isn't actually a branch but a <tag> or <remote-tracking-branch> or <revision>, error out and suggest using --detach. * If <branch> isn't actually a branch but there is a similarly named <remote-tracking-branch> (e.g. origin/<branch>), then suggest using -c. git switch-branch -c <branch> creates <branch> and, if a relevant-remote-tracking branch exists, base the branch on that revision and set the new branch up to track it. Error cases: * If <branch> already exists, error out, suggesting -C or using a non-conflicting name instead. Other cases: * user wants a branch named 'master' that tracks 'origin/next'? Use git branch instead, don't support that in switch-branch. * user wants a branch named 'master' that doesn't track 'origin/master' despite 'origin/master' existing? Use git branch instead; don't support that in switch-branch. > ++ > +If `-C` is given, <new_branch> is created if it doesn't exist; > +otherwise, it is reset. This is the transactional equivalent of > ++ > +------------ > +$ git branch -f <branch> [<start_point>] > +$ git switch-branch <branch> > +------------ > ++ > +that is to say, the branch is not reset/created unless "git > +switch-branch" is successful. ...and when exactly would it fail? Reading this, it looks like the only possible error condition was removed due saying we'll reset the branch if it already exists, so it's rather confusing. > + > +'git switch-branch' --detach [<commit>]:: > + > + Prepare to work on a unnamed branch on top of <commit> (see > + "DETACHED HEAD" section), and updating the index and the files > + in the working tree. Local modifications to the files in the > + working tree are kept, so that the resulting working tree will > + be the state recorded in the commit plus the local > + modifications. > ++ > +When the <commit> argument is a branch name, the `--detach` option can > +be used to detach HEAD at the tip of the branch (`git switch-branch > +<branch>` would check out that branch without detaching HEAD). > ++ > +Omitting <commit> detaches HEAD at the tip of the current branch. > + > +OPTIONS > +------- > +-q:: > +--quiet:: > + Quiet, suppress feedback messages. > + > +--[no-]progress:: > + Progress status is reported on the standard error stream > + by default when it is attached to a terminal, unless `--quiet` > + is specified. This flag enables progress reporting even if not > + attached to a terminal, regardless of `--quiet`. > + > +-f:: > +--force:: > + Proceed even if the index or the working tree differs from > + HEAD. This is used to throw away local changes. Haven't thought through this thoroughly, but do we really need an option for that instead of telling users to 'git reset --hard HEAD' before switching branches if they want their stuff thrown away? > +-c <new_branch>:: > +--create <new_branch>:: > + Create a new branch named <new_branch> and start it at > + <start_point>; see linkgit:git-branch[1] for details. > + > +-C <new_branch>:: > +--force-create <new_branch>:: > + Creates the branch <new_branch> and start it at <start_point>; > + if it already exists, then reset it to <start_point>. This is > + equivalent to running "git branch" with "-f"; see > + linkgit:git-branch[1] for details. Makes sense, but let's get the -b/-B vs. -c/-C consistent. > + > +-t:: > +--track:: > + When creating a new branch, set up "upstream" configuration. See > + "--track" in linkgit:git-branch[1] for details. > ++ > +If no `-c` option is given, the name of the new branch will be derived > +from the remote-tracking branch, by looking at the local part of the > +refspec configured for the corresponding remote, and then stripping > +the initial part up to the "*". > +This would tell us to use "hack" as the local branch when branching > +off of "origin/hack" (or "remotes/origin/hack", or even > +"refs/remotes/origin/hack"). If the given name has no slash, or the above > +guessing results in an empty name, the guessing is aborted. You can > +explicitly give a name with `-c` in such a case. > + > +--no-track:: > + Do not set up "upstream" configuration, even if the > + branch.autoSetupMerge configuration variable is true. These two options and the intervening paragraph, while they make sense to me, seem like the kind of stuff we'd want to throw out -- or at least rework -- when trying to introduce a new command to simplify. But I already discussed that above. > +-l:: > + Create the new branch's reflog; see linkgit:git-branch[1] for > + details. ?? Jettison this. > + > +--detach:: > + Rather than checking out a branch to work on it, check out a > + commit for inspection and discardable experiments. > + This is the default behavior of "git checkout <commit>" when > + <commit> is not a branch name. See the "DETACHED HEAD" section > + below for details. > + > +--orphan <new_branch>:: > + Create a new 'orphan' branch, named <new_branch>, started from > + <start_point> and switch to it. The first commit made on this What?? started from <start_point>? The whole point of --orphan is you have no parent, i.e. no start point. Also, why does the explanation reference an argument that wasn't in the immediately preceding synopsis? > + new branch will have no parents and it will be the root of a new > + history totally disconnected from all the other branches and > + commits. > ++ > +The index and the working tree are adjusted as if you had previously run > +"git checkout <start_point>". This allows you to start a new history > +that records a set of paths similar to <start_point> by easily running > +"git commit -a" to make the root commit. > ++ > +This can be useful when you want to publish the tree from a commit > +without exposing its full history. You might want to do this to publish > +an open source branch of a project whose current tree is "clean", but > +whose full history contains proprietary or otherwise encumbered bits of > +code. > ++ > +If you want to start a disconnected history that records a set of paths > +that is totally different from the one of <start_point>, then you should > +clear the index and the working tree right after creating the orphan > +branch by running "git rm -rf ." from the top level of the working tree. > +Afterwards you will be ready to prepare your new files, repopulating the > +working tree, by copying them from elsewhere, extracting a tarball, etc. Ick. Seems overly complex. I'd rather that --orphan defaulted to clearing the index and working tree, and that one would need to pass HEAD for <start_point> if you wanted to start out with all those other files. That would certainly make the explanation a little clearer to users, and more natural when they start experimenting with it. However, --orphan is pretty special case. Do we perhaps want to leave it out of this new command and only include it in checkout? > +-m:: > +--merge:: > + If you have local modifications to one or more files that are > + different between the current branch and the branch to which > + you are switching, the command refuses to switch branches in > + order to preserve your modifications in context. However, > + with this option, a three-way merge between the current > + branch, your working tree contents, and the new branch is > + done, and you will be on the new branch. > ++ > +When a merge conflict happens, the index entries for conflicting > +paths are left unmerged, and you need to resolve the conflicts > +and mark the resolved paths with `git add` (or `git rm` if the merge > +should result in deletion of the path). > + > +--conflict=<style>:: > + The same as --merge option above, but changes the way the > + conflicting hunks are presented, overriding the > + merge.conflictStyle configuration variable. Possible values are > + "merge" (default) and "diff3" (in addition to what is shown by > + "merge" style, shows the original contents). > + > +--ignore-other-worktrees:: > + `git switch-branch` refuses when the wanted ref is already > + checked out by another worktree. This option makes it check > + the ref out anyway. In other words, the ref can be held by > + more than one worktree. seems rather dangerous...is the goal to be an easier-to-use suggestion for new users while checkout continues to exist, or is this command meant to handle all branch switching functionality that checkout has? > + > +--[no-]recurse-submodules:: > + Using --recurse-submodules will update the content of all initialized > + submodules according to the commit recorded in the superproject. If > + local modifications in a submodule would be overwritten the checkout > + will fail unless `-f` is used. If nothing (or --no-recurse-submodules) > + is used, the work trees of submodules will not be updated. > + Just like linkgit:git-submodule[1], this will detach the > + submodules HEAD. > + > +<branch>:: > + Branch to checkout; if it refers to a branch (i.e., a name that, > + when prepended with "refs/heads/", is a valid ref), then that > + branch is checked out. Otherwise, if it refers to a valid > + commit, your HEAD becomes "detached" and you are no longer on > + any branch (see below for details). I thought we requiring --detach in order to detach. Does this paragraph need updating? Also, if we require --detach when we'll be detaching HEAD, then this paragraph gets a LOT simpler. > ++ > +You can use the `"@{-N}"` syntax to refer to the N-th last > +branch/commit checked out using "git checkout" operation. You may > +also specify `-` which is synonymous to `"@{-1}`. > ++ > +As a special case, you may use `"A...B"` as a shortcut for the > +merge base of `A` and `B` if there is exactly one merge base. You can > +leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. I actually didn't know about the A...B special case for checkout. Interesting...but I'm starting to wonder if this is too much info for a "simplified command". > + > +<new_branch>:: > + Name for the new branch. > + > +<start_point>:: > + The name of a commit at which to start the new branch; see > + linkgit:git-branch[1] for details. Defaults to HEAD. Erm, so if <start_point> is given, and there is an associated remote tracking branch for the given branch name, perhaps we don't set up the automatic tracking in contrast to what I mentioned above? Hmm... > + > +DETACHED HEAD > +------------- > +include::detach-head.txt[] > + > +EXAMPLES > +-------- > + > +. The following sequence checks out the `master` branch. > ++ > +------------ > +$ git switch-branch master > +------------ > ++ > + > +. After working in the wrong branch, switching to the correct > +branch would be done using: > ++ > +------------ > +$ git switch-branch mytopic > +------------ > ++ > +However, your "wrong" branch and correct "mytopic" branch may > +differ in files that you have modified locally, in which case > +the above checkout would fail like this: > ++ > +------------ > +$ git switch-branch mytopic > +error: You have local changes to 'frotz'; not switching branches. > +------------ > ++ > +You can give the `-m` flag to the command, which would try a > +three-way merge: > ++ > +------------ > +$ git switch-branch -m mytopic > +Auto-merging frotz > +------------ > ++ > +After this three-way merge, the local modifications are _not_ > +registered in your index file, so `git diff` would show you what > +changes you made since the tip of the new branch. > + > +. When a merge conflict happens during switching branches with > +the `-m` option, you would see something like this: > ++ > +------------ > +$ git switch-branch -m mytopic > +Auto-merging frotz > +ERROR: Merge conflict in frotz > +fatal: merge program failed > +------------ > ++ > +At this point, `git diff` shows the changes cleanly merged as in > +the previous example, as well as the changes in the conflicted > +files. Edit and resolve the conflict and mark it resolved with > +`git add` as usual: > ++ > +------------ > +$ edit frotz > +$ git add frotz > +------------ > + > +SEE ALSO > +-------- > +linkgit:git-checkout[1] > + > +GIT > +--- > +Part of the linkgit:git[1] suite > diff --git a/Makefile b/Makefile > index 1a44c811aa..f035dbab9e 100644 > --- a/Makefile > +++ b/Makefile > @@ -777,9 +777,11 @@ BUILT_INS += git-format-patch$X > BUILT_INS += git-fsck-objects$X > BUILT_INS += git-init$X > BUILT_INS += git-merge-subtree$X > +BUILT_INS += git-restore-files$X > BUILT_INS += git-show$X > BUILT_INS += git-stage$X > BUILT_INS += git-status$X > +BUILT_INS += git-switch-branch$X > BUILT_INS += git-whatchanged$X > > # what 'all' will build and 'install' will install in gitexecdir, > diff --git a/builtin.h b/builtin.h > index 6538932e99..01ed43ea69 100644 > --- a/builtin.h > +++ b/builtin.h > @@ -214,6 +214,7 @@ extern int cmd_remote_fd(int argc, const char **argv, const char *prefix); > extern int cmd_repack(int argc, const char **argv, const char *prefix); > extern int cmd_rerere(int argc, const char **argv, const char *prefix); > extern int cmd_reset(int argc, const char **argv, const char *prefix); > +extern int cmd_restore_files(int argc, const char **argv, const char *prefix); > extern int cmd_rev_list(int argc, const char **argv, const char *prefix); > extern int cmd_rev_parse(int argc, const char **argv, const char *prefix); > extern int cmd_revert(int argc, const char **argv, const char *prefix); > @@ -227,6 +228,7 @@ extern int cmd_show_index(int argc, const char **argv, const char *prefix); > extern int cmd_status(int argc, const char **argv, const char *prefix); > extern int cmd_stripspace(int argc, const char **argv, const char *prefix); > extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix); > +extern int cmd_switch_branch(int argc, const char **argv, const char *prefix); > extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix); > extern int cmd_tag(int argc, const char **argv, const char *prefix); > extern int cmd_tar_tree(int argc, const char **argv, const char *prefix); > diff --git a/builtin/checkout.c b/builtin/checkout.c > index 764e1a83a1..7dc0f4d3f3 100644 > --- a/builtin/checkout.c > +++ b/builtin/checkout.c > @@ -33,6 +33,16 @@ static const char * const checkout_usage[] = { > NULL, > }; > > +static const char * const switch_branch_usage[] = { > + N_("git switch-branch [<options>] [<branch>]"), > + NULL, > +}; > + > +static const char * const restore_files_usage[] = { > + N_("git restore-files [<options>] [<branch>] -- <file>..."), > + NULL, > +}; > + > struct checkout_opts { > int patch_mode; > int quiet; > @@ -1302,31 +1312,23 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts, > return newopts; > } > > -int cmd_checkout(int argc, const char **argv, const char *prefix) > +static int checkout_main(int argc, const char **argv, const char *prefix, > + struct checkout_opts *opts, struct option *options, > + const char * const usagestr[]) > { > - struct checkout_opts real_opts; > - struct checkout_opts *opts = &real_opts; > struct branch_info new_branch_info; > int dwim_remotes_matched = 0; > - struct option *options = NULL; > > - memset(opts, 0, sizeof(*opts)); > memset(&new_branch_info, 0, sizeof(new_branch_info)); > opts->overwrite_ignore = 1; > opts->prefix = prefix; > opts->show_progress = -1; > - opts->dwim_new_local_branch = 1; > > git_config(git_checkout_config, opts); > > opts->track = BRANCH_TRACK_UNSPECIFIED; > > - options = parse_options_dup(options); > - options = add_common_options(opts, options); > - options = add_switch_branch_options(opts, options); > - options = add_checkout_path_options(opts, options); > - > - argc = parse_options(argc, argv, prefix, options, checkout_usage, > + argc = parse_options(argc, argv, prefix, options, usagestr, > PARSE_OPT_KEEP_DASHDASH); > > if (opts->show_progress < 0) { > @@ -1455,3 +1457,61 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) > return checkout_branch(opts, &new_branch_info); > } > } > + > +int cmd_checkout(int argc, const char **argv, const char *prefix) > +{ > + struct checkout_opts opts; > + struct option *options = NULL; > + int ret; > + > + memset(&opts, 0, sizeof(opts)); > + opts.dwim_new_local_branch = 1; > + > + options = parse_options_dup(options); > + options = add_common_options(&opts, options); > + options = add_switch_branch_options(&opts, options); > + options = add_checkout_path_options(&opts, options); > + > + ret = checkout_main(argc, argv, prefix, &opts, > + options, checkout_usage); > + FREE_AND_NULL(options); > + return ret; > +} > + > +int cmd_switch_branch(int argc, const char **argv, const char *prefix) > +{ > + struct checkout_opts opts; > + struct option *options = NULL; > + int ret; > + > + memset(&opts, 0, sizeof(opts)); > + opts.dwim_new_local_branch = 1; > + > + options = parse_options_dup(options); > + options = add_common_options(&opts, options); > + options = add_switch_branch_options(&opts, options); > + > + ret = checkout_main(argc, argv, prefix, &opts, > + options, switch_branch_usage); > + FREE_AND_NULL(options); > + return ret; > +} > + > +int cmd_restore_files(int argc, const char **argv, const char *prefix) > +{ > + struct checkout_opts opts; > + struct option *options = NULL; > + int ret; > + > + memset(&opts, 0, sizeof(opts)); > + opts.dwim_new_local_branch = 1; > + > + options = parse_options_dup(options); > + options = add_common_options(&opts, options); > + options = add_checkout_path_options(&opts, options); > + > + ret = checkout_main(argc, argv, prefix, &opts, > + options, restore_files_usage); > + FREE_AND_NULL(options); > + return ret; > +} > diff --git a/command-list.txt b/command-list.txt > index 3a9af104b5..4638802754 100644 > --- a/command-list.txt > +++ b/command-list.txt > @@ -151,6 +151,7 @@ git-replace ancillarymanipulators complete > git-request-pull foreignscminterface complete > git-rerere ancillaryinterrogators > git-reset mainporcelain worktree > +git-restore-files mainporcelain worktree > git-revert mainporcelain > git-rev-list plumbinginterrogators > git-rev-parse plumbinginterrogators > @@ -171,6 +172,7 @@ git-status mainporcelain info > git-stripspace purehelpers > git-submodule mainporcelain > git-svn foreignscminterface > +git-switch-branch mainporcelain history > git-symbolic-ref plumbingmanipulators > git-tag mainporcelain history > git-unpack-file plumbinginterrogators > diff --git a/git.c b/git.c > index 2f604a41ea..a2be6c3eb5 100644 > --- a/git.c > +++ b/git.c > @@ -542,6 +542,7 @@ static struct cmd_struct commands[] = { > { "replace", cmd_replace, RUN_SETUP }, > { "rerere", cmd_rerere, RUN_SETUP }, > { "reset", cmd_reset, RUN_SETUP }, > + { "restore-files", cmd_restore_files, RUN_SETUP | NEED_WORK_TREE }, > { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT }, > { "rev-parse", cmd_rev_parse, NO_PARSEOPT }, > { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, > @@ -557,6 +558,7 @@ static struct cmd_struct commands[] = { > { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, > { "stripspace", cmd_stripspace }, > { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT }, > + { "switch-branch", cmd_switch_branch, RUN_SETUP | NEED_WORK_TREE }, > { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, > { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG }, > { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT }, > -- > 2.20.0.rc1.380.g3eb999425c.dirty >
Elijah Newren <newren@gmail.com> writes: >> +Updates files in the working tree to match the version in the index >> +or the specified tree. >> + >> +'git restore-files' [--from=<tree-ish>] <pathspec>...:: > > <tree-ish> and <pathspec>? I understand <commit-ish> and <pathspec>, > or <tree-ish> but have no clue why it'd be okay to specify <tree-ish> > and <pathspec> together. What does that even mean? I have this tree object v2.6.11-tree that is not enclosed in a commit object. I want to take the top-level Makefile out of that tree, stuff it in the index and overwrite the working tree file. $ git checkout v2.6.11-tree Makefile $ git restore-files --from=v2.6.11-tree Makefile >> + Overwrite paths in the working tree by replacing with the >> + contents in the index or in the <tree-ish> (most often a >> + commit). When a <tree-ish> is given, the paths that >> + match the <pathspec> are updated both in the index and in >> + the working tree. > > Is that the default we really want for this command? Why do we > automatically assume these files are ready for commit? I understand > that it's what checkout did, but I'd find it more natural to only > affect the working tree by default. We can give it an option for > affecting the index instead (or perhaps in addition). Oooah. Now this is getting juicy. I do think supporting "--index" (which would make it more in line with what Duy wrote), with optionally "--cached" as well, and making the "working tree only" mode as default may not be a bad idea. I am offhand not sure how the "working tree only" mode (similar to the default mode of "git apply" that mimics the way "patch -p1" works) should interact with the non-overlay mode of the command, but other than that, I tend to agree with the idea that restore-files is only a part of making the contents into committable shape, not exactly ready for it yet.
Thanks for all the comments! There are still some I haven't replied (either I'll agree and do it anyway, or I'll need some more time to digest) On Tue, Dec 4, 2018 at 1:45 AM Elijah Newren <newren@gmail.com> wrote: > > +'git restore-files' [--from=<tree-ish>] <pathspec>... > > Isn't this already inferred by the previous line? Or was the > inclusion of --from on the previous line in error? Looking at the > git-checkout manpage, it looks like you may have just been copying an > existing weirdness, but it needs to be fixed. ;-) Hehe. > > +'git restore-files' (-p|--patch) [--from=<tree-ish>] [<pathspec>...] > > + > > +DESCRIPTION > > +----------- > > +Updates files in the working tree to match the version in the index > > +or the specified tree. > > + > > +'git restore-files' [--from=<tree-ish>] <pathspec>...:: > > <tree-ish> and <pathspec>? I understand <commit-ish> and <pathspec>, > or <tree-ish> but have no clue why it'd be okay to specify <tree-ish> > and <pathspec> together. What does that even mean? > > Also, rather than fixing from <tree-ish> to <commit-ish> or <commit>, > perhaps we should just use <revision> here? (I'm thinking of git > rev-parse's "Specifying revisions", which suggests "revisions" as a > good substitute for "commit-ish" that isn't quite so weird for new > users.) tree-ish is technically more accurate. But I'm ok with just <revision>. If you give it a blob oid then you should get a nice explanation what you're doing wrong anyway. > > + Overwrite paths in the working tree by replacing with the > > + contents in the index or in the <tree-ish> (most often a > > + commit). When a <tree-ish> is given, the paths that > > + match the <pathspec> are updated both in the index and in > > + the working tree. > > Is that the default we really want for this command? Why do we > automatically assume these files are ready for commit? I understand > that it's what checkout did, but I'd find it more natural to only > affect the working tree by default. We can give it an option for > affecting the index instead (or perhaps in addition). Yeah, that behavior of updating the index always bothers me when I use it but I seemed to forget when working on this. > > +--ours:: > > +--theirs:: > > + Check out stage #2 ('ours') or #3 ('theirs') for unmerged > > + paths. > > ++ > > +Note that during `git rebase` and `git pull --rebase`, 'ours' and > > +'theirs' may appear swapped; `--ours` gives the version from the > > +branch the changes are rebased onto, while `--theirs` gives the > > +version from the branch that holds your work that is being rebased. > > ++ > > +This is because `rebase` is used in a workflow that treats the > > +history at the remote as the shared canonical one, and treats the > > +work done on the branch you are rebasing as the third-party work to > > +be integrated, and you are temporarily assuming the role of the > > +keeper of the canonical history during the rebase. As the keeper of > > +the canonical history, you need to view the history from the remote > > +as `ours` (i.e. "our shared canonical history"), while what you did > > +on your side branch as `theirs` (i.e. "one contributor's work on top > > +of it"). > > Total aside because I'm not sure what you could change here, but man > do I hate this. Uh it's actually documented? I'm always confused by this too. --ours and --theirs at this point are pretty much tied to stage 2 and 3. Nothing I can do about it. But if you could come up with some other option names, then we could make "new ours" to be stage 3 during rebase, for example. > > +Part of the linkgit:git[1] suite > > > My single biggest worry about this whole series is that I'm worried > you're perpetuating and even further ingraining one of the biggest > usability problems with checkout: people suggest and use it for > reverting/restoring paths to a previous version, but it doesn't do > that: > > git restore-files --from master~10 Documentation/ > <edit some non-documentation files> > git add -u > git commit -m "Rationale for changing files including reverting Documentation/" > > In particular, now you have a mixture of files in Documentation/ from > master~10 (er, now likely master~11) and HEAD~1; any files and > sub-directories that existed in HEAD~1 still remain and are mixed with > all other files in Documentation/ from the older commit. > > You may think this is a special case, but this particular issue > results in some pretty bad surprises. Also, it was pretty surprising > to me just how difficult it was to implement an svn-like revert in > EasyGit, in large part because of this 'oversight' in git. git > checkout -- <paths> to me has always been fundamentally wrong, but I > just wasn't sure if I wanted to fight the backward compatibility > battle and suggest changing it. With a new command, we definitely > shouldn't be reinforcing this error. (And maybe we should consider > taking the time to fix git checkout too.) What would be the right behavior for git restore-files --from=master~10 Documentation/ then? Consider it an error? I often use "git checkout HEAD" and "git checkout HEAD^" (usually with -p) but not very far back like master~10. > > +If the branch exists in multiple remotes and one of them is named by > > +the `checkout.defaultRemote` configuration variable, we'll use that > > +one for the purposes of disambiguation, even if the `<branch>` isn't > > +unique across all remotes. Set it to > > +e.g. `checkout.defaultRemote=origin` to always checkout remote > > +branches from there if `<branch>` is ambiguous but exists on the > > +'origin' remote. See also `checkout.defaultRemote` in > > +linkgit:git-config[1]. > > So switch-branch will be controlled by checkout.* config variables? > That probably makes the most sense, but it does dilute the advantage > of adding these simpler commands. > > Also, the fact that we're trying to make a simpler command makes me > think that removing the auto-vivify behavior from the default and > adding a simple flag which users can pass to request will allow this > part of the documentation to be hidden behind the appropriate flag, > which may make it easier for users to compartmentalize the command and > it's options, enabling them to learn as they go. Sounds good. I don't know a good name for this new option though so unless anybody comes up with some suggestion, I'll just disable checkout.defaultRemote in switch-branch. If it comes back as a new option, it can always be added later. > > +'git switch-branch' -c|-C <new_branch> [<start_point>]:: > > + > > + Specifying `-c` causes a new branch to be created as if > > + linkgit:git-branch[1] were called and then switched to. In > > + this case you can use the `--track` or `--no-track` options, > > + which will be passed to 'git branch'. As a convenience, > > + `--track` without `-c` implies branch creation; see the > > + description of `--track` below. > > Can we get rid of --track/--no-track and just provide a flag (which > takes no arguments) for the user to use? Problems with --track: > * it's not even in your synopsis > * user has to repeat themselves (e.g. 'next' in two places from '-c > next --track origin/next'); this repetition is BOTH laborious AND > error-prone > * it's rather inconsistent: --track is the default due to > auto-vivify when the user specifies nothing but a branch name that > doesn't exist yet, but when the user realizes the branch doesn't exist > yet and asks to have it created then suddenly tracking is not the > default?? I don't think --track is default anymore (maybe I haven't updated the man page correctly). The dwim behavior is only activated in switch-branch when you specify --guess to reduce the amount of magic we throw at the user. With that in mind, do we still hide --track/--no-track from switch-branch? > I'm not sure what's best, but here's some food for thought: > > > git switch-branch <branch> > switches to <branch>, if it exists. Error cases: > * If <branch> isn't actually a branch but a <tag> or > <remote-tracking-branch> or <revision>, error out and suggest using > --detach. > * If <branch> isn't actually a branch but there is a similarly named > <remote-tracking-branch> (e.g. origin/<branch>), then suggest using > -c. I would make these advice so I can hide them. Or if I manage to make all these hints one line then I'll make it unconditional. > git switch-branch -c <branch> > creates <branch> and, if a relevant-remote-tracking branch exists, > base the branch on that revision and set the new branch up to track Hmm.. this is a bit magical and could be surprising. If I create (and switch to) a new branch foo, I don't necessarily mean tracking origin/foo (I may not even think about origin/foo when I type the command). So tentatively no. > > +If `-C` is given, <new_branch> is created if it doesn't exist; > > +otherwise, it is reset. This is the transactional equivalent of > > ++ > > +------------ > > +$ git branch -f <branch> [<start_point>] > > +$ git switch-branch <branch> > > +------------ > > ++ > > +that is to say, the branch is not reset/created unless "git > > +switch-branch" is successful. > > ...and when exactly would it fail? Reading this, it looks like the > only possible error condition was removed due saying we'll reset the > branch if it already exists, so it's rather confusing. Yeah probably just scrape it. The atomic nature is not worth highlighting. > > +'git switch-branch' --detach [<commit>]:: > > + > > + Prepare to work on a unnamed branch on top of <commit> (see > > + "DETACHED HEAD" section), and updating the index and the files > > + in the working tree. Local modifications to the files in the > > + working tree are kept, so that the resulting working tree will > > + be the state recorded in the commit plus the local > > + modifications. > > ++ > > +When the <commit> argument is a branch name, the `--detach` option can > > +be used to detach HEAD at the tip of the branch (`git switch-branch > > +<branch>` would check out that branch without detaching HEAD). > > ++ > > +Omitting <commit> detaches HEAD at the tip of the current branch. > > + > > +OPTIONS > > +------- > > +-q:: > > +--quiet:: > > + Quiet, suppress feedback messages. > > + > > +--[no-]progress:: > > + Progress status is reported on the standard error stream > > + by default when it is attached to a terminal, unless `--quiet` > > + is specified. This flag enables progress reporting even if not > > + attached to a terminal, regardless of `--quiet`. > > + > > +-f:: > > +--force:: > > + Proceed even if the index or the working tree differs from > > + HEAD. This is used to throw away local changes. > > Haven't thought through this thoroughly, but do we really need an > option for that instead of telling users to 'git reset --hard HEAD' > before switching branches if they want their stuff thrown away? For me it's just a bit more convenient. Hit an error when switching branch? Recall the command from bash history, stick -f in it and run. Elsewhere I think both Junio and Thomas (or maybe only Junio) suggests moving the "git reset" functionality without moving HEAD to one of these commands, which goes the opposite direction... > > +-c <new_branch>:: > > +--create <new_branch>:: > > + Create a new branch named <new_branch> and start it at > > + <start_point>; see linkgit:git-branch[1] for details. > > + > > +-C <new_branch>:: > > +--force-create <new_branch>:: > > + Creates the branch <new_branch> and start it at <start_point>; > > + if it already exists, then reset it to <start_point>. This is > > + equivalent to running "git branch" with "-f"; see > > + linkgit:git-branch[1] for details. > > Makes sense, but let's get the -b/-B vs. -c/-C consistent. Another option I'm considering is -n/-N (for _new_ branch). Maybe -c/-C is good enough. > > +-l:: > > + Create the new branch's reflog; see linkgit:git-branch[1] for > > + details. > > ?? Jettison this. Yep. It looks weird to me too. reflog is just behind the scene these days. Nobody need to explicitly ask for reflog anymore. > > +--orphan <new_branch>:: > > + Create a new 'orphan' branch, named <new_branch>, started from > > + <start_point> and switch to it. The first commit made on this > > What?? started from <start_point>? The whole point of --orphan is > you have no parent, i.e. no start point. Also, why does the > explanation reference an argument that wasn't in the immediately > preceding synopsis? I guess bad phrasing. It should be "switch to <start_point> first, then prepare the worktree so that the first commit will have no parent". Or something along that line. You should really review git-checkout.txt btw ;-) > > + new branch will have no parents and it will be the root of a new > > + history totally disconnected from all the other branches and > > + commits. > > ++ > > +The index and the working tree are adjusted as if you had previously run > > +"git checkout <start_point>". This allows you to start a new history > > +that records a set of paths similar to <start_point> by easily running > > +"git commit -a" to make the root commit. > > ++ > > +This can be useful when you want to publish the tree from a commit > > +without exposing its full history. You might want to do this to publish > > +an open source branch of a project whose current tree is "clean", but > > +whose full history contains proprietary or otherwise encumbered bits of > > +code. > > ++ > > +If you want to start a disconnected history that records a set of paths > > +that is totally different from the one of <start_point>, then you should > > +clear the index and the working tree right after creating the orphan > > +branch by running "git rm -rf ." from the top level of the working tree. > > +Afterwards you will be ready to prepare your new files, repopulating the > > +working tree, by copying them from elsewhere, extracting a tarball, etc. > > Ick. Seems overly complex. I'd rather that --orphan defaulted to > clearing the index and working tree, and that one would need to pass > HEAD for <start_point> if you wanted to start out with all those other > files. That would certainly make the explanation a little clearer to > users, and more natural when they start experimenting with it. > > However, --orphan is pretty special case. Do we perhaps want to leave > it out of this new command and only include it in checkout? I started this by simply splitting git-checkout in two commands that, combined, can do everything git-checkout can. Then suggestions to have better default came in and I think we started to drift further to _removing_ options and falling back to git-checkout. I think we could still keep "complicated" options as long as they are clearly described and don't surprise users until they figure them out. That way I don't have to go back to git-checkout and deal with all the ambiguation it creates. > > +-m:: > > +--merge:: > > + If you have local modifications to one or more files that are > > + different between the current branch and the branch to which > > + you are switching, the command refuses to switch branches in > > + order to preserve your modifications in context. However, > > + with this option, a three-way merge between the current > > + branch, your working tree contents, and the new branch is > > + done, and you will be on the new branch. > > ++ > > +When a merge conflict happens, the index entries for conflicting > > +paths are left unmerged, and you need to resolve the conflicts > > +and mark the resolved paths with `git add` (or `git rm` if the merge > > +should result in deletion of the path). > > + > > +--conflict=<style>:: > > + The same as --merge option above, but changes the way the > > + conflicting hunks are presented, overriding the > > + merge.conflictStyle configuration variable. Possible values are > > + "merge" (default) and "diff3" (in addition to what is shown by > > + "merge" style, shows the original contents). > > + > > +--ignore-other-worktrees:: > > + `git switch-branch` refuses when the wanted ref is already > > + checked out by another worktree. This option makes it check > > + the ref out anyway. In other words, the ref can be held by > > + more than one worktree. > > seems rather dangerous...is the goal to be an easier-to-use suggestion > for new users while checkout continues to exist, or is this command > meant to handle all branch switching functionality that checkout has? As explained above. I'm still thinking the latter, but with fewer surprises and confusion. Though I guess I could be convinced to go with the former (the problem with the former is, even as a no-longer-new user, I still find git-checkout not that pleasant to use and want a better replacement) > > +<branch>:: > > + Branch to checkout; if it refers to a branch (i.e., a name that, > > + when prepended with "refs/heads/", is a valid ref), then that > > + branch is checked out. Otherwise, if it refers to a valid > > + commit, your HEAD becomes "detached" and you are no longer on > > + any branch (see below for details). > > I thought we requiring --detach in order to detach. Does this > paragraph need updating? Also, if we require --detach when we'll be > detaching HEAD, then this paragraph gets a LOT simpler. Yep. I really need to read through the document and update all of it. > > +You can use the `"@{-N}"` syntax to refer to the N-th last > > +branch/commit checked out using "git checkout" operation. You may > > +also specify `-` which is synonymous to `"@{-1}`. > > ++ > > +As a special case, you may use `"A...B"` as a shortcut for the > > +merge base of `A` and `B` if there is exactly one merge base. You can > > +leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. > > I actually didn't know about the A...B special case for checkout. > Interesting...but I'm starting to wonder if this is too much info for > a "simplified command". I could just hint about A...B and send the user to git-checkout.txt if they need to know more. They can learn about git-checkout that way too.
On Tue, Dec 4, 2018 at 8:22 AM Duy Nguyen <pclouds@gmail.com> wrote: > > Thanks for all the comments! There are still some I haven't replied > (either I'll agree and do it anyway, or I'll need some more time to > digest) > > On Tue, Dec 4, 2018 at 1:45 AM Elijah Newren <newren@gmail.com> wrote: > > > +'git restore-files' [--from=<tree-ish>] <pathspec>... > > > > Isn't this already inferred by the previous line? Or was the > > inclusion of --from on the previous line in error? Looking at the > > git-checkout manpage, it looks like you may have just been copying an > > existing weirdness, but it needs to be fixed. ;-) > > Hehe. > > > > +'git restore-files' (-p|--patch) [--from=<tree-ish>] [<pathspec>...] > > > + > > > +DESCRIPTION > > > +----------- > > > +Updates files in the working tree to match the version in the index > > > +or the specified tree. > > > + > > > +'git restore-files' [--from=<tree-ish>] <pathspec>...:: > > > > <tree-ish> and <pathspec>? I understand <commit-ish> and <pathspec>, > > or <tree-ish> but have no clue why it'd be okay to specify <tree-ish> > > and <pathspec> together. What does that even mean? > > > > Also, rather than fixing from <tree-ish> to <commit-ish> or <commit>, > > perhaps we should just use <revision> here? (I'm thinking of git > > rev-parse's "Specifying revisions", which suggests "revisions" as a > > good substitute for "commit-ish" that isn't quite so weird for new > > users.) > > tree-ish is technically more accurate. But I'm ok with just > <revision>. If you give it a blob oid then you should get a nice > explanation what you're doing wrong anyway. Documenting as <revision> but having it be more general under the hood and actually accept <tree-ish> sounds good to me. I just think the pain of trying to explain <tree-ish> is too much of a hurdle for users, especially as I expect it to be very unlikely that users will ever take advantage of it. > > > + Overwrite paths in the working tree by replacing with the > > > + contents in the index or in the <tree-ish> (most often a > > > + commit). When a <tree-ish> is given, the paths that > > > + match the <pathspec> are updated both in the index and in > > > + the working tree. > > > > Is that the default we really want for this command? Why do we > > automatically assume these files are ready for commit? I understand > > that it's what checkout did, but I'd find it more natural to only > > affect the working tree by default. We can give it an option for > > affecting the index instead (or perhaps in addition). > > Yeah, that behavior of updating the index always bothers me when I use > it but I seemed to forget when working on this. > > > > +--ours:: > > > +--theirs:: > > > + Check out stage #2 ('ours') or #3 ('theirs') for unmerged > > > + paths. > > > ++ > > > +Note that during `git rebase` and `git pull --rebase`, 'ours' and > > > +'theirs' may appear swapped; `--ours` gives the version from the > > > +branch the changes are rebased onto, while `--theirs` gives the > > > +version from the branch that holds your work that is being rebased. > > > ++ > > > +This is because `rebase` is used in a workflow that treats the > > > +history at the remote as the shared canonical one, and treats the > > > +work done on the branch you are rebasing as the third-party work to > > > +be integrated, and you are temporarily assuming the role of the > > > +keeper of the canonical history during the rebase. As the keeper of > > > +the canonical history, you need to view the history from the remote > > > +as `ours` (i.e. "our shared canonical history"), while what you did > > > +on your side branch as `theirs` (i.e. "one contributor's work on top > > > +of it"). > > > > Total aside because I'm not sure what you could change here, but man > > do I hate this. > > Uh it's actually documented? I'm always confused by this too. --ours > and --theirs at this point are pretty much tied to stage 2 and 3. > Nothing I can do about it. But if you could come up with some other > option names, then we could make "new ours" to be stage 3 during > rebase, for example. I don't think it's a naming issue, personally. Years ago we could have defined --ours and --theirs differently based on which kind of operation we were in the middle of, but you are probably right that they are now tied to stage 2 and 3. But there's another place that we might still be able to address this; I think the brain-damage here may have just been due to the fact that the recursive merge machinery was rather inflexible and required HEAD to be stage 2. If it were a little more flexible, then we might just be able to make this problem go away. Maybe it can still be fixed (I haven't dug too deeply into it), but if so, the only fix needed here would be to remove this long explanation about why the tool gets things totally backward. > > > +Part of the linkgit:git[1] suite > > > > > > My single biggest worry about this whole series is that I'm worried > > you're perpetuating and even further ingraining one of the biggest > > usability problems with checkout: people suggest and use it for > > reverting/restoring paths to a previous version, but it doesn't do > > that: > > > > git restore-files --from master~10 Documentation/ > > <edit some non-documentation files> > > git add -u > > git commit -m "Rationale for changing files including reverting Documentation/" > > > > In particular, now you have a mixture of files in Documentation/ from > > master~10 (er, now likely master~11) and HEAD~1; any files and > > sub-directories that existed in HEAD~1 still remain and are mixed with > > all other files in Documentation/ from the older commit. > > > > You may think this is a special case, but this particular issue > > results in some pretty bad surprises. Also, it was pretty surprising > > to me just how difficult it was to implement an svn-like revert in > > EasyGit, in large part because of this 'oversight' in git. git > > checkout -- <paths> to me has always been fundamentally wrong, but I > > just wasn't sure if I wanted to fight the backward compatibility > > battle and suggest changing it. With a new command, we definitely > > shouldn't be reinforcing this error. (And maybe we should consider > > taking the time to fix git checkout too.) > > What would be the right behavior for > > git restore-files --from=master~10 Documentation/ > > then? Consider it an error? I often use "git checkout HEAD" and "git > checkout HEAD^" (usually with -p) but not very far back like > master~10. Well, when you use a file rather than a directory: git restore-files --from=master~10 foo.c then you expect git diff master~10 foo.c to come back empty afterward. I expect the same if I give a directory rather than a file. (Even if it does make 'restore-files' feel slightly mis-named.) > > > +If the branch exists in multiple remotes and one of them is named by > > > +the `checkout.defaultRemote` configuration variable, we'll use that > > > +one for the purposes of disambiguation, even if the `<branch>` isn't > > > +unique across all remotes. Set it to > > > +e.g. `checkout.defaultRemote=origin` to always checkout remote > > > +branches from there if `<branch>` is ambiguous but exists on the > > > +'origin' remote. See also `checkout.defaultRemote` in > > > +linkgit:git-config[1]. > > > > So switch-branch will be controlled by checkout.* config variables? > > That probably makes the most sense, but it does dilute the advantage > > of adding these simpler commands. > > > > Also, the fact that we're trying to make a simpler command makes me > > think that removing the auto-vivify behavior from the default and > > adding a simple flag which users can pass to request will allow this > > part of the documentation to be hidden behind the appropriate flag, > > which may make it easier for users to compartmentalize the command and > > it's options, enabling them to learn as they go. > > Sounds good. I don't know a good name for this new option though so > unless anybody comes up with some suggestion, I'll just disable > checkout.defaultRemote in switch-branch. If it comes back as a new > option, it can always be added later. > > > > +'git switch-branch' -c|-C <new_branch> [<start_point>]:: > > > + > > > + Specifying `-c` causes a new branch to be created as if > > > + linkgit:git-branch[1] were called and then switched to. In > > > + this case you can use the `--track` or `--no-track` options, > > > + which will be passed to 'git branch'. As a convenience, > > > + `--track` without `-c` implies branch creation; see the > > > + description of `--track` below. > > > > Can we get rid of --track/--no-track and just provide a flag (which > > takes no arguments) for the user to use? Problems with --track: > > * it's not even in your synopsis > > * user has to repeat themselves (e.g. 'next' in two places from '-c > > next --track origin/next'); this repetition is BOTH laborious AND > > error-prone > > * it's rather inconsistent: --track is the default due to > > auto-vivify when the user specifies nothing but a branch name that > > doesn't exist yet, but when the user realizes the branch doesn't exist > > yet and asks to have it created then suddenly tracking is not the > > default?? > > I don't think --track is default anymore (maybe I haven't updated the > man page correctly). The dwim behavior is only activated in > switch-branch when you specify --guess to reduce the amount of magic > we throw at the user. With that in mind, do we still hide > --track/--no-track from switch-branch? Ooh, you're adding --guess? Cool, that addresses my concerns, just in a different manner. Personally, I'd leave --track/--no-track out. It's extra mental overhead, git branch has options for setting those if they need some special non-default setup, and if there is enough demand for it we can add it later. Removing options once published is much harder. > > I'm not sure what's best, but here's some food for thought: > > > > > > git switch-branch <branch> > > switches to <branch>, if it exists. Error cases: > > * If <branch> isn't actually a branch but a <tag> or > > <remote-tracking-branch> or <revision>, error out and suggest using > > --detach. > > * If <branch> isn't actually a branch but there is a similarly named > > <remote-tracking-branch> (e.g. origin/<branch>), then suggest using > > -c. > > I would make these advice so I can hide them. Or if I manage to make > all these hints one line then I'll make it unconditional. > > > git switch-branch -c <branch> > > creates <branch> and, if a relevant-remote-tracking branch exists, > > base the branch on that revision and set the new branch up to track > > Hmm.. this is a bit magical and could be surprising. If I create (and > switch to) a new branch foo, I don't necessarily mean tracking > origin/foo (I may not even think about origin/foo when I type the > command). So tentatively no. Yeah, if you're adding --guess then I'm happy. I do think, though, that if the user runs switch-branch to a branch that doesn't exist, we should check if there is an associated remote-tracking branch so that we can provide a better error message and help users learn about --guess. (Also, will there be a short -g form?) > > > > +If `-C` is given, <new_branch> is created if it doesn't exist; > > > +otherwise, it is reset. This is the transactional equivalent of > > > ++ > > > +------------ > > > +$ git branch -f <branch> [<start_point>] > > > +$ git switch-branch <branch> > > > +------------ > > > ++ > > > +that is to say, the branch is not reset/created unless "git > > > +switch-branch" is successful. > > > > ...and when exactly would it fail? Reading this, it looks like the > > only possible error condition was removed due saying we'll reset the > > branch if it already exists, so it's rather confusing. > > Yeah probably just scrape it. The atomic nature is not worth highlighting. > > > > > +'git switch-branch' --detach [<commit>]:: > > > + > > > + Prepare to work on a unnamed branch on top of <commit> (see > > > + "DETACHED HEAD" section), and updating the index and the files > > > + in the working tree. Local modifications to the files in the > > > + working tree are kept, so that the resulting working tree will > > > + be the state recorded in the commit plus the local > > > + modifications. > > > ++ > > > +When the <commit> argument is a branch name, the `--detach` option can > > > +be used to detach HEAD at the tip of the branch (`git switch-branch > > > +<branch>` would check out that branch without detaching HEAD). > > > ++ > > > +Omitting <commit> detaches HEAD at the tip of the current branch. > > > + > > > +OPTIONS > > > +------- > > > +-q:: > > > +--quiet:: > > > + Quiet, suppress feedback messages. > > > + > > > +--[no-]progress:: > > > + Progress status is reported on the standard error stream > > > + by default when it is attached to a terminal, unless `--quiet` > > > + is specified. This flag enables progress reporting even if not > > > + attached to a terminal, regardless of `--quiet`. > > > + > > > +-f:: > > > +--force:: > > > + Proceed even if the index or the working tree differs from > > > + HEAD. This is used to throw away local changes. > > > > Haven't thought through this thoroughly, but do we really need an > > option for that instead of telling users to 'git reset --hard HEAD' > > before switching branches if they want their stuff thrown away? > > For me it's just a bit more convenient. Hit an error when switching > branch? Recall the command from bash history, stick -f in it and run. > Elsewhere I think both Junio and Thomas (or maybe only Junio) suggests > moving the "git reset" functionality without moving HEAD to one of > these commands, which goes the opposite direction... Fair enough. > > > +-c <new_branch>:: > > > +--create <new_branch>:: > > > + Create a new branch named <new_branch> and start it at > > > + <start_point>; see linkgit:git-branch[1] for details. > > > + > > > +-C <new_branch>:: > > > +--force-create <new_branch>:: > > > + Creates the branch <new_branch> and start it at <start_point>; > > > + if it already exists, then reset it to <start_point>. This is > > > + equivalent to running "git branch" with "-f"; see > > > + linkgit:git-branch[1] for details. > > > > Makes sense, but let's get the -b/-B vs. -c/-C consistent. > > Another option I'm considering is -n/-N (for _new_ branch). Maybe > -c/-C is good enough. Actually, -n/-N seems like a good idea, especially since --guess also creates a branch. If we do stick with -c/-C, then we may need to document --guess as implying -c to avoid confusion (and then make sure we get the synopsis correct to show which flags can be used together). > > > +-l:: > > > + Create the new branch's reflog; see linkgit:git-branch[1] for > > > + details. > > > > ?? Jettison this. > > Yep. It looks weird to me too. reflog is just behind the scene these > days. Nobody need to explicitly ask for reflog anymore. > > > > +--orphan <new_branch>:: > > > + Create a new 'orphan' branch, named <new_branch>, started from > > > + <start_point> and switch to it. The first commit made on this > > > > What?? started from <start_point>? The whole point of --orphan is > > you have no parent, i.e. no start point. Also, why does the > > explanation reference an argument that wasn't in the immediately > > preceding synopsis? > > I guess bad phrasing. It should be "switch to <start_point> first, > then prepare the worktree so that the first commit will have no > parent". Or something along that line. > > You should really review git-checkout.txt btw ;-) I did after writing several of these comments, and yeah, it really needs a clean up. Seems like something someone would do when writing a (partial) replacement or simplified alternative. ;-) To be fair though, I suspect anyone familiar enough with git that looks at git-checkout.txt again is probably going to miss at least one thing that is bad for new users no matter how closely they look, just because they've grown accustomed to the documentation as it is. I was trying to help point out possible issues that I spotted, but I suspect others may be able to point out more. > > > + new branch will have no parents and it will be the root of a new > > > + history totally disconnected from all the other branches and > > > + commits. > > > ++ > > > +The index and the working tree are adjusted as if you had previously run > > > +"git checkout <start_point>". This allows you to start a new history > > > +that records a set of paths similar to <start_point> by easily running > > > +"git commit -a" to make the root commit. > > > ++ > > > +This can be useful when you want to publish the tree from a commit > > > +without exposing its full history. You might want to do this to publish > > > +an open source branch of a project whose current tree is "clean", but > > > +whose full history contains proprietary or otherwise encumbered bits of > > > +code. > > > ++ > > > +If you want to start a disconnected history that records a set of paths > > > +that is totally different from the one of <start_point>, then you should > > > +clear the index and the working tree right after creating the orphan > > > +branch by running "git rm -rf ." from the top level of the working tree. > > > +Afterwards you will be ready to prepare your new files, repopulating the > > > +working tree, by copying them from elsewhere, extracting a tarball, etc. > > > > Ick. Seems overly complex. I'd rather that --orphan defaulted to > > clearing the index and working tree, and that one would need to pass > > HEAD for <start_point> if you wanted to start out with all those other > > files. That would certainly make the explanation a little clearer to > > users, and more natural when they start experimenting with it. > > > > However, --orphan is pretty special case. Do we perhaps want to leave > > it out of this new command and only include it in checkout? > > I started this by simply splitting git-checkout in two commands that, > combined, can do everything git-checkout can. Then suggestions to have > better default came in and I think we started to drift further to > _removing_ options and falling back to git-checkout. > > I think we could still keep "complicated" options as long as they are > clearly described and don't surprise users until they figure them out. > That way I don't have to go back to git-checkout and deal with all the > ambiguation it creates. Fair enough...though I think it may make sense to also review the complicated options and determine if they are overly complicated. I think --orphan qualifies (I stumbled with it a bit for years the occasional time I needed to use it), and my small suggestion above would simplify both it and its description. We should probably also consider just removing <start_point> as an acceptable argument to --orphan; if people want files from some revision after creating an orphan branch that's a simple extra command. > > > +-m:: > > > +--merge:: > > > + If you have local modifications to one or more files that are > > > + different between the current branch and the branch to which > > > + you are switching, the command refuses to switch branches in > > > + order to preserve your modifications in context. However, > > > + with this option, a three-way merge between the current > > > + branch, your working tree contents, and the new branch is > > > + done, and you will be on the new branch. > > > ++ > > > +When a merge conflict happens, the index entries for conflicting > > > +paths are left unmerged, and you need to resolve the conflicts > > > +and mark the resolved paths with `git add` (or `git rm` if the merge > > > +should result in deletion of the path). > > > + > > > +--conflict=<style>:: > > > + The same as --merge option above, but changes the way the > > > + conflicting hunks are presented, overriding the > > > + merge.conflictStyle configuration variable. Possible values are > > > + "merge" (default) and "diff3" (in addition to what is shown by > > > + "merge" style, shows the original contents). > > > + > > > +--ignore-other-worktrees:: > > > + `git switch-branch` refuses when the wanted ref is already > > > + checked out by another worktree. This option makes it check > > > + the ref out anyway. In other words, the ref can be held by > > > + more than one worktree. > > > > seems rather dangerous...is the goal to be an easier-to-use suggestion > > for new users while checkout continues to exist, or is this command > > meant to handle all branch switching functionality that checkout has? > > As explained above. I'm still thinking the latter, but with fewer > surprises and confusion. Though I guess I could be convinced to go > with the former (the problem with the former is, even as a > no-longer-new user, I still find git-checkout not that pleasant to use > and want a better replacement) > > > > +<branch>:: > > > + Branch to checkout; if it refers to a branch (i.e., a name that, > > > + when prepended with "refs/heads/", is a valid ref), then that > > > + branch is checked out. Otherwise, if it refers to a valid > > > + commit, your HEAD becomes "detached" and you are no longer on > > > + any branch (see below for details). > > > > I thought we requiring --detach in order to detach. Does this > > paragraph need updating? Also, if we require --detach when we'll be > > detaching HEAD, then this paragraph gets a LOT simpler. > > Yep. I really need to read through the document and update all of it. > > > > +You can use the `"@{-N}"` syntax to refer to the N-th last > > > +branch/commit checked out using "git checkout" operation. You may > > > +also specify `-` which is synonymous to `"@{-1}`. > > > ++ > > > +As a special case, you may use `"A...B"` as a shortcut for the > > > +merge base of `A` and `B` if there is exactly one merge base. You can > > > +leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. > > > > I actually didn't know about the A...B special case for checkout. > > Interesting...but I'm starting to wonder if this is too much info for > > a "simplified command". > > I could just hint about A...B and send the user to git-checkout.txt if > they need to know more. They can learn about git-checkout that way > too. It may be fine to stay. Lots of my comments were just "let's try to note any weirdness or complication that might seem excessive so we at least have a conversation about it" (because it's easy to gloss over since we've looked at these documents so many times) rather than a "this definitely needs to go".
On Tue, Dec 4, 2018 at 6:43 PM Elijah Newren <newren@gmail.com> wrote: > > > > +--ours:: > > > > +--theirs:: > > > > + Check out stage #2 ('ours') or #3 ('theirs') for unmerged > > > > + paths. > > > > ++ > > > > +Note that during `git rebase` and `git pull --rebase`, 'ours' and > > > > +'theirs' may appear swapped; `--ours` gives the version from the > > > > +branch the changes are rebased onto, while `--theirs` gives the > > > > +version from the branch that holds your work that is being rebased. > > > > ++ > > > > +This is because `rebase` is used in a workflow that treats the > > > > +history at the remote as the shared canonical one, and treats the > > > > +work done on the branch you are rebasing as the third-party work to > > > > +be integrated, and you are temporarily assuming the role of the > > > > +keeper of the canonical history during the rebase. As the keeper of > > > > +the canonical history, you need to view the history from the remote > > > > +as `ours` (i.e. "our shared canonical history"), while what you did > > > > +on your side branch as `theirs` (i.e. "one contributor's work on top > > > > +of it"). > > > > > > Total aside because I'm not sure what you could change here, but man > > > do I hate this. > > > > Uh it's actually documented? I'm always confused by this too. --ours > > and --theirs at this point are pretty much tied to stage 2 and 3. > > Nothing I can do about it. But if you could come up with some other > > option names, then we could make "new ours" to be stage 3 during > > rebase, for example. > > I don't think it's a naming issue, personally. Years ago we could > have defined --ours and --theirs differently based on which kind of > operation we were in the middle of, but you are probably right that > they are now tied to stage 2 and 3. But there's another place that we > might still be able to address this; I think the brain-damage here may > have just been due to the fact that the recursive merge machinery was > rather inflexible and required HEAD to be stage 2. If it were a > little more flexible, then we might just be able to make this problem > go away. Maybe it can still be fixed (I haven't dug too deeply into > it), but if so, the only fix needed here would be to remove this long > explanation about why the tool gets things totally backward. Aha. I' not really deep in this merge business to know if stages 2 and 3 can be swapped. This is right up your alley. I'll just leave it to you. > > > > +'git switch-branch' -c|-C <new_branch> [<start_point>]:: > > > > + > > > > + Specifying `-c` causes a new branch to be created as if > > > > + linkgit:git-branch[1] were called and then switched to. In > > > > + this case you can use the `--track` or `--no-track` options, > > > > + which will be passed to 'git branch'. As a convenience, > > > > + `--track` without `-c` implies branch creation; see the > > > > + description of `--track` below. > > > > > > Can we get rid of --track/--no-track and just provide a flag (which > > > takes no arguments) for the user to use? Problems with --track: > > > * it's not even in your synopsis > > > * user has to repeat themselves (e.g. 'next' in two places from '-c > > > next --track origin/next'); this repetition is BOTH laborious AND > > > error-prone > > > * it's rather inconsistent: --track is the default due to > > > auto-vivify when the user specifies nothing but a branch name that > > > doesn't exist yet, but when the user realizes the branch doesn't exist > > > yet and asks to have it created then suddenly tracking is not the > > > default?? > > > > I don't think --track is default anymore (maybe I haven't updated the > > man page correctly). The dwim behavior is only activated in > > switch-branch when you specify --guess to reduce the amount of magic > > we throw at the user. With that in mind, do we still hide > > --track/--no-track from switch-branch? > > Ooh, you're adding --guess? Cool, that addresses my concerns, just in > a different manner. No it's always there even in git-checkout, just hidden. > Personally, I'd leave --track/--no-track out. It's extra mental > overhead, git branch has options for setting those if they need some > special non-default setup, and if there is enough demand for it we can > add it later. Removing options once published is much harder. Slightly less convenient (you would need a combination of git-branch and git-switch-branch, if you avoid git-checkout). But since I don't think I have ever used this option, I'm fine with leaving it out and considering adding it back later. > > > I'm not sure what's best, but here's some food for thought: > > > > > > > > > git switch-branch <branch> > > > switches to <branch>, if it exists. Error cases: > > > * If <branch> isn't actually a branch but a <tag> or > > > <remote-tracking-branch> or <revision>, error out and suggest using > > > --detach. > > > * If <branch> isn't actually a branch but there is a similarly named > > > <remote-tracking-branch> (e.g. origin/<branch>), then suggest using > > > -c. > > > > I would make these advice so I can hide them. Or if I manage to make > > all these hints one line then I'll make it unconditional. > > > > > git switch-branch -c <branch> > > > creates <branch> and, if a relevant-remote-tracking branch exists, > > > base the branch on that revision and set the new branch up to track > > > > Hmm.. this is a bit magical and could be surprising. If I create (and > > switch to) a new branch foo, I don't necessarily mean tracking > > origin/foo (I may not even think about origin/foo when I type the > > command). So tentatively no. > > Yeah, if you're adding --guess then I'm happy. I do think, though, > that if the user runs switch-branch to a branch that doesn't exist, we > should check if there is an associated remote-tracking branch so that > we can provide a better error message and help users learn about > --guess. (Also, will there be a short -g form?) Yeah better error and suggestion is a good idea. And yes the short form -g is already added (I did try to use it and find --guess too time consuming even with bash completion support). > > > > +-f:: > > > > +--force:: > > > > + Proceed even if the index or the working tree differs from > > > > + HEAD. This is used to throw away local changes. > > > > > > Haven't thought through this thoroughly, but do we really need an > > > option for that instead of telling users to 'git reset --hard HEAD' > > > before switching branches if they want their stuff thrown away? > > > > For me it's just a bit more convenient. Hit an error when switching > > branch? Recall the command from bash history, stick -f in it and run. > > Elsewhere I think both Junio and Thomas (or maybe only Junio) suggests > > moving the "git reset" functionality without moving HEAD to one of > > these commands, which goes the opposite direction... > > Fair enough. I'm actually still not sure how to move it here (I guess 'here' is restore-files since we won't move HEAD). All the --mixed, --merge and --hard are confusing. But maybe we could just make 'git restore-files --from HEAD -f :/" behave just like "git reset --hard HEAD" (but with some safety net) But we can leave it for discussion in the next round. > > > > +--orphan <new_branch>:: > > > > + Create a new 'orphan' branch, named <new_branch>, started from > > > > + <start_point> and switch to it. The first commit made on this > > > > > > What?? started from <start_point>? The whole point of --orphan is > > > you have no parent, i.e. no start point. Also, why does the > > > explanation reference an argument that wasn't in the immediately > > > preceding synopsis? > > > > I guess bad phrasing. It should be "switch to <start_point> first, > > then prepare the worktree so that the first commit will have no > > parent". Or something along that line. > > > > You should really review git-checkout.txt btw ;-) > > I did after writing several of these comments, and yeah, it really > needs a clean up. Seems like something someone would do when writing > a (partial) replacement or simplified alternative. ;-) Heh ;-) Fine I'll do it. I have to read and re-read git-checkout.txt anyway and already queued up a couple small fixes. > > > > + new branch will have no parents and it will be the root of a new > > > > + history totally disconnected from all the other branches and > > > > + commits. > > > > ++ > > > > +The index and the working tree are adjusted as if you had previously run > > > > +"git checkout <start_point>". This allows you to start a new history > > > > +that records a set of paths similar to <start_point> by easily running > > > > +"git commit -a" to make the root commit. > > > > ++ > > > > +This can be useful when you want to publish the tree from a commit > > > > +without exposing its full history. You might want to do this to publish > > > > +an open source branch of a project whose current tree is "clean", but > > > > +whose full history contains proprietary or otherwise encumbered bits of > > > > +code. > > > > ++ > > > > +If you want to start a disconnected history that records a set of paths > > > > +that is totally different from the one of <start_point>, then you should > > > > +clear the index and the working tree right after creating the orphan > > > > +branch by running "git rm -rf ." from the top level of the working tree. > > > > +Afterwards you will be ready to prepare your new files, repopulating the > > > > +working tree, by copying them from elsewhere, extracting a tarball, etc. > > > > > > Ick. Seems overly complex. I'd rather that --orphan defaulted to > > > clearing the index and working tree, and that one would need to pass > > > HEAD for <start_point> if you wanted to start out with all those other > > > files. That would certainly make the explanation a little clearer to > > > users, and more natural when they start experimenting with it. > > > > > > However, --orphan is pretty special case. Do we perhaps want to leave > > > it out of this new command and only include it in checkout? > > > > I started this by simply splitting git-checkout in two commands that, > > combined, can do everything git-checkout can. Then suggestions to have > > better default came in and I think we started to drift further to > > _removing_ options and falling back to git-checkout. > > > > I think we could still keep "complicated" options as long as they are > > clearly described and don't surprise users until they figure them out. > > That way I don't have to go back to git-checkout and deal with all the > > ambiguation it creates. > > Fair enough...though I think it may make sense to also review the > complicated options and determine if they are overly complicated. I > think --orphan qualifies (I stumbled with it a bit for years the > occasional time I needed to use it), and my small suggestion above > would simplify both it and its description. We should probably also > consider just removing <start_point> as an acceptable argument to > --orphan; if people want files from some revision after creating an > orphan branch that's a simple extra command. It is good that you pointed it out though. I still have to digest this option before I make more comments, but generally if there's a simpler (even if new) way to achieve the same thing, I'm all for it.
Duy Nguyen <pclouds@gmail.com> writes: >> My single biggest worry about this whole series is that I'm worried >> you're perpetuating and even further ingraining one of the biggest >> usability problems with checkout: people suggest and use it for >> reverting/restoring paths to a previous version, but it doesn't do >> that: > > ... > > git restore-files --from=master~10 Documentation/ The "single biggest worry" could be due to Elijah not being aware of other recent discussions. My understanding of the plan is - "git checkout" will learn a new "--[no-]overlay" option, where the current behaviour, i.e. "take paths in master~10 that match pathspec Documentation/, and overlay them on top of what is in the index and the working tree", is explained as "the overlay mode" and stays to be the default. With "checkout --no-overlay master~10 Documentation/", the command will become "replace paths in the current index and the working tree that match the pathspec Documentation/ with paths in master~10 that match pathspec Documentation/". - "git restore-files --from=<tree> <pathspec>" by default will use "--no-overlay" semantics, but the users can still use "--overlay" from the command line as an option. So "restore-files" would become truly "restore the state of Documentation/ to match that of master~10", I would think. >> Also, the fact that we're trying to make a simpler command makes me >> think that removing the auto-vivify behavior from the default and >> adding a simple flag which users can pass to request will allow this >> part of the documentation to be hidden behind the appropriate flag, >> which may make it easier for users to compartmentalize the command and >> it's options, enabling them to learn as they go. > > Sounds good. I don't know a good name for this new option though so > unless anybody comes up with some suggestion, I'll just disable > checkout.defaultRemote in switch-branch. If it comes back as a new > option, it can always be added later. Are you two discussing the "checkout --guess" option? I am somewhat lost here. >> > +-f:: >> > +--force:: >> > + Proceed even if the index or the working tree differs from >> > + HEAD. This is used to throw away local changes. >> >> Haven't thought through this thoroughly, but do we really need an >> option for that instead of telling users to 'git reset --hard HEAD' >> before switching branches if they want their stuff thrown away? > > For me it's just a bit more convenient. Hit an error when switching > branch? Recall the command from bash history, stick -f in it and run. > Elsewhere I think both Junio and Thomas (or maybe only Junio) suggests > moving the "git reset" functionality without moving HEAD to one of > these commands, which goes the opposite direction... Isn't there a huge difference? "checkout --force <other-branch>" needs to clobber only the changes that are involved in the switch, i.e. if your README.txt is the same between master and maint while Makefile is different, after editing both files while on master, you can not "switch-branch" to maint without doing something to Makefile (i.e. either discard your local change or wiggle your local change to the context of 'maint' with "checkout -m"). But you can carry the changes to README.txt while checking out 'maint' branch. Running "git reset --hard HEAD" would mean that you will lose the changes to README.txt as well. >> > +--orphan <new_branch>:: >> > + Create a new 'orphan' branch, named <new_branch>, started from >> > + <start_point> and switch to it. The first commit made on this >> >> What?? started from <start_point>? The whole point of --orphan is >> you have no parent, i.e. no start point. Also, why does the >> explanation reference an argument that wasn't in the immediately >> preceding synopsis? > > I guess bad phrasing. It should be "switch to <start_point> first, > then prepare the worktree so that the first commit will have no > parent". Or something along that line. It should be a <tree-ish>, no? It is not a "point" in history, but is "start with this tree". I may have more comments on this message but that's it from me for now.
Duy Nguyen <pclouds@gmail.com> writes: > On Tue, Dec 4, 2018 at 6:43 PM Elijah Newren <newren@gmail.com> wrote: >> > > > +--ours:: >> > > > +--theirs:: >> ... >> go away. Maybe it can still be fixed (I haven't dug too deeply into >> it), but if so, the only fix needed here would be to remove this long >> explanation about why the tool gets things totally backward. > > Aha. I' not really deep in this merge business to know if stages 2 and > 3 can be swapped. This is right up your alley. I'll just leave it to > you. Please don't show stage#2 and stage#3 swapped to the end user, unless that is protected behind an option (not per-repo config). It is pretty much ingrained that stage#2 is what came from the commit that will become the first parent of the commit being prepared, and changing it without an explicit command line option will break tools. > I'm actually still not sure how to move it here (I guess 'here' is > restore-files since we won't move HEAD). All the --mixed, --merge and > --hard are confusing. But maybe we could just make 'git restore-files > --from HEAD -f :/" behave just like "git reset --hard HEAD" (but with > some safety net) But we can leave it for discussion in the next round. Perhaps you two should pay a bit closer attention to what Thomas Gummerer is working on. I've touched above in my earlier comments, too, e.g. <xmqqefb3mhrs.fsf@gitster-ct.c.googlers.com>
On Tue, Dec 4, 2018 at 6:14 PM Junio C Hamano <gitster@pobox.com> wrote: > > Duy Nguyen <pclouds@gmail.com> writes: > > >> My single biggest worry about this whole series is that I'm worried > >> you're perpetuating and even further ingraining one of the biggest > >> usability problems with checkout: people suggest and use it for > >> reverting/restoring paths to a previous version, but it doesn't do > >> that: > > > > ... > > > > git restore-files --from=master~10 Documentation/ > > The "single biggest worry" could be due to Elijah not being aware of > other recent discussions. My understanding of the plan is > > - "git checkout" will learn a new "--[no-]overlay" option, where > the current behaviour, i.e. "take paths in master~10 that match > pathspec Documentation/, and overlay them on top of what is in > the index and the working tree", is explained as "the overlay > mode" and stays to be the default. With "checkout --no-overlay > master~10 Documentation/", the command will become "replace paths > in the current index and the working tree that match the pathspec > Documentation/ with paths in master~10 that match pathspec > Documentation/". > > - "git restore-files --from=<tree> <pathspec>" by default will use > "--no-overlay" semantics, but the users can still use "--overlay" > from the command line as an option. > > So "restore-files" would become truly "restore the state of > Documentation/ to match that of master~10", I would think. Oh, sweet, that's awesome. I saw some of the other emails as I was scanning through and looked at the --overlay stuff since it sounded relevant but something in the explanation made me think it was about whether the command would write to the index or the working tree, rather than being about adding+overwriting vs. just overwriting. > >> Also, the fact that we're trying to make a simpler command makes me > >> think that removing the auto-vivify behavior from the default and > >> adding a simple flag which users can pass to request will allow this > >> part of the documentation to be hidden behind the appropriate flag, > >> which may make it easier for users to compartmentalize the command and > >> it's options, enabling them to learn as they go. > > > > Sounds good. I don't know a good name for this new option though so > > unless anybody comes up with some suggestion, I'll just disable > > checkout.defaultRemote in switch-branch. If it comes back as a new > > option, it can always be added later. > > Are you two discussing the "checkout --guess" option? I am somewhat > lost here. Generally what was being discussed was just that this manpage was rather complicated for the standard base-case due to the need to explain all the behavior associated with --guess since that option is on by default in checkout. And since --guess is controlled by checkout.defaultRemote, that was part of the extra complexity that had to be learned in the basic explanation, rather than letting users learn it when they learn a new flag. The critical part you were missing was part of the original text just before the quoted part was: >>> So switch-branch will be controlled by checkout.* config variables? >>> That probably makes the most sense, but it does dilute the advantage >>> of adding these simpler commands. Duy is responding to that even if it wasn't included in his quoting. > >> > +-f:: > >> > +--force:: > >> > + Proceed even if the index or the working tree differs from > >> > + HEAD. This is used to throw away local changes. > >> > >> Haven't thought through this thoroughly, but do we really need an > >> option for that instead of telling users to 'git reset --hard HEAD' > >> before switching branches if they want their stuff thrown away? > > > > For me it's just a bit more convenient. Hit an error when switching > > branch? Recall the command from bash history, stick -f in it and run. > > Elsewhere I think both Junio and Thomas (or maybe only Junio) suggests > > moving the "git reset" functionality without moving HEAD to one of > > these commands, which goes the opposite direction... > > Isn't there a huge difference? "checkout --force <other-branch>" > needs to clobber only the changes that are involved in the switch, > i.e. if your README.txt is the same between master and maint while > Makefile is different, after editing both files while on master, you > can not "switch-branch" to maint without doing something to Makefile > (i.e. either discard your local change or wiggle your local change > to the context of 'maint' with "checkout -m"). But you can carry > the changes to README.txt while checking out 'maint' branch. > Running "git reset --hard HEAD" would mean that you will lose the > changes to README.txt as well. Ah, indeed. Thanks for pointing out what I missed here. > >> > +--orphan <new_branch>:: > >> > + Create a new 'orphan' branch, named <new_branch>, started from > >> > + <start_point> and switch to it. The first commit made on this > >> > >> What?? started from <start_point>? The whole point of --orphan is > >> you have no parent, i.e. no start point. Also, why does the > >> explanation reference an argument that wasn't in the immediately > >> preceding synopsis? > > > > I guess bad phrasing. It should be "switch to <start_point> first, > > then prepare the worktree so that the first commit will have no > > parent". Or something along that line. > > It should be a <tree-ish>, no? It is not a "point" in history, but > is "start with this tree". Are you saying that referring to it as a tree will lessen the confusion users face when they think it's a commit that serves as a parent and get confused with the fact that this option is named "--orphan"? Or are you making some other orthogonal point here? > I may have more comments on this message but that's it from me for > now. Fair enough. Sorry if I've distracted from RC stuff with all my responses.
On Tue, Dec 4, 2018 at 6:26 PM Junio C Hamano <gitster@pobox.com> wrote: > > Duy Nguyen <pclouds@gmail.com> writes: > > > On Tue, Dec 4, 2018 at 6:43 PM Elijah Newren <newren@gmail.com> wrote: > >> > > > +--ours:: > >> > > > +--theirs:: > >> ... > >> go away. Maybe it can still be fixed (I haven't dug too deeply into > >> it), but if so, the only fix needed here would be to remove this long > >> explanation about why the tool gets things totally backward. > > > > Aha. I' not really deep in this merge business to know if stages 2 and > > 3 can be swapped. This is right up your alley. I'll just leave it to > > you. > > Please don't show stage#2 and stage#3 swapped to the end user, > unless that is protected behind an option (not per-repo config). > It is pretty much ingrained that stage#2 is what came from the > commit that will become the first parent of the commit being > prepared, and changing it without an explicit command line option > will break tools. What depends on stage#2 coming from the commit that will become the first parent? I wasn't thinking in terms of modifying checkout/restore-files/diff/etc in a way that would make them show things different than what was recorded in the index, I was rather musing on whether it was feasible to have rebase tell the merge machinery to treat HEAD as stage #3 and the other commit as stage #2 so that it was swapping what was actually recorded in the index. I know the merge machinery implicitly assumes HEAD == stage #2 in multiple places, and it'd obviously need a fair amount of fixing to handle this. I wasn't immediately aware of other things that would break. If you know of some, I'm happy to hear. Otherwise, I might go and learn the hard way (after I get around to the merge rewrite) why my idea is crazy. :-) > > I'm actually still not sure how to move it here (I guess 'here' is > > restore-files since we won't move HEAD). All the --mixed, --merge and > > --hard are confusing. But maybe we could just make 'git restore-files > > --from HEAD -f :/" behave just like "git reset --hard HEAD" (but with > > some safety net) But we can leave it for discussion in the next round. > > Perhaps you two should pay a bit closer attention to what Thomas > Gummerer is working on. I've touched above in my earlier comments, > too, e.g. <xmqqefb3mhrs.fsf@gitster-ct.c.googlers.com> Indeed, I'm excited about his changes now; I'll keep an eye out.
Elijah Newren <newren@gmail.com> writes: > What depends on stage#2 coming from the commit that will become the > first parent? How about "git diff --cc" for a starter? What came from HEAD's ancestry should appear first and then what came from the side branch that is merged into.
diff --git a/.gitignore b/.gitignore index 0d77ea5894..c63dcb1427 100644 --- a/.gitignore +++ b/.gitignore @@ -143,6 +143,7 @@ /git-request-pull /git-rerere /git-reset +/git-restore-files /git-rev-list /git-rev-parse /git-revert @@ -167,6 +168,7 @@ /git-submodule /git-submodule--helper /git-svn +/git-switch-branch /git-symbolic-ref /git-tag /git-unpack-file diff --git a/Documentation/git-checkout.txt b/Documentation/git-checkout.txt index 25887a6087..25ec7f508f 100644 --- a/Documentation/git-checkout.txt +++ b/Documentation/git-checkout.txt @@ -406,6 +406,11 @@ $ edit frotz $ git add frotz ------------ +SEE ALSO +-------- +linkgit:git-switch-branch[1] +linkgit:git-restore-files[1] + GIT --- Part of the linkgit:git[1] suite diff --git a/Documentation/git-restore-files.txt b/Documentation/git-restore-files.txt new file mode 100644 index 0000000000..03c1250ad0 --- /dev/null +++ b/Documentation/git-restore-files.txt @@ -0,0 +1,167 @@ +git-restore-files(1) +==================== + +NAME +---- +git-restore-files - Restore working tree files + +SYNOPSIS +-------- +[verse] +'git restore-files' [-f|--ours|--theirs|-m|--conflict=<style>] [--from=<tree-ish>] <pathspec>... +'git restore-files' [--from=<tree-ish>] <pathspec>... +'git restore-files' (-p|--patch) [--from=<tree-ish>] [<pathspec>...] + +DESCRIPTION +----------- +Updates files in the working tree to match the version in the index +or the specified tree. + +'git restore-files' [--from=<tree-ish>] <pathspec>...:: + + Overwrite paths in the working tree by replacing with the + contents in the index or in the <tree-ish> (most often a + commit). When a <tree-ish> is given, the paths that + match the <pathspec> are updated both in the index and in + the working tree. ++ +The index may contain unmerged entries because of a previous failed merge. +By default, if you try to check out such an entry from the index, the +checkout operation will fail and nothing will be checked out. +Using `-f` will ignore these unmerged entries. The contents from a +specific side of the merge can be checked out of the index by +using `--ours` or `--theirs`. With `-m`, changes made to the working tree +file can be discarded to re-create the original conflicted merge result. + +'git restore-files' (-p|--patch) [--from=<tree-ish>] [<pathspec>...]:: + This is similar to the "check out paths to the working tree + from either the index or from a tree-ish" mode described + above, but lets you use the interactive interface to show + the "diff" output and choose which hunks to use in the + result. See below for the description of `--patch` option. + +OPTIONS +------- +-q:: +--quiet:: + Quiet, suppress feedback messages. + +--[no-]progress:: + Progress status is reported on the standard error stream + by default when it is attached to a terminal, unless `--quiet` + is specified. This flag enables progress reporting even if not + attached to a terminal, regardless of `--quiet`. + +-f:: +--force:: + Do not fail upon unmerged entries; instead, unmerged entries + are ignored. + +--ours:: +--theirs:: + Check out stage #2 ('ours') or #3 ('theirs') for unmerged + paths. ++ +Note that during `git rebase` and `git pull --rebase`, 'ours' and +'theirs' may appear swapped; `--ours` gives the version from the +branch the changes are rebased onto, while `--theirs` gives the +version from the branch that holds your work that is being rebased. ++ +This is because `rebase` is used in a workflow that treats the +history at the remote as the shared canonical one, and treats the +work done on the branch you are rebasing as the third-party work to +be integrated, and you are temporarily assuming the role of the +keeper of the canonical history during the rebase. As the keeper of +the canonical history, you need to view the history from the remote +as `ours` (i.e. "our shared canonical history"), while what you did +on your side branch as `theirs` (i.e. "one contributor's work on top +of it"). + +--ignore-skip-worktree-bits:: + In sparse checkout mode, update only entries matched by + <paths> and sparse patterns in + $GIT_DIR/info/sparse-checkout. This option ignores the sparse + patterns and adds back any files in <paths>. + +-m:: +--merge:: + When checking out paths from the index, this option lets you + recreate the conflicted merge in the specified paths. + +--conflict=<style>:: + The same as --merge option above, but changes the way the + conflicting hunks are presented, overriding the + merge.conflictStyle configuration variable. Possible values are + "merge" (default) and "diff3" (in addition to what is shown by + "merge" style, shows the original contents). + +-p:: +--patch:: + Interactively select hunks in the difference between the + <tree-ish> (or the index, if unspecified) and the working + tree. The chosen hunks are then applied in reverse to the + working tree (and if a <tree-ish> was specified, the index). ++ +This means that you can use `git restore-files -p` to selectively +discard edits from your current working tree. See the ``Interactive +Mode'' section of linkgit:git-add[1] to learn how to operate the +`--patch` mode. + +--[no-]recurse-submodules:: + Using --recurse-submodules will update the content of all initialized + submodules according to the commit recorded in the superproject. If + local modifications in a submodule would be overwritten the checkout + will fail unless `-f` is used. If nothing (or --no-recurse-submodules) + is used, the work trees of submodules will not be updated. + Just like linkgit:git-submodule[1], this will detach the + submodules HEAD. + +<tree-ish>:: + Tree to checkout from (when paths are given). If not specified, + the index will be used. + +EXAMPLES +-------- + +. The following sequence checks out the `master` branch, reverts +the `Makefile` to two revisions back, deletes hello.c by +mistake, and gets it back from the index. ++ +------------ +$ git switch-branch master <1> +$ git restore-files --from master~2 Makefile <2> +$ rm -f hello.c +$ git restore-files hello.c <3> +------------ ++ +<1> switch branch +<2> take a file out of another commit +<3> restore hello.c from the index ++ +If you want to check out _all_ C source files out of the index, +you can say ++ +------------ +$ git restore-files '*.c' +------------ ++ +Note the quotes around `*.c`. The file `hello.c` will also be +checked out, even though it is no longer in the working tree, +because the file globbing is used to match entries in the index +(not in the working tree by the shell). ++ +If you have an unfortunate branch that is named `hello.c`, this +step would be confused as an instruction to switch to that branch. +You should instead write: ++ +------------ +$ git restore-files hello.c +------------ + +SEE ALSO +-------- +linkgit:git-checkout[1] + +GIT +--- +Part of the linkgit:git[1] suite diff --git a/Documentation/git-switch-branch.txt b/Documentation/git-switch-branch.txt new file mode 100644 index 0000000000..d5bf5cb37d --- /dev/null +++ b/Documentation/git-switch-branch.txt @@ -0,0 +1,289 @@ +git-switch-branch(1) +==================== + +NAME +---- +git-switch-branch - Switch branches + +SYNOPSIS +-------- +[verse] +'git switch-branch' [-q] [-f] [-m] <branch> +'git switch-branch' [-q] [-f] [-m] --detach [<commit>] +'git switch-branch' [-q] [-f] [-m] [[-b|-B|--orphan] <new_branch>] [<start_point>] + +DESCRIPTION +----------- +Switch to a specified branch and update files in the working tree to +match it. + +'git switch-branch' <branch>:: + To prepare for working on <branch>, switch to it by updating + the index and the files in the working tree. Local + modifications to the files in the working tree are kept, so + that they can be committed to the <branch>. ++ +If <branch> is not found but there does exist a tracking branch in +exactly one remote (call it <remote>) with a matching name, treat as +equivalent to ++ +------------ +$ git switch-branch -b <branch> --track <remote>/<branch> +------------ ++ +If the branch exists in multiple remotes and one of them is named by +the `checkout.defaultRemote` configuration variable, we'll use that +one for the purposes of disambiguation, even if the `<branch>` isn't +unique across all remotes. Set it to +e.g. `checkout.defaultRemote=origin` to always checkout remote +branches from there if `<branch>` is ambiguous but exists on the +'origin' remote. See also `checkout.defaultRemote` in +linkgit:git-config[1]. + +'git switch-branch' -c|-C <new_branch> [<start_point>]:: + + Specifying `-c` causes a new branch to be created as if + linkgit:git-branch[1] were called and then switched to. In + this case you can use the `--track` or `--no-track` options, + which will be passed to 'git branch'. As a convenience, + `--track` without `-c` implies branch creation; see the + description of `--track` below. ++ +If `-C` is given, <new_branch> is created if it doesn't exist; +otherwise, it is reset. This is the transactional equivalent of ++ +------------ +$ git branch -f <branch> [<start_point>] +$ git switch-branch <branch> +------------ ++ +that is to say, the branch is not reset/created unless "git +switch-branch" is successful. + +'git switch-branch' --detach [<commit>]:: + + Prepare to work on a unnamed branch on top of <commit> (see + "DETACHED HEAD" section), and updating the index and the files + in the working tree. Local modifications to the files in the + working tree are kept, so that the resulting working tree will + be the state recorded in the commit plus the local + modifications. ++ +When the <commit> argument is a branch name, the `--detach` option can +be used to detach HEAD at the tip of the branch (`git switch-branch +<branch>` would check out that branch without detaching HEAD). ++ +Omitting <commit> detaches HEAD at the tip of the current branch. + +OPTIONS +------- +-q:: +--quiet:: + Quiet, suppress feedback messages. + +--[no-]progress:: + Progress status is reported on the standard error stream + by default when it is attached to a terminal, unless `--quiet` + is specified. This flag enables progress reporting even if not + attached to a terminal, regardless of `--quiet`. + +-f:: +--force:: + Proceed even if the index or the working tree differs from + HEAD. This is used to throw away local changes. + +-c <new_branch>:: +--create <new_branch>:: + Create a new branch named <new_branch> and start it at + <start_point>; see linkgit:git-branch[1] for details. + +-C <new_branch>:: +--force-create <new_branch>:: + Creates the branch <new_branch> and start it at <start_point>; + if it already exists, then reset it to <start_point>. This is + equivalent to running "git branch" with "-f"; see + linkgit:git-branch[1] for details. + +-t:: +--track:: + When creating a new branch, set up "upstream" configuration. See + "--track" in linkgit:git-branch[1] for details. ++ +If no `-c` option is given, the name of the new branch will be derived +from the remote-tracking branch, by looking at the local part of the +refspec configured for the corresponding remote, and then stripping +the initial part up to the "*". +This would tell us to use "hack" as the local branch when branching +off of "origin/hack" (or "remotes/origin/hack", or even +"refs/remotes/origin/hack"). If the given name has no slash, or the above +guessing results in an empty name, the guessing is aborted. You can +explicitly give a name with `-c` in such a case. + +--no-track:: + Do not set up "upstream" configuration, even if the + branch.autoSetupMerge configuration variable is true. + +-l:: + Create the new branch's reflog; see linkgit:git-branch[1] for + details. + +--detach:: + Rather than checking out a branch to work on it, check out a + commit for inspection and discardable experiments. + This is the default behavior of "git checkout <commit>" when + <commit> is not a branch name. See the "DETACHED HEAD" section + below for details. + +--orphan <new_branch>:: + Create a new 'orphan' branch, named <new_branch>, started from + <start_point> and switch to it. The first commit made on this + new branch will have no parents and it will be the root of a new + history totally disconnected from all the other branches and + commits. ++ +The index and the working tree are adjusted as if you had previously run +"git checkout <start_point>". This allows you to start a new history +that records a set of paths similar to <start_point> by easily running +"git commit -a" to make the root commit. ++ +This can be useful when you want to publish the tree from a commit +without exposing its full history. You might want to do this to publish +an open source branch of a project whose current tree is "clean", but +whose full history contains proprietary or otherwise encumbered bits of +code. ++ +If you want to start a disconnected history that records a set of paths +that is totally different from the one of <start_point>, then you should +clear the index and the working tree right after creating the orphan +branch by running "git rm -rf ." from the top level of the working tree. +Afterwards you will be ready to prepare your new files, repopulating the +working tree, by copying them from elsewhere, extracting a tarball, etc. + +-m:: +--merge:: + If you have local modifications to one or more files that are + different between the current branch and the branch to which + you are switching, the command refuses to switch branches in + order to preserve your modifications in context. However, + with this option, a three-way merge between the current + branch, your working tree contents, and the new branch is + done, and you will be on the new branch. ++ +When a merge conflict happens, the index entries for conflicting +paths are left unmerged, and you need to resolve the conflicts +and mark the resolved paths with `git add` (or `git rm` if the merge +should result in deletion of the path). + +--conflict=<style>:: + The same as --merge option above, but changes the way the + conflicting hunks are presented, overriding the + merge.conflictStyle configuration variable. Possible values are + "merge" (default) and "diff3" (in addition to what is shown by + "merge" style, shows the original contents). + +--ignore-other-worktrees:: + `git switch-branch` refuses when the wanted ref is already + checked out by another worktree. This option makes it check + the ref out anyway. In other words, the ref can be held by + more than one worktree. + +--[no-]recurse-submodules:: + Using --recurse-submodules will update the content of all initialized + submodules according to the commit recorded in the superproject. If + local modifications in a submodule would be overwritten the checkout + will fail unless `-f` is used. If nothing (or --no-recurse-submodules) + is used, the work trees of submodules will not be updated. + Just like linkgit:git-submodule[1], this will detach the + submodules HEAD. + +<branch>:: + Branch to checkout; if it refers to a branch (i.e., a name that, + when prepended with "refs/heads/", is a valid ref), then that + branch is checked out. Otherwise, if it refers to a valid + commit, your HEAD becomes "detached" and you are no longer on + any branch (see below for details). ++ +You can use the `"@{-N}"` syntax to refer to the N-th last +branch/commit checked out using "git checkout" operation. You may +also specify `-` which is synonymous to `"@{-1}`. ++ +As a special case, you may use `"A...B"` as a shortcut for the +merge base of `A` and `B` if there is exactly one merge base. You can +leave out at most one of `A` and `B`, in which case it defaults to `HEAD`. + +<new_branch>:: + Name for the new branch. + +<start_point>:: + The name of a commit at which to start the new branch; see + linkgit:git-branch[1] for details. Defaults to HEAD. + +DETACHED HEAD +------------- +include::detach-head.txt[] + +EXAMPLES +-------- + +. The following sequence checks out the `master` branch. ++ +------------ +$ git switch-branch master +------------ ++ + +. After working in the wrong branch, switching to the correct +branch would be done using: ++ +------------ +$ git switch-branch mytopic +------------ ++ +However, your "wrong" branch and correct "mytopic" branch may +differ in files that you have modified locally, in which case +the above checkout would fail like this: ++ +------------ +$ git switch-branch mytopic +error: You have local changes to 'frotz'; not switching branches. +------------ ++ +You can give the `-m` flag to the command, which would try a +three-way merge: ++ +------------ +$ git switch-branch -m mytopic +Auto-merging frotz +------------ ++ +After this three-way merge, the local modifications are _not_ +registered in your index file, so `git diff` would show you what +changes you made since the tip of the new branch. + +. When a merge conflict happens during switching branches with +the `-m` option, you would see something like this: ++ +------------ +$ git switch-branch -m mytopic +Auto-merging frotz +ERROR: Merge conflict in frotz +fatal: merge program failed +------------ ++ +At this point, `git diff` shows the changes cleanly merged as in +the previous example, as well as the changes in the conflicted +files. Edit and resolve the conflict and mark it resolved with +`git add` as usual: ++ +------------ +$ edit frotz +$ git add frotz +------------ + +SEE ALSO +-------- +linkgit:git-checkout[1] + +GIT +--- +Part of the linkgit:git[1] suite diff --git a/Makefile b/Makefile index 1a44c811aa..f035dbab9e 100644 --- a/Makefile +++ b/Makefile @@ -777,9 +777,11 @@ BUILT_INS += git-format-patch$X BUILT_INS += git-fsck-objects$X BUILT_INS += git-init$X BUILT_INS += git-merge-subtree$X +BUILT_INS += git-restore-files$X BUILT_INS += git-show$X BUILT_INS += git-stage$X BUILT_INS += git-status$X +BUILT_INS += git-switch-branch$X BUILT_INS += git-whatchanged$X # what 'all' will build and 'install' will install in gitexecdir, diff --git a/builtin.h b/builtin.h index 6538932e99..01ed43ea69 100644 --- a/builtin.h +++ b/builtin.h @@ -214,6 +214,7 @@ extern int cmd_remote_fd(int argc, const char **argv, const char *prefix); extern int cmd_repack(int argc, const char **argv, const char *prefix); extern int cmd_rerere(int argc, const char **argv, const char *prefix); extern int cmd_reset(int argc, const char **argv, const char *prefix); +extern int cmd_restore_files(int argc, const char **argv, const char *prefix); extern int cmd_rev_list(int argc, const char **argv, const char *prefix); extern int cmd_rev_parse(int argc, const char **argv, const char *prefix); extern int cmd_revert(int argc, const char **argv, const char *prefix); @@ -227,6 +228,7 @@ extern int cmd_show_index(int argc, const char **argv, const char *prefix); extern int cmd_status(int argc, const char **argv, const char *prefix); extern int cmd_stripspace(int argc, const char **argv, const char *prefix); extern int cmd_submodule__helper(int argc, const char **argv, const char *prefix); +extern int cmd_switch_branch(int argc, const char **argv, const char *prefix); extern int cmd_symbolic_ref(int argc, const char **argv, const char *prefix); extern int cmd_tag(int argc, const char **argv, const char *prefix); extern int cmd_tar_tree(int argc, const char **argv, const char *prefix); diff --git a/builtin/checkout.c b/builtin/checkout.c index 764e1a83a1..7dc0f4d3f3 100644 --- a/builtin/checkout.c +++ b/builtin/checkout.c @@ -33,6 +33,16 @@ static const char * const checkout_usage[] = { NULL, }; +static const char * const switch_branch_usage[] = { + N_("git switch-branch [<options>] [<branch>]"), + NULL, +}; + +static const char * const restore_files_usage[] = { + N_("git restore-files [<options>] [<branch>] -- <file>..."), + NULL, +}; + struct checkout_opts { int patch_mode; int quiet; @@ -1302,31 +1312,23 @@ static struct option *add_checkout_path_options(struct checkout_opts *opts, return newopts; } -int cmd_checkout(int argc, const char **argv, const char *prefix) +static int checkout_main(int argc, const char **argv, const char *prefix, + struct checkout_opts *opts, struct option *options, + const char * const usagestr[]) { - struct checkout_opts real_opts; - struct checkout_opts *opts = &real_opts; struct branch_info new_branch_info; int dwim_remotes_matched = 0; - struct option *options = NULL; - memset(opts, 0, sizeof(*opts)); memset(&new_branch_info, 0, sizeof(new_branch_info)); opts->overwrite_ignore = 1; opts->prefix = prefix; opts->show_progress = -1; - opts->dwim_new_local_branch = 1; git_config(git_checkout_config, opts); opts->track = BRANCH_TRACK_UNSPECIFIED; - options = parse_options_dup(options); - options = add_common_options(opts, options); - options = add_switch_branch_options(opts, options); - options = add_checkout_path_options(opts, options); - - argc = parse_options(argc, argv, prefix, options, checkout_usage, + argc = parse_options(argc, argv, prefix, options, usagestr, PARSE_OPT_KEEP_DASHDASH); if (opts->show_progress < 0) { @@ -1455,3 +1457,61 @@ int cmd_checkout(int argc, const char **argv, const char *prefix) return checkout_branch(opts, &new_branch_info); } } + +int cmd_checkout(int argc, const char **argv, const char *prefix) +{ + struct checkout_opts opts; + struct option *options = NULL; + int ret; + + memset(&opts, 0, sizeof(opts)); + opts.dwim_new_local_branch = 1; + + options = parse_options_dup(options); + options = add_common_options(&opts, options); + options = add_switch_branch_options(&opts, options); + options = add_checkout_path_options(&opts, options); + + ret = checkout_main(argc, argv, prefix, &opts, + options, checkout_usage); + FREE_AND_NULL(options); + return ret; +} + +int cmd_switch_branch(int argc, const char **argv, const char *prefix) +{ + struct checkout_opts opts; + struct option *options = NULL; + int ret; + + memset(&opts, 0, sizeof(opts)); + opts.dwim_new_local_branch = 1; + + options = parse_options_dup(options); + options = add_common_options(&opts, options); + options = add_switch_branch_options(&opts, options); + + ret = checkout_main(argc, argv, prefix, &opts, + options, switch_branch_usage); + FREE_AND_NULL(options); + return ret; +} + +int cmd_restore_files(int argc, const char **argv, const char *prefix) +{ + struct checkout_opts opts; + struct option *options = NULL; + int ret; + + memset(&opts, 0, sizeof(opts)); + opts.dwim_new_local_branch = 1; + + options = parse_options_dup(options); + options = add_common_options(&opts, options); + options = add_checkout_path_options(&opts, options); + + ret = checkout_main(argc, argv, prefix, &opts, + options, restore_files_usage); + FREE_AND_NULL(options); + return ret; +} diff --git a/command-list.txt b/command-list.txt index 3a9af104b5..4638802754 100644 --- a/command-list.txt +++ b/command-list.txt @@ -151,6 +151,7 @@ git-replace ancillarymanipulators complete git-request-pull foreignscminterface complete git-rerere ancillaryinterrogators git-reset mainporcelain worktree +git-restore-files mainporcelain worktree git-revert mainporcelain git-rev-list plumbinginterrogators git-rev-parse plumbinginterrogators @@ -171,6 +172,7 @@ git-status mainporcelain info git-stripspace purehelpers git-submodule mainporcelain git-svn foreignscminterface +git-switch-branch mainporcelain history git-symbolic-ref plumbingmanipulators git-tag mainporcelain history git-unpack-file plumbinginterrogators diff --git a/git.c b/git.c index 2f604a41ea..a2be6c3eb5 100644 --- a/git.c +++ b/git.c @@ -542,6 +542,7 @@ static struct cmd_struct commands[] = { { "replace", cmd_replace, RUN_SETUP }, { "rerere", cmd_rerere, RUN_SETUP }, { "reset", cmd_reset, RUN_SETUP }, + { "restore-files", cmd_restore_files, RUN_SETUP | NEED_WORK_TREE }, { "rev-list", cmd_rev_list, RUN_SETUP | NO_PARSEOPT }, { "rev-parse", cmd_rev_parse, NO_PARSEOPT }, { "revert", cmd_revert, RUN_SETUP | NEED_WORK_TREE }, @@ -557,6 +558,7 @@ static struct cmd_struct commands[] = { { "status", cmd_status, RUN_SETUP | NEED_WORK_TREE }, { "stripspace", cmd_stripspace }, { "submodule--helper", cmd_submodule__helper, RUN_SETUP | SUPPORT_SUPER_PREFIX | NO_PARSEOPT }, + { "switch-branch", cmd_switch_branch, RUN_SETUP | NEED_WORK_TREE }, { "symbolic-ref", cmd_symbolic_ref, RUN_SETUP }, { "tag", cmd_tag, RUN_SETUP | DELAY_PAGER_CONFIG }, { "unpack-file", cmd_unpack_file, RUN_SETUP | NO_PARSEOPT },
"git checkout" doing too many things is a source of confusion for many users (and it even bites old timers sometimes). To rememdy that, the command is now split in two: switch-branch and checkout-files. The good old "git checkout" command is still here and will be until all (or most of users) are sick of it. See the new man pages for the final design of these commands. The actual implementation though is still pretty much the same as "git checkout". Following patches will adjust their behavior to match the man pages. Signed-off-by: Nguyễn Thái Ngọc Duy <pclouds@gmail.com> --- .gitignore | 2 + Documentation/git-checkout.txt | 5 + Documentation/git-restore-files.txt | 167 ++++++++++++++++ Documentation/git-switch-branch.txt | 289 ++++++++++++++++++++++++++++ Makefile | 2 + builtin.h | 2 + builtin/checkout.c | 84 ++++++-- command-list.txt | 2 + git.c | 2 + 9 files changed, 543 insertions(+), 12 deletions(-) create mode 100644 Documentation/git-restore-files.txt create mode 100644 Documentation/git-switch-branch.txt