Cloning Git Repositories

Git can interact with remote repositories. One source of remote repositories is GitHub. It can also be a self-hosted solution like Gitea. These notes are about cloning remote repositories.

You get a copy of a remote repository by cloning it.

git clone <URL OF REPOSITORY>
git clone https://github.com/leachim6/hello-world.git

The URL of the repository can be either a link to a hosting service or a link to a repository somewhere on our network. If we clone the repository above and look in the .git/objects folder we will see no objects. The reason is GitHub has optimized the repository and the data is stored in packed files in the pack directory. Let’s unpack them. Move the .pack file to the objects directory. We must move, simply copying will show the files as existing and they won’t be extracted, then unpack:

cd .git/objects
mv pack/*.pack .
cat <FILENAME>.pack | git unpack-objects

Now we see our familiar directory structure. We can remove the pack file now. This step, while interesting, is not necessary. Files will be unpacked as needed by git.

We manage our cloned repository by fetching, pulling from, and pushing to the remote repository. Fetch ahem fetches the latest remote code and put it in the repository but not the staging area or working directory. You can manually merge the changes. Pull also fetches the latest remote code and also merges it into the staging area and working directory. It may require manual intervention if there are merge conflicts. Push goes the other way and sends your latest changes to the remote server. It also may require manual intervention. It is recommended to do a pull before pushing if there is more than one contributor.

git fetch Fetches latest upstream changes
git pull Fetches latest upstream changes and merges them into the working directory
git push Send the latest changes upstream
git push -u Send the latest changes upstream, creating the branch

 

It is more usual to use pull than fetch.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.