In HG, is there any command such that i can get the file content of the file from another branch without switching/checkout to the particular branch?
Similar to the other answer, you can use the cat command but in a slightly different way:
hg cat --rev=11204 path/to/file
This will pick the file from revision #11204 which can be any branch.
You may want to add the --output switch lets you use whatever filename and/or location you want:
hg cat --output=C:/new/path/to/file/filename#11204.ext --rev=11204 path/to/file
If you are using TortoiseHG, you can use the "save at revision" command to do this.
Steps:
Run Tortoise Workbench
Find the changeset in history which has the file revision you want (could be in any branch)
Say its changeset #abcd1234
Right click on the specific file in the file list for that changeset
Select "Save at revision..." and tell it where to put the file.
The file would typically be named filename#abcd1234.ext so that you don't mix it up with the current version.
Use the cat subcommand with the revision number of the commit you're looking for.
Here's an example. I create two branches with the contents of the file file indicating which branch it's on. Then I use cat to examine the contents on both branches
% hg init foo
% cd foo
% echo "default branch" > file
% hg add file
% hg ci -m 'default' file
% echo "default 2nd rev" > file
% hg ci -m 'default 2nd rev' file
% hg glog
# changeset: 1:305b2b5ccbd9
| tag: tip
| user: dj
| date: Mon Sep 17 14:11:20 2018 -0400
| summary: default 2nd rev
|
o changeset: 0:6d22c68d8abc
user: dj
date: Mon Sep 17 14:10:56 2018 -0400
summary: default
% hg co -r 0
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
% echo "new branch" > file
% hg branch 'new branch'
marked working directory as branch new branch
(branches are permanent and global, did you want a bookmark?)
% hg ci -m 'new branch'
% hg glog
# changeset: 2:4c8d2181526c
| branch: new branch
| tag: tip
| parent: 0:6d22c68d8abc
| user: dj
| date: Mon Sep 17 14:12:30 2018 -0400
| summary: new branch
|
| o changeset: 1:305b2b5ccbd9
|/ user: dj
| date: Mon Sep 17 14:11:20 2018 -0400
| summary: default 2nd rev
|
o changeset: 0:6d22c68d8abc
user: dj
date: Mon Sep 17 14:10:56 2018 -0400
summary: default
% hg branches
new branch 2:4c8d2181526c
default 1:305b2b5ccbd9
% hg cat -r 2 file
new branch
% hg cat -r 1 file
default 2nd rev
Related
This question already has answers here:
Undo an accidental hg strip?
(3 answers)
Closed 3 years ago.
I have this Mercurial repository where I keep a counter:
$ echo 1 > count
$ hg add count
$ hg com -m 'incrementing to 1'
$ echo 2 > count
$ hg com -m 'incrementing to 2'
So far so good, but then I committed a mistake:
$ hg com -m 'incrementing to 3'
So I use hg strip to revert this last commit:
$ hg strip --keep -r -2
saved backup bundle to /home/adam/sandbox/count/.hg/strip-backup/bda856a578bf-ff2b025f-backup.hg
Unbeknownst to me, I have committed another, bigger mistake! I stripped the two topmost commits, and I just wanted to strip the topmost one:
$ hg log
changeset: 0:7b5533cf962a
tag: tip
user: Adam Victor Nazareth Brandizzi <brandizzi#gmail.com>
date: Wed May 15 08:00:27 2019 -0300
summary: incrementing to 1
How I get my commits back?
NOTE: this is a contrived example of a case were I needed to use hg strip. No need to wasting time pointing out there are alternatives etc.
As I've learned from this message in Mercurial's mailing list, the solution is quite straightforward. Note that when I ran hg strip --keep -r -2 I got this line in the output:
saved backup bundle to /home/adam/sandbox/count/.hg/strip-backup/bda856a578bf-ff2b025f-backup.hg
So, basically, Mercurial made a backup of the original history! Now I just need to pull from it:
$ hg pull /home/adam/sandbox/count/.hg/strip-backup/bda856a578bf-ff2b025f-backup.hg
pulling from /home/adam/sandbox/count/.hg/strip-backup/bda856a578bf-ff2b025f-backup.hg
searching for changes
adding changesets
adding manifests
adding file changes
added 2 changesets with 2 changes to 1 files
(run 'hg update' to get a working copy)
$ hg update
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
My file is back to the original state...
$ cat count
66
...as well as my history:
$ hg log
changeset: 2:fc4d1cc18a4b
tag: tip
user: Adam Victor Nazareth Brandizzi <brandizzi#gmail.com>
date: Wed May 15 08:00:51 2019 -0300
summary: incrementing to 3
changeset: 1:bda856a578bf
user: Adam Victor Nazareth Brandizzi <brandizzi#gmail.com>
date: Wed May 15 08:00:38 2019 -0300
summary: incrementing to 2
changeset: 0:7b5533cf962a
user: Adam Victor Nazareth Brandizzi <brandizzi#gmail.com>
date: Wed May 15 08:00:27 2019 -0300
summary: incrementing to 1
Now I can call the right strip command (hg strip --keep -r -1).
What if we do not have the backup file name anymore?
You may have lost the backup file name (it did happen to me!) but you can find it in $YOUR_REPO/.hg/strip-backup. If you have many backups, just list them sorted by time, descending:
$ ls -tl .hg/strip-backup/
total 4
-rw-rw-r-- 1 adam adam 754 May 15 08:10 bda856a578bf-ff2b025f-backup.hg
-rw-rw-r-- 1 adam adam 754 May 15 08:02 02ac79859dd0-0902f53b-backup.hg
The last one is probably the one you want.
Folks, I need your help!
I tried to push my hg repository to bitbucket for the first time. It didn't work and I thought this was because the project is to huge. Therefore, I wanted to push only one single file.
Since I did "hg add *" before, I wanted to revoke this, and typed "hg remove *" and now it DELETED FILES ON MY COMPUTER. But not all of them, only some...
I need these files back, how do I do it? Please tell me they're not gone, please please please.
Thank you so much for your help!!
This is my command history:
518 hg commit commit_test.txt -m "first commit"
519 hg add commit_test.txt
520 hg commit -m "test"
521 ls -ll
522 ls -la
523 cd .hg/
524 ls
525 ls -la
526 pico hgrc
527 cd ..
528 hg status
529 hg add commit_test.txt~
530 hg add commit_test.txt
531 hg commit -m "blabla"
532 hg push
533 hg add commit_test.txt
534 hg commit -m "maaan"
535 hg status
536 hg remove *
537 hg status
538 hg commit -m "bla"
539 ls
540 hg add test.txt
541 hg commit -m "test"
542 hg push
543 hg revert
544 hg revert --all
545 history
546 hg add *
547 hg add -r *
548 hg help add
549 hg add -S *
550 cd gui_relabel/
551 hg add *
552 cd images/
553 hg add *
554 cd ..
555 hg revert all
556 hg revert *
557 history
Here is what hg log gives me:
$ hg log
changeset: 6:4726f671ae96
tag: tip
user: KG <...#gmail.com>
date: Wed Dec 04 12:21:30 2013 +0100
summary: test
changeset: 5:55b3158def38
user: KG <...#gmail.com>
date: Wed Dec 04 12:17:19 2013 +0100
summary: bla
changeset: 4:ae0dd836586d
user: KG <...#gmail.com>
date: Wed Dec 04 12:14:50 2013 +0100
summary: blabla
changeset: 3:0249fdc26fa7
user: KG <...#gmail.com>
date: Wed Dec 04 12:13:59 2013 +0100
summary: test
changeset: 2:40bdcf4d2104
user: KG <...#gmail.com>
date: Wed Dec 04 12:12:37 2013 +0100
summary: first commit test
changeset: 1:f9e20020ca1d
user: KG <...#gmail.com>
date: Sun Nov 10 14:54:46 2013 +0100
summary: first commit
changeset: 0:7a8edcee06ff
user: KG <...#gmail.com>
date: Mon Nov 04 20:36:41 2013 +0100
summary: blabla
Your files are not gone. Mercurial does not throw away data, that is one of the first rules of the system.
To explain what hg remove did to your files, let us look at the cases one by one. A file in your working copy can be in one out of a handful different states: it can be modified, added, removed, missing, unknown, ignored, or clean.
I've prepared a working copy that looks like this:
$ hg status --all
M a
A b
R c
! d
? e
I f
C g
The files a to g are in the seven states I mentioned above. We can now talk about what happens when you try to remove these files:
A modified file (a) is not removed:
$ hg remove a
not removing a: file is modified (use -f to force removal)
An added file (b) is also not removed:
$ hg remove b
not removing b: file has been marked for add (use forget to undo)
A removed file (c) is no longer present in the working copy, so nothing more happens.
A missing file (d) is no longer present in the working copy, so nothing more happens.
An untracked file (e) is not removed:
$ hg remove e
not removing e: file is untracked
An ignored file (f) is not removed (since it is not tracked):
$ hg remove f
not removing f: file is untracked
A clean file (g) is removed.
The only type of file that hg remove will actually remove from your working copy is a clean file. A clean file is already committed, so the content is safely stored in the repository. You can get it back with hg revert:
$ hg revert g
$ hg status --all
M a
A b
R c
! d
? e
I f
C g
The file g is back with the same content as it had before you removed it.
It looks like you could run
hg backout 5
You can read more about backout if you like
When using mercurial, when working on a large change, I will do several commits (as safe points, or share my progress with coworkers).
Whe I am done, I want to push my changes into the main repository. Before I do that, though, I will want to do a "hg pull -u && hg merge"to get changes that occured since my last pull, and review my changes.
Because I have several changesets, running hg diff on each of those is not a good solution.
I tried running hg diff -c"outgoing() and not merge()" to only show the diff for my outgoing changesets (without merge ones, as this tends to only add noise which I want to avoid).
However, it is still not ok for me, as mercurial seems to skip some information, and I do not understand on what basis. Please refer to the scenario below:
full scenario:
# clean working directory
rm -fr base clone
#prepare reference repo
mkdir base; cd base
hg init
touch 1.txt
echo " 1\n2" >>1.txt
hg add 1.txt
touch 2.txt
hg add 2.txt
hg commit -m"initial commit"
cd ..
hg clone base clone
cd base
#update ref repo.
echo "foo" >> 1.txt
hg commit -m"update file on remote repository"
#make changes locally
cd ../clone
echo "foo" >> 2.txt
hg commit -m "update initial file"
#now we are ready to push
hg pull -u && hg merge
hg commit -m"merge"
#actually...
sed -i -e 's/foo/bar/' 2.txt
hg commit -m"changed my mind"
hg out
changeset: 1:fce14ac089b2
date: Thu Nov 14 11:17:08 2013 +0100
summary: update initial file
changeset: 3:1967c50242c0
parent: 1:fce14ac089b2
parent: 2:2cdaf75afc6d
date: Thu Nov 14 11:17:10 2013 +0100
summary: merge
changeset: 4:120a0b8a0ede
tag: tip
date: Thu Nov 14 11:18:54 2013 +0100
summary: changed my mind
#try to display my changes
hg diff -c"outgoing() and not merge()"
--- a/2.txt Thu Nov 14 11:17:10 2013 +0100
+++ b/2.txt Thu Nov 14 11:18:54 2013 +0100
## -1,1 +1,1 ##
-foo
+bar
#only one change here!
#but
hg log -r"outgoing() and not merge()"
changeset: 1:fce14ac089b2
date: Thu Nov 14 11:17:08 2013 +0100
summary: update initial file
changeset: 4:120a0b8a0ede
tag: tip
date: Thu Nov 14 11:18:54 2013 +0100
summary: changed my mind
#is ok
So, could anyone please explain to me why mercurial skips changeset fce14ac089b2 entirely? What I expect is a consolidated output, i.e something like:
--- a/2.txt
+++ b/2.txt
## -1,1 +1,1 ##
+bar
Thanks a lot in advance.
I think the problem is a misunderstanding of what hg diff -c aka hg diff --change does. It takes a single revision id and shows you the change made by that one revision relative to its left parent.
You're calling:
hg diff -c"outgoing() and not merge()"
which yields multiple revisions (it's a revision set), but only one of them is being used. You're essentially seeing the output of:
hg diff --change 120a0b8a0ede
If what you're trying to do is see all of your outgoing changes lumped together in one diff you don't want --change/-c.
If you think about it the revset outgoing() could contain many changeset from many branches so smooshing them all into one diff isn't necessarily possible at all.
If the view you're trying to get is "how does the most recent head on branch default locally compare to the most recent head on default remotely" you need to know the node id of the most recent head on default, and then locally you'd just run:
hg diff node_id_of_most_recent_head_on_default_at_remote_site
This isn't guaranteed, as I expect that there's some way to have a change graph where the last public ancestor isn't the change-set you're after, but...
> hg diff -r "last(ancestors(.) and public())"
diff -r 5579be654db4 2.txt
--- a/2.txt Sat Nov 16 11:03:06 2013 +0000
+++ b/2.txt Sat Nov 16 11:29:34 2013 +0000
## -0,0 +1,1 ##
+bar
I forgot to tag and older version of my files with a release tag. The older version is at r13, latest is about r65. I cloned the latest repository to a new directory, did an "hg update -r13" to get the older code I wanted to tag, then did the tag command, but got the message:
abort: not at a branch head (use -f to force)
Is it safe to use the -f option in this situation?
I guess you can still do the tagging right in the repo without updating yourself to a particular revision.
hg tag -r 13 tagname
See the details at Mercurial wiki.
I tried testing it :
temp $ hg init .
temp $ touch a.txt
temp $ hg add a.txt
temp $ hg commit -m "added a"
temp $ hg status
temp $ echo "sdwwdd" >> a.txt
temp $ hg commit -m "modified a"
temp $ echo "\neddwedd" >> a.txt
temp $ hg commit -m "modified a again"
temp $ hg log
changeset: 2:ef40a402fdab
tag: tip
user: "xxxx"
date: Fri Dec 23 16:51:48 2011 -0800
summary: modified a again
changeset: 1:d630dc3e2e3a
user: "xxxx"
date: Fri Dec 23 16:51:31 2011 -0800
summary: modified a
changeset: 0:7c9917f24515
user: "xxxx"
date: Fri Dec 23 16:51:04 2011 -0800
summary: added a
Output:
temp $ hg tag -r 1 a.txt a_1
temp $ hg tags
tip 3:e3157256098f
a_1 1:d630dc3e2e3a
a.txt 1:d630dc3e2e3a
temp $ hg tag -r 1 all_1
temp $ hg tags
tip 4:a643971911d8
all_1 1:d630dc3e2e3a
a_1 1:d630dc3e2e3a
a.txt 1:d630dc3e2e3a
Let's say I have this:
hg init
touch a
hg add a
hg commit -m a
touch b
hg add b
hg commit -m b
hg up -r 0
touch c
hg add c
hg commit -m c
Now I will have multiple heads, because of the last commit. If, for example I want to keep the last head, the one created by commit c ( effectively discarding b, and all other commits made after the first ) , how could I do it? I played a little with mq's strip command, and I'm able to achieve what I want, but I'd like to know if there's another way.
Yes, there is another way. From your example above, the output of hg glog looks a bit like this:
# changeset: 2:925573c7103c
| tag: tip
| parent: 0:4fe26dfe856d
| user: Joel B Fant
| date: Thu Jul 28 23:20:45 2011 -0400
| summary: c
|
| o changeset: 1:9dc928176506
|/ user: Joel B Fant
| date: Thu Jul 28 23:20:24 2011 -0400
| summary: b
|
o changeset: 0:4fe26dfe856d
user: Joel B Fant
date: Thu Jul 28 23:20:12 2011 -0400
summary: a
If you clone test but specify a revision, it will only clone that revision and its ancestors. So if your repo's directory is TwoHeadsMightNotBeBetter, you can go to its parent directory and issue:
hg clone TwoHeadsMightNotBeBetter OneHeadIsFine -r 925573c7103c
I specified changeset id in that, but you could also use -r 2 instead, or even -r tip since it is currently the tip of that repo. Now when you go into the new clone and do hg glog, you have only one head:
# changeset: 1:925573c7103c
| tag: tip
| user: Joel B Fant
| date: Thu Jul 28 23:20:45 2011 -0400
| summary: c
|
o changeset: 0:4fe26dfe856d
user: Joel B Fant
date: Thu Jul 28 23:20:12 2011 -0400
summary: a
If you had 3 heads and wanted to keep 2, it would be a little different. You'd have to clone one of them with -r and the also specify -r on hg pull to pull specific branches.
You can also perform a dummy merge of the changeset containing b (changeset 1 in this case):
$ hg --config ui.merge=internal:fail merge 1
resolving manifests
getting b
1 files updated, 0 files merged, 0 files removed, 0 files unresolved
(branch merge, don't forget to commit)
$ hg revert --all --no-backup --rev .
removing b
$ hg status
R b
$ hg commit -m "Dummy merge -- eliminate b"
committed changeset 3:c163151f19df
$ ls
a c
Note that in this case you haven't rewritten any history or created any new repos -- particularly important if b had already been pushed to a centralized repo and pulled by others.