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.
Example:
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:
cd ..
ls .
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
ls .
You would see html-sam and rs-html-sam folders. Next is to copy over the files and start tracking the untracked files.
cd rs-html-sam
git checkout -b add-untracked-files
cp -rf ../html-sam/ rs-html-sam
git status
git add .
git commit -am "Added new files."
You can push to production.
git push origin add-untracked-files
Better Solution:
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.
cd /path-to-your-project
wget https://gist.githubusercontent.com/b4oshany/fef164455800d3bbf1002cf4012858d1/raw/e5cc2186406cd064fd8f5635a293a2451825343a/git-reinit.sh
mv git-reinit.sh ..
bash ../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
cd GitTraining
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:
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"