Change current branch and keep local changes
I'm on branch
a. I want to make a commit on branch
b, so that someone who clones branch
b has the same working directory as I have now.
Stashing the current changes does not work because in some cases this results in a conflict.
I'm looking for the equivalent of making a temporary copy of the working directory, calling
git checkout -f b, deleting all files, copying the temporary directory to the project directory and making a commit.
git reset --soft if your friend. If you want the revision after B to be the way you have on the working tree (uncommitted as of now), you can do this:
git checkout --detach # disconnect from A git reset --soft b # set the branch pointer to whatever revision B is pointing to.... Your working tree will be unaffected git add . # add everything to index git commit -m "revision after B that made it look the way I had it where I was working" # if you like everything, move b branch to this revision and push git branch -f b git checkout b git push some-remote b
That should do.
git switch branch without discarding local changes, There are a bunch of different ways depending on how far along you are and which branch(es) you want them on. Let's take a classic mistake: A Git Branch is a separate line of development in a software project. User can create a branch, and keep on committing their changes to this branch without messing the original 'master' branch. Advertisements Usually, each developer working on a code makes his/her changes in a separate branch. Git offers features to merge the branch […]
Stash your changes before switching branch with Git, You need to switch back to your main dev branch. Before that happens, you have to take care of all your current changes. You can either commit if you error: Your local changes to the following files would be overwritten by checkout: file1. txt. Please, commit 1. $ git stash save "changes on new-branch"� The --keep option preserves the uncommitted changes in unrelated files, or aborts if the changes would have to be overwritten. If it aborts, git stash your changes and retry, or you can use the --hard option to lose the changes even from files that didn't change between the commits. Moving to a New Branch¶
Any commit essentially is "a temporary copy of the working directory".
The "changes between" you refer to are in fact produced for you from the snapshots when you
git show <commit> for example.
That why it's a bit difficult to gie a straightforward answer your question. Let's try these two possible paths :
With history rewrite of branch
If you want a commit on branch
b reflecting the exact state on branch
a at this very moment, why not just point directly branch b where it should. Like this :
# get the uncommited changes on the branch git commit -am "Useful message" # point b where a is now git branch -f b a # instead of forcing the branch we could merge with a strategy ours # but it's hard to tell what you need from your description alone # reset a to its previous state git reset --hard HEAD^
Initial state :
C1---C2 <<< a <<< HEAD # with a dirty tree, uncommited changes ? <<< b # (you didn't mention where b was pointing)
C1---C2 <<< a <<< HEAD \ C3 <<< b # where your last (previously uncommited) changes are
As it rewrites history of the branch, it should be considered carefully, and probably ruled out if the branch is shared. Still, it does what you asked for : "someone who clones branch b has the same working directory as I have now".
Without rewriting history of branch
To avoid rewriting the history of branch
b, an alternative is to merge a into b with a strategy which will take everything from
a side, not only conflicting parts. It would go like this :
# we work on a copy of branch a git checkout -b a-copy a # get the uncommited changes on the branch git commit -am "Useful message" # proceed with the merge, taking nothing from b git merge -s ours b # we now reflect the merge on b, and this is a fast-forward git checkout b git merge a-copy # no further need for the working copy of a git branch -D a-copy
a doesn't need to be reset because it didn't move at any point.
After the first commit :
C1---C2 <<< a \ C3 <<< a-copy <<< HEAD o <<< b (anywhere in the tree)
After the first merge :
C1---C2 <<< a \ C3 (same state as a, plus the previously uncommited changes) \ C4 <<< a-copy <<< HEAD (the merge taking only C3 side) / o <<< b (anywhere in the tree)
End state :
C1---C2 <<< a \ C3 (same state as a, plus the previously uncommited changes) \ C4 <<< b <<< HEAD / o
git checkout, The "checkout" command can switch the currently active branch - but it can Thereby, you can reset single files to earlier revisions - while keeping the rest of the the last committed version of a file - effectively undoing any local changes that� 2. Reset all changes in your project files git reset --hard origin/<branch_name> Don’t forget to change <branch_name> to your working branch. For example, if you are working on the master branch, the command will look like this: git reset --hard origin/master . References. git clean command; git reset command
What's the difference between "git fetch" and "git pull"?, goal in mind: to update your current HEAD branch with the latest changes from the remote server. Since "git pull" tries to merge remote changes with your local ones, Use Git's Stash feature to save your local changes temporarily. $ git checkout <branch> Switch to the branch "issue1" by doing the following. $ git checkout issue1 Switched to branch 'issue1' This history tree should look like this at the moment. By passing in the -b option when executing the checkout command, a new branch will be created and you will be switched over thereafter. $ git checkout -b <branch>
git-checkout Documentation, NAME. git-checkout - Switch branches or restore working tree files Local modifications to the files in the working tree are kept, so that they can be committed to the <branch> . Omitting <branch> detaches HEAD at the tip of the current branch. refuses to switch branches in order to preserve your modifications in context. Merge changes in one local branch to another local branch Merge changes in one remote branch to another remote branch When we say incoming or outgoing, we’re taking the perspective of the local branch we’re currently in. Changes committed on the local branch that we want to send out to another branch (usually on the remote repository) are
Chapter 28 Pull, but you have local work, 16 Existing project, GitHub first Your goal: get commit C into your local branch, while retaining the work in commit D or your uncommitted changes. R in the example below) that has also been changed in commit C , you cannot git pull . jenny@2015-mbp ethel $ git stash save Saved working directory and index state � Update your branch with the latest changes from master. When working in a branch, you may want to incorporate the latest changes from the master branch into your branch. There are two approaches you can use: rebase or merge. Rebase takes the changes made in the commits in your current branch and replays them on the history of another branch. Rebasing rewrites the commit history of your current branch.
- Commit your changes on branch a; checkout branch b; git pull origin/a --force; git reset HEAD~1; do your commit; git push --force; checkout branch a again; git reset HEAD~1; and that's it
- I'll also note that except for the way it totally ruins your current index, it's much more efficient to just add everything and commit (using
git update-refto put the commit onto branch
b). You can fix the former problem by using a temporary index, the way
git stashdoes. But that wouldn't illustrate why you can use
git stashto get what you want. :-)
- Can you suggest a way that keeps the history of the branch
b? So that just one commit is added.