Category Archives: Git

Duplicating a Git repository

I was working in a personal Git repository and I wanted to make a duplicate copy of it under a different organisation. The following approach worked well for me:

Open Terminal / Command Prompt and Clone the existing source repo:
git clone --bare https://your_source_repository_url

Make sure you don’t skip the “–bare” portion of the command line.
You will see that a new folder is created from your source repository.


Move inside the folder:
cd your_source_repository

Now we need to push the files from your source repository into your target repository:
git push --mirror https://target_repository_url

Make sure your don’t skip the “–mirror” portion of the command line. Also this command has to be executed from the source repository’s clone location folder.

Fetching colleague’s fork on Github

I am currently using Github and all project members have forked the main repository into private repository. Now the problem is that I need to review my colleagues code committed in his branch inside his forked repository. So to do this I found out a good article that showed me exactly how to do that. I used the following approach.

git remote add my-colleague https://github.xyz.com/my-colleague/project.git

git fetch my-colleague

git checkout -b my-colleague-branch --track my-colleague/branch

After I do this I can see all the work done by my colleague and I can switch back to my branch anytime.

Delete Untracked File Using Git

Sometimes we want to clean out our Git workspace and remove any file which is untracked. A hard reset usually does the job for modified files as shown below:
git reset --hard

But hard reset doesn’t fit in case of untracked files. The following command will do the job:
git clean -d -fx ""

-x means ignored files are also removed as well as files unknown to git.

-d means remove untracked directories in addition to untracked files.

-f is required to force it to run.

Reference:
https://www.kernel.org/pub/software/scm/git/docs/git-clean.html

Configuring Git user name and email

Since I like working using terminal I prefer to do all my operations of git from the command prompt. The first prerequisite to this is to have my name and email configured. This few commands are required to do this simple operation:

To set the name and email:

git config --global user.name "Your Name"
git config --global user.email "Your Email"

To view the name and email which is known to git execute the same commands without any parameter as shown below:

git config --global user.name
git config --global user.email