4/15/2023 0 Comments Git switch vs checkout![]() Git branch -set-upstream upstream/branch branch That setting can be overridden by using the -track and -no-track options, and changed later using git branch -set-upstream-to.Īnd git checkout -track origin/branch will do the same as git branch -set-upstream-to): # or, since 1.7.0 This behavior may be changed via the global tosetupmerge configuration flag. When a local branch is started off a remote-tracking branch, Git sets up the branch (specifically the branch.remote and rge configuration entries) so that git pull will appropriately merge from the remote-tracking branch. ![]() create the branch branch (with git branch) and track the remote tracking branch origin/branch.create/reset branch to the point referenced by origin/branch.Git checkout -b branch origin/branch will: faultRemote=origin to always checkout remote branches from there if is ambiguous but exists on the ‘origin’ remote.įirst, some background: Tracking means that a local branch has its upstream set to a remote branch: # git config branch.remote origin If the branch exists in multiple remotes and one of them is named by the faultRemote configuration variable, we’ll use that one for the purposes of disambiguation, even if the isn’t unique across all remotes. Note: with Git 2.23 (Q3 2019), that would use the new command git switch: git switch -c -track / If it did, you would need git checkout -B abranch origin/abranch) (That is, as commented by Sebastian Graf, if the local branch did not exist already. git checkout -track origin/abranch will only create ‘ abranch‘, not a branch with a different name.git checkout -b mybranch origin/abranch will create mybranch and track origin/abranch.The practical difference comes when using a local branch named differently: Read more directly from the makers via the docs.The two commands have the same effect (thanks to Robert Siemer’s answer for pointing it out). For example, git stash pop is actually restoring the changes and deleting the stash in the same command, which can be separated - like how git pull is the combination of git fetch and git merge. Please note that I’m merely covering the main use case of git stash in this post, and deliberately left out the intricate and powerful peripheral options. (For more details on rebase, see squash git commits guide.) This way, the reluctant commit is squashed into the final, single commit. When done working on this branch, I’d usually clean up the commits by squashing them using git rebase -i HEAD~n, where n=number of commits to squash into 1. git commit -m "RC: in middle of adding product API" git checkout feature-branch-2 # do a reluctant commit before switching to another branch git add. Run git log to check what you’d already done, and carry on from there. The benefit of this RC approach is that you don’t have to rely on your own memory to remember that you had already begun work on the branch - it’s already committed. When back on the branch you left behind, if you merged new commits to master, run git rebase master to put your RC on top of the latest commit from master branch and continue working.Checkout to another branch to work on it.Indicate in the commit message that it is a reluctant commit.Commit the changes on the branch you’re about to leave behind.(In fact I used it way before discovering git stash.) The steps are: So in situations where you’re unsure how much time you will be spending working on a different branch in the same repository, it’s probably best to do what I’ve come to call (to myself) a “reluctant commit”.Ī reluctant commit (I use the shorthand “RC” in commit messages to indicate this) is my made-up alternative of git stash. This happened to me a few times, and when I finally remembered that I’d done work and stashed it previously, it’s already too late. ![]() The reason is simple: you might forget that you’d done work there. I found that when I’m unsure how long I’m going to be working on another branch, it’s better to not use git stash. There are times when it’s not wise to use git stash to keep your uncommitted changes before switching branches. Git stash is not always the best solution This can be useful if you’ve accidentally written code on a wrong branch. What that means is if you do git stash in feature-xyz branch, you can run git stash pop in master branch, for example. # restore previously stashed changes git checkout feature-branchĪnother neat fact about git stash is that it is apparently maintained across the all branches in your git repository.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |