hg和git命令对照表

夏景胜
2023-12-01

hg和git命令对照表

来源 https://github.com/sympy/sympy/wiki/Git-hg-rosetta-stone

 

Git hg rosetta stone

 
muxator edited this page on 10 Mar 2017 ·  50 revisions

The sympy git server is at https://github.com/sympy/sympy . The main Sympy repository may be cloned with git clone git://github.com/sympy/sympy.git.

The first and the most important thing is that you should understand that git is different. For example it uses staging area (so called index) for iteratively preparing commits. This and other great and unique features of git make it the preference of many developers, so go read its documentation!

Here is a nice cheatsheet which will probably make your life easier in the beginning:  https://jan-krueger.net/development/git-cheat-sheet-extended-edition

Being said all this, now comes a simplified mapping between hg commands and git commands. Use it with care -- there are some semantic differences ...


If you know how to use hg very well and just looking at how to do the same things in git, this page is right for you. Use it like a dictionary hg -> git. Some equivalent git commands may seem more complex than the corresponding hg counterparts; that's because the natural flow of work in git doesn't map 1:1 to Mercurial. But the point here is that if you are used to some specific workflow in hg, it can be directly translated to git using the table below and it does exactly the same thing as you are expecting it to.

When editing this wiki page, please only add an exact equivalent to some hg command; a full explanation can always be found somewhere else on the net.

Table of Contents

Rosetta Stone

hggit
hg cat -r rev some_filegit show rev:some_file
hg clone http://hg.sympy.org/sympy-git.hggit clone git://git.sympy.org/sympy.git
hg clone -U http://hg.sympy.org/sympy-git.hggit clone --bare git://git.sympy.org/sympy.git
hg diffgit diff HEAD
hg diff -r A -r Bgit diff A^..B
hg statusgit status
hg status -cgit ls-files -t | grep '^H'
hg manifestgit ls-tree -r --name-only --full-tree HEAD
hg parentsgit show --pretty=format:'%P' -s
hg commitgit commit -a
hg recordgit add -p; git commit # or, for a more detailed interface: git add -i; git commit
hg email -r tipgit send-email HEAD^ # or: git format-patch HEAD^ ; git send-email 0001-Add-whitespace.patch
hg viewgitk, git gui
hg help commandgit help command
~/.hgrc~/.gitconfig
.hg/hgrc.git/config
hg pathsgit remote -v
editing paths in .hg/hgrcgit remote add name url # see "git help remote" for more info how to edit paths; alternatively, you can edit them by hand in .git/config too
.hgignore.gitignore
.hg/hgrc [ui] ignore.git/info/exclude
hg addgit add (note, it adds _content_ to index; can work on a hunk-by-hunk basis with -p!)
hg rmgit rm
hg pushgit push
hg pullgit fetch
hg pull -ugit pull
hg addremovegit add -A (or: git add .; git ls-files --deleted xargs git rm)
hg revert -agit reset --hard
hg revert some_filegit checkout some_file
hg purgegit clean -fd
hg purge --allgit clean -fdx
hg strip 2fccd4cgit reset --hard 2fccd4c^ (on a normal repository)
git reset --soft 2fccd4c^ (on a bare repository)
hg forgetgit rm --cached (reference: stackoverflow)
hg exportgit format-patch
hg import --no-commit some.patchgit apply some.patch
hg import some.patchgit am some.patch
hg outgit fetch && git log origin/master..
hg ingit fetch && git log ..origin/master
hg update tipgit checkout HEAD # or this: "git checkout master", or "git merge FETCH_HEAD", depending on what you did before this
hg update -Cgit checkout -f
hg update some.branchgit checkout some.branch # Note that "git branch some.branch" is not the same as this.
hg up --date 2014-01-01git checkout `git rev-list -n 1 --before="2014-01-01" master`
hg qimportstg something (A separate patch manager extension is probably not necessary in git -- normal workflow combined with git rebase -i should cover your needs)
hg qpush(see hg qimport)
hg qpop(see hg qimport)
hg qimport -r tip?
hg qnew -f some.patch?
hg resolve -a -mgit add -u
hg rootgit rev-parse --show-toplevel
hg log -G (old way: hg glog)git log --graph --all --decorate # or: git log --graph --all;
hg verifygit fsck
hg branchesgit branch -a
hg branchgit rev-parse --abbrev-ref HEAD
hg rollbackgit reset HEAD~
hg backoutgit revert

Setup

~/.hgrc:

[ui]
 username = Ondrej Certik <ondrej@certik.cz>

~/.gitconfig:

[user]
     name = Ondrej Certik
     email = ondrej@certik.cz

 [color]
     ui = auto

 [color]
     decorate = short

 [alias]
     ci = commit
     di = diff --color-words
     st = status

     # aliases that match the hg in / out commands
     out      = !git fetch && git log FETCH_HEAD..
     outgoing = !git fetch && git log FETCH_HEAD..
     in       = !git fetch && git log ..FETCH_HEAD
     incoming = !git fetch && git log ..FETCH_HEAD

More Information

One can find some info here:

and at many other pages.

Tips

- use gitk to visualize history (much more capable than "hg vi")
 - use git gui to visually stage/unstage what you are preparing for commit
   to index (it too can work on hunk-by-hunk basis)
 - git stash is your friend
 - git rebase --interactive is your friend too :)
 - windows users: either use cygwin or msysgit:
   https://code.google.com/p/msysgit/
- don't try to project your usual habits - open your mind, and maybe you'll
   discover much more superior workflow. (yes, this needs hard work and RTFM,
   and being ready that FM sometimes differ from what software actually does)
 - Add this
parse_git_branch() {
   git branch 2> /dev/null | sed -e '/^[^*]/d' -e 's/* \(.*\)/(\1)/'
   # __git_ps1 "(%s)"
   # use the second line instead if you have bash autocompletion for git enabled
 }
 PS1="\w\$(parse_git_branch) $ "
to your promptstring to show current branch when in a git-tracked directory.
   (see http://b.lesseverything.com/2008/3/25/got-git-howto-git-and-github)

git -> hg conversion

You can use this script:

#! /bin/bash

work=`mktemp -t -d sym.XXX`
git format-patch -k -p -o $work master..HEAD
# add a new line after the subject line so that Mercurial imports it fine.
sed -i '4a\\' $work/*
cd ~/repos/sympy.hg/
hg import $work/*
rm -r $work

to convert all patches between master..HEAD to mercurial repository sitting at ~/repos/sympy.hg/.

Alternatively, you could use the hg-git Mercurial plugin.

how to checkout remote branch

Start with some repository, for example create a new one from scratch:

$ mkdir sympy
$ cd sympy
$ git init
or clone our official repository:
$ git clone git://git.sympy.org/sympy.git
$ cd sympy
Now let's say you want to checkout some branch from git://github.com/certik/sympy.git. The canonical way is to add it to your remotes:
$ git remote add ondrej git://github.com/certik/sympy.git
Then fetch all branches from there into your remote branches:
$ git fetch ondrej
You can now list them with "git branch -r", or examine them with "git log ondrej/some_branch". Finally, to checkout the mpmath5 branch, do:
$ git checkout -b mpmath5 ondrej/mpmath5

 

================= End

 

转载于:https://www.cnblogs.com/lsgxeva/p/10193435.html

 类似资料: