Table of Contents
You want to move content from one git repo to another in the same cloud server while preserving the history. Better yet, you want to transfer a portion of the repo from server A to server B.
Either way, you are about to find a solution in this tutorial. Let us start with the requirements.
5 git commands to move content from one git repo to another
1. git clone
Following command creates a copy of a repository's online content on your local machine.
git clone <remote URL>
2. git remote
Git remote is a command to control the link between the local repository and its copy in the cloud server. The remote, also called the upstream, is a customizable URL. It mainly references a name (e.g., origin) and a branch (e.g., main).
You can set,
git remote add <name> <URL>
remove
git remote remove <name>
# or use the short form, rm
git remote rm <name>
OR
list the remote
git remote -v
# or peek directly into .git subdirectory
ls .git/logs/refs/remotes
3. git reset
The reset command plays a critical role as you move content from one git repo. It discards what you no longer need, creating a space for incoming content. It exists in three forms: mixed, soft, and hard.
The soft reset changes the HEAD's position without deleting the files. The mixed reset unstages a file. Lastly, git reset hard, which we will apply in this tutorial, deletes commit objects and discard the affected files from the filesystem.
4. git mv
Although there are many ways to move a file in git, we will stick to the mv command and git filter-branch. Git mv command moves or renames a file.
git mv <old location> <target destination>
You can force a move using the -f
option.
git mv -f <old location> <target destination>
5. git filter-branch
The git filter-branch command rewrites git revision history, rewriting the branches depending on the supplied option. The options can change each tree and its information. For instance, the --subdirectory-filter
option, that we will use in this tutorial,
git filter-branch --subdirectory-filter <target subdirectory> -- --all
only acts on the specified subdirectory. It enables us to create multiple repositories from the main repository.
In five steps, let us use the above commands to move content from one git repo to another.
Set up a lab to move content from one git repo to another
I am creating a repository on GitLab called move_content
.
I copy its URL and clone it locally.
Navigate into it and create two files and two directories with a file each.
cd move_content touch file1 file2 mkdir js py cd js touch main.js cat >> main.js console.log("We want to move content from one git repo to another") cd .. cd py touch calc.py cat >> calc.py num1 = int(input("Enter num1: ")) operator = input("Operator: ") num2 = int(input("Enter num2: ")) def calc(): result = 0 if operator == "+": result = num1 + num2 elif operator == "-": result = num1 - num2 elif operator == "*": result = num1 * num2 elif operator == "/": result = num1 / num2 else: print("Unknown operator") exit() return result print(f"{num1} {operator} {num2} = {calc()}") cd ..
Stage, commit, and push the changes.
git add . git commit -m "Set up the project to move content from one git repo to another" git push
That is all we need for repo A. Let us use it to move content from the py directory to repo B on GitHub: migrate_repo
.
As you can see from the above screenshot, repo B lacks the py directory we want to move into it. Let us do it as follows.
Move content from one git repo to another in 5 steps
Step-1: Clone repo A and remove its upstream
We have cloned repo A from GitLab. Let us check and remove its upstream.
git remote -v
The current upstream is the origin
. Remove it to avoid accidentally overwriting it. Then, confirm its deletion.
git remote rm origin git remote -v
Step-2: Dissect the target portion
Let us chop off anything we do not need from repo A, except the py folder we are interested in. We can do that using the filter-branch command we learned earlier.
git filter-branch --subdirectory-filter py -- --all
In case we have remnants of unwanted data, we can delete them using the following commands:
git reset --hard git gc --aggressive git prune git clean -fd
The reset command checks for remnants of the unneeded objects and discards them. The garbage collection (gc) command cleans any mess made while dissecting the py subdirectory from repo A.
The prune command looks for unreachable git objects whose parents we have discarded and removes them from the filesystem. Finally, git clean with the -f
and -d
options checks for untracked files and folders and removes them from the working tree.
Step-3: Move dissected portion into a new directory
Let us make a directory with the -p
option.
mkdir -p Python
The -p
option informs the terminal to make parent directories if needed.
Now we shall loop through the lines in the .py
file.
for f in *.py; do
And move them to the new directory
> git mv $f Python
until the transaction is over.
> done
Now that we have moved the target content into the new directory, let us commit the changes.
git commit -m "Transfer the dissected content into the new directory"
And navigate to repo B to move content from one git repo to another.
cd ..
Step-4: Clone repo B and create for it a remote to repo A
Head over to the repo you want to transfer content into and clone it. I am cloning the GitHub repository called migrate_repo
I showed you in the setup section.
cd
into it and create a remote to repo A and confirm the new remote.
cd migrate_repo git remote add remote_to_A ../move_content
You can replace remote_to_A
with any word.
Step-5: Fetch updates and push to repo B
Pull all changes and history from the remote's (we created in step~4) main
branch, accommodating unrelated histories.
git pull remote_to_A main --allow-unrelated-histories
Our default text editor opens up, asking for a merge message.
Give it a commit message to complete the transaction.
We can then delete the temporary upstream because it has served its purpose: move content from one repo to another.
git remote rm remote_to_A
And push the changes to repo B's remote.
git push
We can then confirm the completed transaction on GitHub.
We have moved the py directory from repo A (on GitLab) to repo B (on GitLab) without losing the folder's history.
Summary
It is easy to move content from one git repo to another by following straightforward steps. It starts by understanding the key commands needed in the process. You then dive into a step-by-step transfer of the target content, ensuring you preserve the history.
As explained in this tutorial, you can move content between the same server repositories or repositories of different code tracking servers.