简体   繁体   English

重新构建Git仓库基础的Bash脚本

[英]Bash script that rebase Git repository

I'm trying to find a way to rebase the repository by using a Bash Script, I got this, but it returned alot a few errors that I don't know how to fix them for example: ./bash.sh: line 3: $'\\r': command not found 我正在尝试找到一种使用Bash脚本对存储库进行基础设置的方法,我明白了,但是它返回了很多我不知道如何解决的错误,例如: ./bash.sh: line 3: $'\\r': command not found

Errors: 错误:

./bash.sh: line 3: $'\r': command not found
./bash.sh: line 6: $'\r': command not found
Updating Repo: /home/teste/ with url:
./bash.sh: line 8: $'\r': command not found
./bash.sh: line 16: syntax error near unexpected token `$'\r''
'/bash.sh: line 16: `(git fetch --all && git stash)

Here is the script that I got 这是我得到的脚本

directory="/home/teste/" ## Where the server is located
original_dir="/root" ## Where we should back after the pull

cd $directory # switch to the git repo
repo_url=$(git config --get remote.origin.url)

echo "Updating Repo: $directory with url: $repo_url"

main_branch="master"
if [ "$repo_url" == "XXXXX" ]; then # if you have a repo where the primary branch isnt master
    $main_branch="trunk"
fi

# update the repo, then stash any local changes
echo -e "\ncalling: git fetch --all && git stash"
(git fetch --all && git stash)
current_branch=$(git rev-parse --abbrev-ref HEAD)

# switch to master/trunk branch and rebase it, then switch back to original branch
if [ $current_branch != $main_branch ]; then
    echo -e "\ncalling: git checkout $main_branch && git rebase && git checkout $current_branch"
    (git checkout $main_branch && git rebase && git checkout $current_branch)
fi

# rebase the original branch and then stash pop back to original state
echo -e "\ncalling: git rebase && git stash pop on branch: $current_branch"
(git rebase && git stash pop )

#switch back to the starting directory
cd $original_dir
echo ""

It's your file encoding, not the scripting itself - windows carriage returns are different to unix format. 这是您的文件编码,而不是脚本本身-Windows回车符不同于UNIX格式。 If you are using a GUI editor, change your line endings to 'unix' or 'win' format depending on your OS. 如果您使用的是GUI编辑器,请根据您的操作系统将行尾更改为“ unix”或“ win”格式。

There is also a tool called dos2unix that you can run against a file to convert line endings to unix format (or visa versa unix2dos). 还有一个名为dos2unix的工具,您可以针对一个文件运行该工具,以将行尾转换为unix格式(反之亦然,unix2dos反之亦然)。

In your .gitattributes configuration you can also set what format you want. 在您的.gitattributes配置中,您还可以设置所需的格式。 This will make sure the code you check out and commit gets treated accordingly in the future: 这样可以确保您签出并提交的代码将来会得到相应处理:

text eol=crlf # unix

OR 要么

text eol=lf # windows

See: https://help.github.com/articles/dealing-with-line-endings/ 参见: https : //help.github.com/articles/dealing-with-line-endings/

See this page for other ways to skin this cat. 参见此页面以其他方式给这只猫蒙皮。 '\\r': command not found - .bashrc / .bash_profile '\\ r':找不到命令-.bashrc / .bash_profile

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM