git切换分支保存修改的代码的方法

最近在一个原有的项目上做一次非常大的改版,底层的数据库做了很大的变化,跟现在的版本无法兼容。现在的工作除了开发最新的版本之外还要对原来的版本做例行的维护,修修补补。于是有了在两个分支之间游走切换的问题,最新改版的代码在分支new上,旧版本的代码在分支old上,我在new上开发了一半,忽然有人给了我一个改进的需求,于是我要切换回old去修改代码。在这个场景下,我摸索了三种方法:

及时commit代码

在new分支上把已经开发完成的部分代码commit掉,不push,然后切换到old分支修改代码,做完了commit,所有分支互不影响,这是一个理想的方法。

使用git stash

有时候写了一半的JAVA代码,都还不能编译通过的,就被叫去改另一个分支的bug了。

在new分支上的时候在命令行输入:

git stash
git stash save “修改的信息"

这样以后你的代码就回到自己上一个commit了,直接git stash的话git stash的栈会直接给你一个hash值作为版本的说明,如果用git stash save “修改的信息”,git stash的栈会把你填写的“修改的信息”作为版本的说明。

接下来你回到old分支修改代码完成,你又再回到new分支,输入:

git stash pop
git stash list
git stash apply stash@{0}

就可以回到保存的版本了。git stash pop的作用是将git stash栈中最后一个版本取出来,git stash apply stash@{0}的作用是可以指定栈中的一个版本,通过git stash list可以看到所有的版本信息:

stash@{0}: On order-master-bugfix: 22222
stash@{1}: On order-master-bugfix: 22222

然后你可以选择一个你需要的版本执行:

git stash apply stash@{0}

这时候你搁置的代码就回来了。

原文地址: http://www.tonitech.com/2344.html

这是一个非常常用的场景,我正在一个分支上修改功能,然后遇到一个bug需要解决,我得切换到其他分支来修改这个bug,但是目前的功能还在开发阶段,还不成熟,还不想执行add和commit,执行这两个后就会在历史中有记录,并不想这样做,于是就有了git stash功能,把我当前的修改暂时保存起来,然后回来的时候再取出来继续开发功能.

git stash是针对整个git工程来进行保存的,也就是说区分不了branch.比如我在a分支git stash save "sss"暂存了一个修改,那么我切换到b分支,我使用git stash pop 就能把在a分支保存的"sss"这个修改同步到了b分支上.所以当我们需要在不同的分支上取出不同的分支上保存的修改,那么就用到了git stash list,这个命令可以把在所有分支上暂存的信息显示出来,然后我们通过 git stash apply stash@{0} 来选择恢复哪个暂存,stash@{0}这个会在list中列出来.

而我们使用Android studio就太方便了.

在当前工程的任何一个文件中,点击右键,选择git--> 选择repository ---> 里面会列出stash changes和unstash changes命令,一个是保存修改的命令,一个是恢复修改的命令.

stash changes会让我们给要保存的内容输入一个message,这个和git stash save ""是一样的

而 unstash changes会列表我们之前保存过的list

可以很方便的恢复我们之前的保存的内容.

SourceTree拥有一个精美简洁的界面,大大简化了开发者与代码库之间的 Git 操作方式,这对于那些不熟悉 Git 命令的开发者来说非常实用。 SourceTree拥有完整的 Git 功能: 通过一个简单的用户界面即可使用所有的 Git 命令 通过一次单击,即可管理所有的 Git 库,无论是托管的还是本地的 通过一次单击,即可 进行 commit 、push、pull、merge等操作 一些先进的功能,如补丁处理、rebase、shelve、cherry picking等 可以连接到你托管在Bitbucket、 Stash 、Microsoft TFS或 Git Hub中的代码库 中级 Git 教程 回购有关某些 Git 中级实践的HackBU研讨会,仅比 git add git commit git push和 git status更高一步。 对于Windows用户 下载此应用程序以在Windows中使用 Git 终端: : 想要查询更多的信息, 您可以通过此和此了解有关 git 的更多信息。 git stash . git ignore git 标签 git 结帐 git 还原 Backward compatibility note * The "--base" option of "format-patch" computed the patch-ids for prerequisite patches in an unstable way, which has been updated to compute in a way that is compatible with " git patch-id --stable". * The " git log" command by default behaves as if the --mailmap option was given. UI, Workflows & Features * The " git fast-export/import" pair has been taught to handle commit s with log messages in encoding other than UTF-8 better. * In recent versions of Git , per-worktree refs are exposed in refs/worktrees/<wtname>/ hierarchy, which means that worktree names must be a valid refname component. The code now sanitizes the names given to worktrees, to make sure these refs are well-formed. * " git merge" learned "--quit" option that cleans up the in-progress merge while leaving the working tree and the index still in a mess. * " git format-patch" learns a configuration to set the default for its --notes=<ref> option. * The code to show args with potential typo that cannot be interpreted as a commit -ish has been improved. * " git clone --recurse-submodules" learned to set up the submodules to ignore commit object names recorded in the superproject git link and instead use the commit s that happen to be at the tip of the remote-tracking branches from the get-go, by passing the new "--remote-submodules" option. * The pattern " git diff/grep" use to extract funcname and words boundary for Matlab has been extend to cover Octave, which is more or less equivalent. * " git help git " was hard to discover (well, at least for some people). * The pattern " git diff/grep" use to extract funcname and words boundary for Rust has been add ed. * " git status" can be told a non-standard default value for the "--[no-]ahead-behind" option with a new configuration variable status.aheadBehind. * " git fetch" and " git pull" reports when a fetch results in non-fast-forward updates to let the user notice unusual situation. The commands learned "--no-show-forced-updates" option to disable this safety feature. * Two new commands " git switch" and " git restore" are introduced to split "checking out a branch to work on advancing its history" and "checking out paths out of the index and/or a tree-ish to work on advancing the current history" out of the single " git checkout" command. * " git branch --list" learned to always output the detached HEAD as the first item (when the HEAD is detached, of course), regardless of the locale. * The conditional inclusion mechanism learned to base the choice on the branch the HEAD currently is on. * " git rev-list --objects" learned the "--no-object-names" option to squelch the path to the object that is used as a grouping hint for pack-objects. * A new tag.gpgSign configuration variable turns " git tag -a" into " git tag -s". * " git multi-pack-index" learned expire and repack subcommands. * " git blame" learned to "ignore" commit s in the history, whose effects (as well as their presence) get ignored. * " git cherry-pick/revert" learned a new "--skip" action. * The tips of refs from the alternate object store can be used as starting point for reachability computation now. * Extra blank lines in " git status" output have been reduced. * The commit s in a repository can be described by multiple commit -graph files now, which allows the commit -graph files to be updated incrementally. * " git range-diff" output has been tweaked for easier identification of which part of what file the patch shown is about. Performance, Internal Implementation, Development Support etc. * Update supporting parts of " git rebase" to remove code that should no longer be used. * Developer support to emulate unsatisfied prerequisites in tests to ensure that the remainder of the tests still succeeds when tests with prerequisites are skipped. * " git update-server-info" learned not to rewrite the file with the same contents. * The way of specifying the path to find dynamic libraries at runtime has been simplified. The old default to pass -R/path/to/dir has been replaced with the new default to pass -Wl,-rpath,/path/to/dir, which is the more recent GCC uses. Those who need to build with an old GCC can still use "CC_LD_DYNPATH=-R" * Prepare use of reachability index in topological walker that works on a range (A..B). * A new tutorial targeting specifically aspiring git -core developers has been add ed. * Auto-detect how to tell HP-UX aCC where to use dynamically linked libraries from at runtime. * " git mergetool" and its tests now spawn fewer subprocesses. * Dev support update to help tracing out tests. * Support to build with MSVC has been updated. * " git fetch" that grabs from a group of remotes learned to run the auto-gc only once at the very end. * A handful of Windows build patches have been upstreamed. * The code to read state files used by the sequencer machinery for " git status" has been made more robust against a corrupt or stale state files. * " git for-each-ref" with multiple patterns have been optimized. * The tree-walk API learned to pass an in-core repository instance throughout more codepaths. * When one step in multi step cherry-pick or revert is reset or commit ted, the command line prompt script failed to notice the current status, which has been improved. * Many GIT _TEST_* environment variables control various aspects of how our tests are run, but a few followed "non-empty is true, empty or unset is false" while others followed the usual "there are a few ways to spell true, like yes, on, etc., and also ways to spell false, like no, off, etc." convention. * Adjust the dir-iterator API and apply it to the local clone optimization codepath. * We have been trying out a few language features outside c89; the coding guidelines document did not talk about them and instead had a blanket ban against them. * A test helper has been introduced to optimize preparation of test repositories with many simple commit s, and a handful of test scripts have been updated to use it. Fixes since v2.22 ----------------- * A relative pathname given to " git init --template=<path> <repo>" ought to be relative to the directory " git init" gets invoked in, but it instead was made relative to the repository, which has been corrected. * " git worktree add " used to fail when another worktree connected to the same repository was corrupt, which has been corrected. * The ownership rule for the file descriptor to fast-import remote backend was mixed up, leading to an unrelated file descriptor getting closed, which has been fixed. * A "merge -c" instruction during " git rebase --rebase-merges" should give the user a chance to edit the log message, even when there is otherwise no need to create a new merge and replace the existing one (i.e. fast-forward instead), but did not. Which has been corrected. * Code cleanup and futureproof. * More parameter validation. * " git update-server-info" used to leave stale packfiles in its output, which has been corrected. * The server side support for " git fetch" used to show incorrect value for the HEAD symbolic ref when the namespace feature is in use, which has been corrected. * " git am -i --resolved" segfaulted after trying to see a commit as if it were a tree, which has been corrected. * " git bundle verify" needs to see if prerequisite objects exist in the receiving repository, but the command did not check if we are in a repository upfront, which has been corrected. * " git merge --squash" is designed to update the working tree and the index without creating the commit , and this cannot be countermanded by add ing the "-- commit " option; the command now refuses to work when both options are given. * The data collected by fsmonitor was not properly written back to the on-disk index file, breaking t7519 tests occasionally, which has been corrected. * Update to Unicode 12.1 width table. * The command line to invoke a " git cat-file" command from inside " git p4" was not properly quoted to protect a caret and running a broken command on Windows, which has been corrected. * " git request-pull" learned to warn when the ref we ask them to pull from in the local repository and in the published repository are different. * When creating a partial clone, the object filtering criteria is recorded for the origin of the clone, but this incorrectly used a hardcoded name "origin" to name that remote; it has been corrected to honor the "--origin <name>" option. * " git fetch" into a lazy clone forgot to fetch base objects that are necessary to complete delta in a thin packfile, which has been corrected. * The filter_data used in the list-objects-filter (which manages a lazily sparse clone repository) did not use the dynamic array API correctly---'nr' is supposed to point at one past the last element of the array in use. This has been corrected. * The description about slashes in git ignore patterns (used to indicate things like "anchored to this level only" and "only matches directories") has been revamped. * The URL decoding code has been updated to avoid going past the end of the string while parsing %-<hex>-<hex> sequence. * The list of for-each like macros used by clang-format has been updated. * " git branch --list" learned to show branches that are checked out in other worktrees connected to the same repository prefixed with '+', similar to the way the currently checked out branch is shown with '*' in front. (merge 6e9381469e nb/branch-show-other-worktrees-head later to maint). * Code restructuring during 2.20 period broke fetching tags via "import" based transports. * The commit -graph file is now part of the "files that the runtime may keep open file descriptors on, all of which would need to be closed when done with the object store", and the file descriptor to an existing commit -graph file now is closed before "gc" finalizes a new instance to replace it. * " git checkout -p" needs to selectively apply a patch in reverse, which did not work well. * Code clean-up to avoid signed integer wraparounds during binary search. * " git interpret-trailers" always treated '#' as the comment character, regardless of core.commentChar setting, which has been corrected. * " git stash show 23" used to work, but no more after getting rewritten in C; this regression has been corrected. * " git rebase --abort" used to leave refs/rewritten/ when concluding " git rebase -r", which has been corrected. * An incorrect list of options was cached after command line completion failed (e.g. trying to complete a command that requires a repository outside one), which has been corrected. * The code to parse scaled numbers out of configuration files has been made more robust and also easier to follow. * The codepath to compute delta islands used to spew progress output without giving the callers any way to squelch it, which has been fixed. * Protocol capabilities that go over wire should never be translated, but it was incorrectly marked for translation, which has been corrected. The output of protocol capabilities for debugging has been tweaked a bit. * Use "Erase in Line" CSI sequence that is already used in the editor support to clear cruft in the progress output. * " git submodule foreach" did not protect command line options passed to the command to be run in each submodule correctly, when the "--recursive" option was in use. * The configuration variable rebase.rescheduleFailedExec should be effective only while running an interactive rebase and should not affect anything when running a non-interactive one, which was not the case. This has been corrected. * The " git clone" documentation refers to command line options in its description in the short form; they have been replaced with long forms to make them more recognisable. * Generation of pack bitmaps are now disabled when .keep files exist, as these are mutually exclusive features. (merge 7328482253 ew/repack-with-bitmaps-by-default later to maint). * " git rm" to resolve a conflicted path leaked an internal message "needs merge" before actually removing the path, which was confusing. This has been corrected. * " git stash --keep-index" did not work correctly on paths that have been removed, which has been fixed. (merge b932f6a5e8 tg/ stash -keep-index-with-removed-paths later to maint). * Window 7 update ;-) * A codepath that reads from GPG for signed object verification read past the end of allocated buffer, which has been fixed. * " git clean" silently skipped a path when it cannot lstat() it; now it gives a warning. * " git push --atomic" that goes over the transport-helper (namely, the smart http transport) failed to prevent refs to be pushed when it can locally tell that one of the ref update will fail without having to consult the other end, which has been corrected. * The internal diff machinery can be made to read out of bounds while looking for --function-context line in a corner case, which has been corrected. (merge b777f3fd61 jk/xdiff-clamp-funcname-context-index later to maint). * Other code cleanup, docfix, build fix, etc. (merge fbec05c210 cc/test-oidmap later to maint). (merge 7a06fb038c jk/no-system-includes-in-dot-c later to maint). (merge 81ed2b405c cb/xdiff-no-system-includes-in-dot-c later to maint). (merge d61e6ce1dd sg/fsck-config-in-doc later to maint). 切换 分支 时要保证该 分支 代码全部提交,否则会带到另一个 分支 上面; 切换 分支 时停止tomcat; 本地已经存在该 分支 :Team——>Switch To——>“指定 分支 ” 本地不存在该 分支 :Team——>Switch To——>Remote Tracking——>“指定 分支 ”——>check out——>check out as New Local Branc... 经常情况,我们需要对项目的文件 进行 修改 ,但是有时候就会出现一下子把项目玩崩了,然后根本回不到最开始的版本。如果有前一版本的压缩包还好,但是如果没有,就需要一切都推到重来。 针对于此问题,我们可以使用 Git 进行 版本管理 包括但不限于 对代码 进行 远程 保存 出现问题后,及时重置版本 解决 方法 1.创建一个 Git ee仓库 当然,开始的第一步需要你下载安装 git ,比较简单,这里就不演示了。 详情可见这篇博客 下载安装 Git _qq_22841387的博客 在下载安装完成后,我们需要首先去 Git tee官 git stash 无法暂存新建文件 默认情况下, git stash 命令只暂存 Git 已追踪的文件更改,不会暂存未追踪的文件和 . git ignore 文件中忽略的文件 解决办法:使用 git add 文件名 对新建文件 进行 追踪,再 进行 git stash 操作即可。如: git add club.vue 再 git stash 有的时候项目中需要不停的 切换 分支 ,那我们已经 修改 的代码还 不想 提交,用什么 方法 既不用提交自己的代码成功 切换 分支 呢?下面就介绍下 git stash 的系列用法, stash 在英文意思是隐藏, git stash 的作用也是隐藏未完成的代码,防止它干扰别人或者新 分支 的工作。 SVN从服务器上update文件时,如果文件有冲突,SVN会自动帮你merge,很方便; GIT 如果本地有文件改动未提交,且该文件和服务器最新版本有冲突,pull更新时会提示错误,无法更新。 这个小小的区别,就是造成了产生“ Git Pull Failed”错误的根本原因。 解决办法: Git 编译器,要么先 commit 自己的改动再pull(简单不介绍了),要么使用 Stash Changes后再pull,如下。 1. 当本地改动后, 原文参考:http://www.tonitech.com/2344.html刚才做操作发现一个问题,就是本地有一个old 分支 ,此时本地又new了一个新的 分支 ,那么我在old 分支 上做 修改 切换 到new 分支 上,发现文件是 修改 后的。原本理解是一个 分支 一套代码,那么为什么此时代码是一样的,如果工作中遇到一个 分支 上文件 修改 一半,临时需要去另一个 分支 继续 修改 文件时,需要如何操作呢?一、及时 commit 代码在n...