Skip to content

Instantly share code, notes, and snippets.

@b4oshany
Last active July 24, 2020 04:01
Show Gist options
  • Save b4oshany/fef164455800d3bbf1002cf4012858d1 to your computer and use it in GitHub Desktop.
Save b4oshany/fef164455800d3bbf1002cf4012858d1 to your computer and use it in GitHub Desktop.
Add untracked files to an new existing local project that already has an existing remote repo
targetBranch=master
# Remove the .git config folder.
rm -rf .git/
# Re-init the Git repo and add repository
git init
git remote add origin [email protected]:JamaicanDevelopers/GitTraining.git
git fetch origin
# Create a new branch with untracked files and then add it.
git checkout -b untrackedfiles && git add .
echo "- Oshane Test" >> CONTRIBUTORS.md
git commit -am "Untracked"
git tag v.untracked untrackedfiles
# Switch to the target branch
git checkout $targetBranch
git checkout -b merged-untracked
# Add the untracked files to the target file
git checkout untrackedfiles .
git commit -am "Update source code with untracked files"
# Clean up
git branch -D untrackedfiles
# Afterwards, run the follonng commands:
echo "Run the following command:"
echo "> git diff $targetBranch"
# Run the following command:
# ./reinit.sh <repo-url> <branch> <repo-alias>
# ./reinit.sh [email protected]:JamaicanDevelopers/GitTraining.git master origin
# The problem this is solving is:
# How to add a git link from remote repo to a new existing local project?
# See https://community.jamaicans.dev/t/how-to-add-a-git-link-from-remote-repo-to-a-new-existing-local-project/115 for more details.
#!/bin/bash
argMsg="Must have at least 2 arguments: <repo-url> <branch> <repo-alias>"
repoAlias="origin"
mergeBranch="merged-untracked"
srcBranch="untrackedfiles"
if [[ -z "$1" || -z "$2" ]]
then
echo $argMsg
exit
fi
repoUrl=$1
targetBranch=$2
if [[ ! -z "$3" ]]
then
repoAlias=$3
fi
echo "$repoUrl $targetBranch $repoAlias"
# Remove the .git config folder.
echo "Should the following command be executed?"
echo "> rm -rf .git/"
read -p "Are you sure? (Y or N)" -n 1 -r
echo # (optional) move to a new line
if [[ $REPLY =~ ^[Yy]$ ]]
then
echo "rm -rf .git/"
rm -rf .git/
fi
# Re-init the Git repo and add repository
git init
git remote add $repoAlias $repoUrl
git fetch origin
# Create a new branch with untracked files and then add it.
git checkout -b $srcBranch && git add .
git commit -am "Untracked"
git tag v.untracked $srcBranch
# Switch to the target branch
git checkout $targetBranch
git checkout -b $mergeBranch
# Add the untracked files to the target file
git checkout $srcBranch .
git commit -am "Update source code with untracked files"
# Clean up
git branch -D $srcBranch
# Afterwards, run the follonng commands:
echo "Run the following command:"
echo "> git diff $targetBranch"
echo "> git push $repoAlias $mergeBranch"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment