git checkout master path/to/default.aspx.cs

Before doing this, you probably have to : git checkout redesign

So, just git checkout FROM_BRANCH_NAME path/to/file

Answer from Nepomuk Pajonk on Stack Overflow
🌐
Daehnhardt
daehnhardt.com › home › git survival guide › git checkout for overwriting directories from different branches
Git Checkout for overwriting directories from different branches
September 22, 2024 - To overwrite one branch’s directory with the contents of the same directory from another branch, run git checkout <source-branch> -- <path>/ and commit the result. For example, git checkout dev -- scripts/ replaces the scripts/ directory on ...
Discussions

How do I overwrite one branch with another branch?
Why not... make a new branch? More on reddit.com
🌐 r/git
23
42
May 20, 2019
How do I 'overwrite', rather than 'merge', a branch on another branch in Git? - Stack Overflow
1 With git, how to force overwrite a file in one branch, from a file in another branch (bypass merge), and commit? More on stackoverflow.com
🌐 stackoverflow.com
With git, how to force overwrite a file in one branch, from a file in another branch (bypass merge), and commit? - Stack Overflow
I have a single file in a branch called 'production' that I want overwritten by a specific file in another branch called 'sqa'. The nature of the file is that it is not code where there are multiple More on stackoverflow.com
🌐 stackoverflow.com
Git Overwrite file in master with same file from different branch - Stack Overflow
In Git, how can I overwrite a file in my master branch with this same file from a specific branch called inventory. Can I do this to one specific file in my inventory branch? I accidentally merged the wrong version of a file to master, the correct version is found in inventory branch. ... I think the easiest way is to create another ... More on stackoverflow.com
🌐 stackoverflow.com
March 4, 2020
People also ask

How do I overwrite a single directory from another branch without merging the whole branch?

Use git checkout -- . For example, git checkout dev -- scripts/ copies only the scripts/ directory from dev into your current branch, without merging any other files. Commit the result to record the change.

🌐
daehnhardt.com
daehnhardt.com › home › git survival guide › git checkout for overwriting directories from different branches
Git Checkout for overwriting directories from different branches
What is the difference between git checkout and git restore for restoring files?

git checkout moves HEAD, switches branches, and can overwrite paths from another branch. git restore is the newer, narrower command dedicated to restoring file contents from the index or a commit, giving more granular control without touching HEAD.

🌐
daehnhardt.com
daehnhardt.com › home › git survival guide › git checkout for overwriting directories from different branches
Git Checkout for overwriting directories from different branches
Does git checkout overwrite uncommitted changes in the working directory?

Yes. git checkout -- replaces the files at that path with the source-branch versions, discarding any uncommitted local edits there. Commit or stash your work first if you need to keep it.

🌐
daehnhardt.com
daehnhardt.com › home › git survival guide › git checkout for overwriting directories from different branches
Git Checkout for overwriting directories from different branches
🌐
Rip Tutorial
riptutorial.com › overwrite single file in current working directory with the same from another branch
Git Tutorial => Overwrite single file in current working directory...
Create an orphan branch (i.e. branch with no parent commit) ... The checked out file will overwrite not yet commited changes you did in this file.
🌐
Reddit
reddit.com › r/git › how do i overwrite one branch with another branch?
r/git on Reddit: How do I overwrite one branch with another branch?
May 20, 2019 - You could try to use git pull --rebase to apply your changes on top of the new master. ... Why not just do a fresh clone if you're going to make the changes manually anyway? Or are you going to cherry pick them?
🌐
GitHub
gist.github.com › ummahusla › 8ccfdae6fbbe50171d77
Git overwrite branch with another branch · GitHub
git checkout -B master anotherbranch See https://stackoverflow.com/a/29871400/148680
Top answer
1 of 15
335

You can use the 'ours' merge strategy:

$ git checkout staging
$ git merge -s ours email # Merge branches, but use our (=staging) branch head
$ git checkout email
$ git merge staging

The question requires the commits of the email branch to be kept. They must not be made unreachable, which rules out any reset-based approaches or deleting and recreating the branch.

I no longer need the old changes in email branch, yet I don't want to delete them.

To keep commits of both branches reachable, one or more references must exist that point to those commits. You can always keep two references (staging and email), but the question asks to get rid of one of the refs. Therefore, the branches need to be merged somehow.


EDIT 2020-07-30:

I thought a bit more about this question and possible solutions. If you absolutely require the merge parents in the correct order, need to perform this action with a single command line invocation, and don't mind running plumbing commands, you can do the following:

$ git checkout A
$ git merge --ff-only $(git commit-tree -m "Throw away branch 'A'" -p A -p B B^{tree})

This basically acts like the (non-existent) merge -s theirs strategy. You can find the resulting history in the plumbing branch of the demo repository

Not very readable and not as easy to remember compared to the -s ours switch, but it does the job. The resulting tree is again the same as branch B:

$ git rev-parse A^{tree} B^{tree} HEAD^{tree}
3859ea064e85b2291d189e798bfa1bff87f51f3e
0389f8f2a3e560b639d82597a7bc5489a4c96d44
0389f8f2a3e560b639d82597a7bc5489a4c96d44

EDIT 2020-07-29:

There seems to be a lot of confusion as to what the difference between -s ours and -X ours (the latter being equivalent to -s recursive --strategy-option ours) is. Here's a small example to show the two results from using these two methods. I also recommend reading the question and answers of (Git Merging) When to use 'ours' strategy, 'ours' option and 'theirs' option?

First, setup a repository with 2 branches and 3 commits (1 base commit, and 1 commit per branch). You can find the sample repository on GitHub

$ git init
$ echo 'original' | tee file1 file2 file3
$ git commit -m 'initial commit'
$ git branch A
$ git branch B
$ git checkout A
$ echo 'A' > file1
$ git commit -m 'change on branch A' file1
$ git checkout B
$ echo 'B' > file2
$ git commit -m 'change on branch B' file2

Now, let's try the strategy option (doesn't really matter if we use theirs or ours for this explanation):

$ git merge -X ours A
$ cat file*
A
B
original

We end up with a merge of both branches' contents (branch "strategy-option" in the sample repo). Compare that to using the merge strategy (re-init your repository or reset branch, before executing the next steps):

$ git merge -s ours A
$ cat file*
original
B
original

The result is quite different (branch "merge-strategy" in the sample repo). With the strategy option, we get a merge result of both branches, with the strategy we throw away any changes which happened in the other branch.

You will also notice that the commit created by the merge-strategy in fact points to the exact same tree as the latest commit of "our" branch, while the strategy-option created a new, previously-unseen tree:

$ git rev-parse A^{tree} B^{tree} merge-strategy^{tree} strategy-option^{tree}
3859ea064e85b2291d189e798bfa1bff87f51f3e
0389f8f2a3e560b639d82597a7bc5489a4c96d44
0389f8f2a3e560b639d82597a7bc5489a4c96d44
5b09d34a37a183723b409d25268c8cb4d073206e

OP indeed asked for "I no longer need the old changes in […] branch" and "So I just want to dump all the contents of [A] into [B]", which is not possible to do with a strategy option. Using the 'ours' merge strategy is one possibility of many, but likely the easiest (other possibilities include using low level commands of Git such as write-tree and commit-tree).

2 of 15
143

I've seen several answers and that's the only procedure that let me fix that without any conflicts.

If you want all changes from branch_new in branch_old, then:

git checkout branch_new
git merge -s ours branch_old
git checkout branch_old
git merge branch_new

once applied those four commands you can push the branch_old without any problem

🌐
Programming-books
programming-books.io › essential › git › overwrite-single-file-in-current-working-directory-with-the-same-from-another-branch-29f5f5a9f32740d98dd9015153c4da79
Overwrite single file in current working directory with the same from another branch
This command will check out the file file.example (which is located in the directory path/to/) and overwrite any changes you might have made to this file. ... some-branch can be anything tree-ish known to git (see Revision Selection and gitrevisions for more details)
Find elsewhere
🌐
Graphite
graphite.com › guides › git-overwrite-local-remote
How to overwrite a local git local branch with a remote - Graphite
The git reset command is the most direct way to overwrite your local branch. Use the --hard option to reset the index and working tree. Any changes to tracked files in the working tree since the commit are discarded.
🌐
w3tutorials
w3tutorials.net › blog › how-do-i-overwrite-rather-than-merge-a-branch-on-another-branch-in-git
How to Overwrite a Git Branch with Another Branch (Instead of Merging) – Make Them Point to the Same Commit
git fetch origin # Fetch updates from the remote git checkout main # Temporarily switch to main git pull origin main # Pull latest changes (if needed) git checkout feature-x # Switch back to the target branch · Run git reset --hard <source-branch> ...
🌐
Brian Childress
brianchildress.co › git-force-overwrite-of-local-file-with-remote
GIT: Force Overwrite of Local File with Remote File - Brian Childress
For example, you’re working locally and your files just get into a place where you need to reset, maybe only a file or two. Maybe you’ve merged in another branch and there are numerous conflicts and you just want whatever the remote branch has, whatever the reason, here are a few commands ...
🌐
Squash
squash.io › how-to-force-overwrite-during-git-merge
How to Force Overwrite During Git Merge - Squash Labs
October 28, 2023 - The --strategy-option=theirs tells git to take the changes from other_branch and overwrite any conflicting changes in the current branch. It's important to note that this method uses the "theirs" merge strategy, which means that it will always ...
Top answer
1 of 2
99

