Git에서 브랜치를 별칭으로 지정할 수 있습니까?
대규모로 git을 사용하는 방법을 찾고 있습니다. 나는 채택을 늘리고 마스터 브랜치 트렁크를 호출하여 일을 더 쉽게 만들고 싶었습니다.
이것은 SVN 사용자에게 약간의 편안함을 줄 수 있습니다. 나는 트렁크라는 브랜치를 만들 수 있다는 것을 알고 있지만 그것은 git 규범에서 벗어난 것처럼 보이고 일부 사용자가 혼란스러워 할 수 있습니다.
내 마음의 내용에 대한 태그를 만들고 삭제할 수도 있다는 것을 알고 있지만 해당 태그를 체크 아웃하면 나에게 괜찮지 만 아마도 내가하고 싶은 일이 아닌 비 로컬 브랜치라고 알려줍니다.
나는 총 git newb이지만 릴리스 및 빌드 시스템에서 노련한 전문가입니다.
제가하고 싶은 것은 마스터 트렁크를 호출 할 수있는 것입니다. 명령 별칭을 지정하는 기능이 버전이 지정된 개체의 이름에도 적용됩니까?
git-svn이 존재하고 다른 도구가 있다는 것을 알고 있지만 계층화 된 저장소 시스템의 오버 헤드가 나를 두렵게합니다.
Greg가 제안한대로 마스터 브랜치 트렁크의 이름을 바꾸거나 git 및 svn 사용자 모두가 익숙한 'main'브랜치를 가지도록 마스터 브랜치에 대한 기호 참조 인 트렁크를 만들 수도 있습니다.
git symbolic-ref refs/heads/trunk refs/heads/master
트렁크는 일등 시민이 아닙니다. 체크 아웃 trunk
하고 a git status
를 수행 하면 실제로에있을 master
것이지만 trunk
분기 이름 (로그, 병합 등)을 사용하는 모든 위치 에서 명령을 사용할 수 있습니다 .
Git의 "master"라는 이름에 특별한 것은 없습니다. 단지 관례에 따라 (기본적으로) 그렇게 불립니다. 원하는 경우 "트렁크"라고 부를 수 있습니다.
git branch -m master trunk
이것은 "트렁크"라는 이름이 관례에 의해서만 불려지는 Subversion과 매우 유사합니다. Subversion에서 메인 브랜치를 "마스터"라고 부를 수 있습니다.
이것은 Charles Bailey의 답변에 표시된 기술에 대한 안전 래퍼입니다.
$ git branch-alias <alias> <long-and-unwieldy-branch-name> # create alias
$ git branch-alias <alias> # create alias for current branch
$ git branch # view branches and branch aliases
$ git log <alias>
$ git checkout <alias>
$ git push origin <alias> # pushes the branch, not the alias/reference
$ git branch-alias -d <alias> # delete an alias safely
$ git branch-alias -h # help / usage details
git 버전 2.7.0-2.8.2 (포함)의 버그로 인해 "git branch"가 분기 별칭에 대해 "alias-> branch"대신 "alias-> alias"를 표시했습니다. 해당 버그의 영향을받는 경우 2.8.3 이상으로 업그레이드하는 것이 좋습니다.
#!/bin/sh
# git branch-alias
# Author: Phil S.
# Version 1.13.1
version=1.13.1
# Creates branch aliases, so that you can refer to a long branch name
# by a convenient short alias. This is particularly useful for branch
# names beginning with bug-tracker ID numbers (or similar), where the
# benefits of tab-completion are greatly reduced.
# This is mostly a "do what I mean" wrapper around "git symbolic-ref",
# with numerous safety measures included in order to eliminate the
# (otherwise considerable) risk of trashing a branch if you get your
# arguments wrong.
# Installation:
# Place this script somewhere in your PATH and name it "git-branch-alias"
# and you will be able to invoke it with "git branch-alias" as per the
# following examples. If you have obtained the script from the git
# mailing list, please see the "Mailing list archives" note below.
# Examples:
# git branch-alias <alias> <long-and-unwieldy-branch-name> # create alias
# git branch-alias <alias> # create alias for current branch
# git branch # view branches and branch aliases
# git log <alias>
# git checkout <alias>
# git push origin <alias> # pushes the branch, not the alias/reference
# git branch-alias -d <alias> # delete an alias safely
# git branch-alias -h # help / usage details
# Caveats:
# Although everything else I've tried works seamlessly, I note that
# git merge <alias> will cause the alias name to be mentioned in the
# commit message, rather than the name of the real branch. It would
# be nicer if the branch name appeared.
# Compatibility:
# Originally developed with git version 1.7.12.4
# Also tested with git versions 1.9.0, 2.5.4, 2.6.6, 2.8.3
#
# Related git changes between versions 1.7.12.4 and 2.8.3:
# git v1.8.0.1
# * A symbolic ref refs/heads/SYM was not correctly removed with "git
# branch -d SYM"; the command removed the ref pointed by SYM
# instead.
#
# git v1.8.1
# * "git symbolic-ref" learned the "-d $symref" option to delete the
# named symbolic ref, which is more intuitive way to spell it than
# "update-ref -d --no-deref $symref".
#
# git v2.6.5
# * "git symbolic-ref" forgot to report a failure with its exit status.
#
# I believe this is commit 3e4068ed90fd3c6f24303560113aae6dbb758699:
# > symbolic-ref: propagate error code from create_symref()
# > If create_symref() fails, git-symbolic-ref will still exit with
# > code 0, and our caller has no idea that the command did nothing.
# > This appears to have been broken since the beginning of time
#
# As this affects symref creation only, the sole adverse effect here
# would be an unintended message to the user if symref creation had
# actually failed (but not even a misleading one, on account of our
# reading the reference after its creation, and thus displaying an
# error if it turned out to be invalid).
#
# git v2.8.3
# * A change back in version 2.7 to "git branch" broke display of a
# symbolic ref in a non-standard place in the refs/ hierarchy (we
# expect symbolic refs to appear in refs/remotes/*/HEAD to point at
# the primary branch the remote has, and as .git/HEAD to point at the
# branch we locally checked out).
#
# This caused "git branch" to display "ref -> ref" instead of "ref -> branch"
# for branch aliases. The functionality still works otherwise, but is not
# nearly so convenient to work with when you cannot trivially see what each
# alias points to. This bug affected git versions 2.7.0 - 2.8.2 (inclusive).
# Change log:
# v1.13.1
# Change incorrect uses of git show-ref, introduced by v1.10 (including
# effective regression of v1.08), to use git symbolic-ref instead.
#
# v1.12:
# Fix the option handling for '--', and added it to the help text.
#
# v1.11:
# Minor tidy-ups. Re-posted to git mailing list:
# https://www.mail-archive.com/git%40vger.kernel.org/msg161274.html
#
# v1.10:
# No longer dependent on refs existing as individual files, as they
# may be packed in .git/packed-refs.
#
# v1.09:
# POSIX-compatible option handling and output.
# Documented an issue with "git branch" in git versions 2.7.0 - 2.8.2.
#
# v1.08:
# Remove test git show-ref --verify --heads --quiet "refs/heads/${symref}"
# for asserting that the specified reference was valid before deleting a
# reference, as we need to permit the deletion of references to branches
# which have /already/ been deleted, and this test prevented that.
# n.b. We already had another validation test to fall back on, using
# git symbolic-ref "refs/heads/${symref}"
#
# v1.07:
# Minor tweaks. Posted as feature-request to git mailing list:
# https://www.mail-archive.com/git%40vger.kernel.org/msg49171.html
# Mailing list archives:
# If you are reading this via the git mailing list archives at gmane.org
# then this code will probably be broken by an email obfuscation filter
# which automatically converts the symbol '@' to the string ' <at> '.
# Specifically the shell positional parameter expansion "$@" is changed
# to "$ <at> "), so don't try to use the version from gmane. The copy
# of this message at http://www.mail-archive.com/git%40vger.kernel.org/
# should have the correct code.
command=$(basename $0)
if [ "${command##git-}" != "${command}" ]; then
command="git ${command##git-}"
fi
# Print argument (and newline) to stdout or stderr.
stdout () {
printf %s\\n "$1"
}
stderr () {
printf %s\\n "$1" >&2
}
# Returns the supplied parameters suitably quoted for later evaluation.
quote () {
for param; do
printf %s "${param}Z" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/Z\$/' /"
done
}
# Process option parameters.
parameters=
while [ $# -gt 0 ]; do
case "$1" in
( -- ) {
shift
parameters="${parameters}$(quote "$@")"
break
};;
( -v | --version ) version_help=1; shift;;
( -h | --help ) help=1; shift;;
( -d | --delete ) delete=1; shift;;
( -* ) {
stdout "Invalid option: $1"
stdout
shorthelp=1
shift
};;
( * ) { # non-option parameter
parameters="${parameters}$(quote "$1")"
shift
};;
esac
done
# Process non-option parameters.
eval "set -- ${parameters}"
symref=$1
branch=$2
# If too few or too many parameters were supplied, display shorthelp.
if [ -z "${symref}" ] || [ -n "$3" ]; then
shorthelp=1
fi
# If displaying the version, exit immediately.
if [ -n "${version_help}" ]; then
stdout "${command} version ${version}"
exit 0
fi
# Don't let short help override long help.
if [ -n "${help}" ]; then
shorthelp=
fi
# Include the usage summary in both short and long help.
if [ -n "${help}" ] || [ -n "${shorthelp}" ]; then
cat <<EOF
Usage:
${command} [--] <alias> [<branch>]
${command} (-d | --delete) [--] <alias>
${command} (-v | --version)
EOF
fi
# n.b. Calling "git branch-alias --help" causes git to look for
# a man page for "git-branch-alias", so we shouldn't advertise
# the long option (although we support it if the script is called
# by its real name, rather than via git).
if [ -n "${shorthelp}" ]; then
cat <<EOF
For help, use: ${command} -h
EOF
exit 0
fi
# Detailed help.
if [ -n "${help}" ]; then
cat <<EOF
Creates a symbolic reference <alias> referring to <branch>.
<branch> defaults to the current checked-out branch.
This symbolic reference acts as an alias for <branch>, and can be
used in its place. More specifically, it WILL be dereferenced to
its target in nearly all situations, so for any given command you
should treat every usage of <alias> as if it were actually <branch>.
If either <alias> or <branch> begins with a hyphen, you can use the
'--' option to prevent subsequent arguments being treated as options.
To safely delete a branch alias, always use:
${command} -d <alias>
WARNING: These symbolic references appear in your branch list as:
<alias> -> <branch>
and so you might be tempted to try to delete them like a branch:
git branch -d <alias>
However this can cause problems. In git versions prior to 1.8.0.1
<alias> will be dereferenced and you will instead delete the
branch it refers to (git will allow this even if you currently
have that branch checked out), and the symbolic reference will
still remain (referencing a branch which is no longer available).
In later versions of git the <alias> will be deleted rather than
the branch; however git will still not check to see whether you
currently have <alias> checked out, and will not prevent you
from deleting it in that situation. This will leave your HEAD ref
in an invalid state. Using ${command} -d <alias> resolves
this situation by first switching HEAD to <alias>'s target branch
if HEAD was currently set to <alias>.
EOF
exit 0
fi
# Confirm the CWD is within a git repository.
#cwd=$(git rev-parse --show-toplevel)
git=$(git rev-parse --git-dir)
if [ $? -ne 0 ]; then
exit 1
fi
# Use the current branch by default.
if [ -z "${branch}" ]; then
branch=$(git symbolic-ref -q HEAD)
if [ $? -ne 0 ]; then
stderr "Could not establish current HEAD."
exit 1
fi
fi
# We expect plain branch names, but also accept the fully-qualified
# (refs/heads/NAME) paths needed by git symbolic-ref; so strip that
# refs/heads/ prefix if it is specified.
branch=${branch##refs/heads/}
symref=${symref##refs/heads/}
# Deleting a symref.
if [ -n "${delete}" ]; then
# Verify that it IS a symbolic reference.
if ! git symbolic-ref "refs/heads/${symref}" >/dev/null; then
stderr "Error validating refs/heads/${symref} as symbolic reference."
exit 1
fi
# If we currently have <symref> checked out, deleting it is bad
# (as HEAD would no longer be a valid reference). I believe we do
# need to inspect the file here, as attempting to read the HEAD
# reference via git dereferences it to its target branch, and thus
# we are unable to distinguish between the branch and the symref.
if grep "^ref: refs/heads/${symref}\$" "${git}/HEAD" >/dev/null 2>&1; then
stdout "Cannot delete the currently checked out symbolic reference."
branch=$(git symbolic-ref -q HEAD)
if [ $? -ne 0 ]; then
stderr "Could not establish current HEAD."
exit 1
fi
stdout "Switching HEAD to target branch ${branch}"
# By using git symbolic-ref HEAD to find the target ref
# and setting HEAD to that target, nothing really changes,
# but we can now delete the reference safely.
if ! git symbolic-ref HEAD "${branch}"; then
stderr "Error updating HEAD from ${symref} to ${branch}"
stderr "Aborting."
exit 1
fi
fi
# Delete the reference.
# git 1.8.1+ provides: git symbolic-ref --delete <symref>
# but older versions do not include that option, so we use
# the backwards-compatible command.
stdout "Deleting symbolic reference refs/heads/${symref}"
git update-ref -d --no-deref "refs/heads/${symref}"
exit $?
fi
# Creating a new symbolic reference.
# Error checking. git symbolic-ref doesn't really do any, and will
# happily mess up your branches; particularly if you get the arguments
# the wrong way around (treating it like ln -s is a really bad idea).
if ! git show-ref --verify --heads --quiet "refs/heads/${branch}"; then
stderr "Target branch refs/heads/${branch} does not exist."
exit 1
fi
if target=$(git symbolic-ref -q "refs/heads/${symref}"); then
stderr "Symbolic reference refs/heads/${symref} already exists:"
stderr " ${symref} -> ${target##refs/heads/}"
stderr "To delete it, use: ${command} -d ${symref}"
exit 1
elif git show-ref --verify --heads --quiet "refs/heads/${symref}"; then
stderr "Reference refs/heads/${symref} already exists"
stderr "(and is not a symbolic reference!)"
exit 1
fi
# The parameters are good.
# Generate the reference and display the confirmed result.
if git symbolic-ref "refs/heads/${symref}" "refs/heads/${branch}"; then
target=$(git symbolic-ref "refs/heads/${symref}")
stdout " ${symref} -> ${target##refs/heads/}"
else
stderr "Failed to create branch alias."
exit 1
fi
# EOF
업스트림 기능 요청 : https://www.mail-archive.com/git@vger.kernel.org/msg161274.html
참고 URL : https://stackoverflow.com/questions/549920/is-it-possible-to-alias-a-branch-in-git
'Program Tip' 카테고리의 다른 글
Go에서 2D 슬라이스를 만드는 간결한 방법은 무엇입니까? (0) | 2020.12.08 |
---|---|
문자열 상수와 문자열 리터럴의 차이점은 무엇입니까? (0) | 2020.12.08 |
C # 코드에서 (큰) XML을 구문 분석하는 가장 좋은 방법은 무엇입니까? (0) | 2020.12.08 |
Postgresql의 where 절에서 별칭 열 사용 (0) | 2020.12.08 |
Eclipse 프로젝트에서 경고 / 오류를 생성하는 폴더를 제외하는 방법은 무엇입니까? (0) | 2020.12.08 |