Situation 2 - You have a local project that is untracked, but there’s a remote repository of the project that is tracked by git and you want to add your changes:
Simple Solution, but not the best
Honestly, the simplest solution is to clone the repo to a different folder, copy the files to the cloned repo, commit and push.
Lets assume you have a project in your home folder called html-sam and you’re currently in your html-
sam folder. Run the following commands:
You should see your html-sam folder. Now clone the repo using git clone .
Lets call the new folder rs-html-sam
git clone <url-for-html-sam> rs-html-sam
You would see
rs-html-sam folders. Next is to copy over the files and start tracking the untracked files.
git checkout -b add-untracked-files
cp -rf ../html-sam/ rs-html-sam
git add .
git commit -am "Added new files."
You can push to production.
git push origin add-untracked-files
Add the remote repository link to the local untracked project, track the files in a different branch, merge the files with the main branch then compare the changes between the main branch and the new branch.
We wrote a small utility that does this. You can download it from @b4oshany Github Gist repo.
mv git-reinit.sh ..
Please note, git-reinit.sh bash script requires the following arguments:
Lets use the Git training repository as an example.
- Clone the repository:
git clone https://github.com/JamaicanDevelopers/GitTraining.git
- Remove the Git from the repository
rm -rf .git/
- Create some dummy files and git the contributors.md file:
echo "This is a dummy file" >> dummyfile.md
echo "- Another Test" >> CONTRIBUTORS.md
- If you run
git status, it will state that the project isn’t tracked by git. However, we need to add our local changes that no longer have git to track it. Run the following command:
bash ../reinit.sh [email protected]:JamaicanDevelopers/GitTraining.git master origin
git diff master
The output should look file this:
You can read the code follow the code snippet to gain an understanding of what it is doing.
Nevertheless, here’s a modified simply version of the code snippet that was tailored for the Git training repository:
# Remove the .git config folder.
rm -rf .git/
# Re-init the Git repo and add repository
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"