If you want all changes from master in dev_branch, then:

git switch dev_branch
git reset --hard master

If you have dev_branch pushed to a remote already, you have to do:

git push --force

To force-push the branch to the remote. Warning: This will break the history of the branch for people who cloned it before! Then, other people will have to do a git pull --rebase on the dev_branch to get the changes.


You can also rename the dev branch to something old and then make a new branch from master with the same name:

git branch -m dev_branch old_dev_branch
git branch -m master dev_branch

Or, use the ours strategy — not sure why it wouldn't work for you:

git checkout master
git merge -s ours dev_branch
git checkout dev_branch
git merge master
2 of 2
1

Why do that? You can delete the branch if it isn't needed anymore (But why? Branches cost next to nothing.). Or you can rename it:

git branch -m dev_branch obsolete_dev

Or you could do this to delete it:

git branch -D dev_branch

Now create a new branch off master (assuming you are on it):

git branch dev_branch

See git branch --help for further options (setting up remotes and all that jazz).

If you now have new branches, you'll have to synchronize with any peer repositories.

Best way to avoid hassle: Have an "active" development branch, if it goes stale, abandon it and create a new one. No history lost that way (could prove crucial sometime).

Have e.g. a branch for each major version, develop on branches off those to fix version bugs, master forges ahead. Use cherry-pick and perhaps merges to port fixes to older versions.

🌐
Stack Overflow
stackoverflow.com › questions › 60519855 › git-overwrite-file-in-master-with-same-file-from-different-branch
Git Overwrite file in master with same file from different branch - Stack Overflow
March 4, 2020 - In Git, how can I overwrite a file in my master branch with this same file from a specific branch called inventory. Can I do this to one specific file in my inventory branch? I accidentally merged the wrong version of a file to master, the correct version is found in inventory branch. ... I think the easiest way is to create another branch - may be called fix-file-{xyz} - which branching from master.
🌐
Quora
quora.com › Is-there-a-way-to-completely-replace-files-in-git-without-needing-to-merge-and-resolve-merge-conflicts
Is there a way to completely replace files in git, without needing to merge and resolve merge conflicts? - Quora
Several Git techniques let you replace files entirely (or make one branchs files win) so you avoid manual merge conflict resolution. Choose the method that fits your workflow and whether you want to preserve history. 1) Replace file(s) in a branch with versions from another branch (fast, ...
🌐
freeCodeCamp
freecodecamp.org › news › how-to-override-local-files-with-git-pull
How to Overwrite Local Files with Git Pull
January 19, 2020 - If you feel the need to discard ... reset/overwrite everything with a copy from the remote branch, then you should follow this guide. Important: If you have any local changes, they will be lost. With or without --hard option, any local commits that haven’t been pushed will be lost. If you have any files that are not tracked by Git ...
Top answer
1 of 15
2617
git checkout main                 # first get back to main
git checkout experiment -- app.js # then copy the version of app.js 
                                  # from branch "experiment"

See also Undo working copy modifications of one file in Git.


Update August 2019, Git 2.23

With the new git switch and git restore commands, that would be:

git switch main
git restore --source experiment -- app.js

By default, only the working tree is restored.
If you want to update the index as well (meaning restore the file content, and add it to the index in one command):

git restore --source experiment --staged --worktree -- app.js
# shorter:
git restore -s experiment -SW -- app.js

As Jakub Narębski mentions in the comments:

git show experiment:path/to/app.js > path/to/app.js

works too, except that, as detailed in the SO question "How to retrieve a single file from specific revision in Git?", you need to use the full path from the root directory of the repo.
Hence the path/to/app.js used by Jakub in his example.

As Frosty mentions in the comment:

you will only get the most recent state of app.js

But, for git checkout or git show, you can actually reference any revision you want, as illustrated in the SO question "git checkout revision of a file in git gui":

$ git show $REVISION:$FILENAME
$ git checkout $REVISION -- $FILENAME

would be the same is $FILENAME is a full path of a versioned file.

$REVISION can be as shown in git rev-parse:

experiment@{yesterday}:app.js # app.js as it was yesterday 
experiment^:app.js            # app.js on the first commit parent
experiment@{2}:app.js         # app.js two commits ago

and so on.

schmijos adds in the comments:

you also can do this from a stash:

git checkout stash -- app.js

This is very useful if you're working on two branches and don't want to commit.

2 of 15
684

Everything is much simpler. Use git checkout for that.

Suppose you're on the master branch. To get app.js from the new-feature branch, do:

git checkout new-feature path/to/app.js

// Note that there isn't any leading slash in the path!

This will bring you the contents of the desired file. You can, as always, use part of the SHA-1 hash value instead of the new-feature branch name to get the file as it was in that particular commit.

Note: new-feature needs to be a local branch, not a remote one.