Working with Repositories in Git – A Beginner’s Guide
What is a Git Repository?
A repository (repo) is a storage location for your project files, including all versions and changes tracked by Git. Repositories allow you to manage, collaborate, and maintain your code history effectively.
Git supports two types of repositories:
✅ Local Repository – Stored on your computer, where you make and track changes.
✅ Remote Repository – Hosted online (e.g., GitHub, GitLab, Bitbucket) for collaboration.
This guide will show you how to create, clone, manage, and interact with Git repositories.
Creating a New Git Repository
To start tracking a project with Git, initialize a repository in an existing folder:
git init
This command creates a hidden .git
folder, where Git stores version history.
To verify the repository has been initialized, run:
git status
If successful, you will see:
On branch main
No commits yet
Cloning an Existing Repository
If you want to work on an existing project, clone the remote repository to your local machine:
git clone <repository-url>
Example:
git clone https://github.com/user/repository.git
This downloads all files, branches, and history to your local computer.
To navigate into the cloned repository:
cd repository-name
Viewing and Managing Repository Files
Check the Status of Your Repository
To see which files are modified, staged, or untracked:
git status
List Tracked and Untracked Files
To view all files in the repository:
git ls-files
Ignoring Files with .gitignore
To exclude files from Git tracking, create a .gitignore
file and list file names or patterns:
/node_modules
*.log
.env
Add and commit the .gitignore
file:
git add .gitignore
git commit -m "Added .gitignore"
Synchronizing with Remote Repositories
To work with remote repositories, you need to connect and sync changes.
Add a Remote Repository
If you initialized a local repository but want to link it to a remote one:
git remote add origin <repository-url>
Verify the remote repository link:
git remote -v
Push Changes to a Remote Repository
To upload commits to the remote repository:
git push
If pushing to a new branch for the first time:
git push --set-upstream origin <branch-name>
Pull Updates from a Remote Repository
To get the latest changes from the remote repository:
git pull
This fetches and merges changes from the remote repository into your local branch.
Fetch Updates Without Merging
To check for changes without applying them:
git fetch
This allows you to review changes before merging them.
Viewing Repository History
See Commit History
To display a list of all past commits:
git log
For a simplified version:
git log --oneline
See Changes Between Commits
To compare differences between commits:
git diff
To compare a specific file:
git diff HEAD~1 <file-name>
Next Steps
Now that you know how to manage repositories, explore more Git topics:
Staging & Committing Changes – Learn how to track and save modifications.
Undoing Mistakes in Git – Fix errors and restore previous versions.
Pushing & Pulling from GitHub & GitLab – Sync code with remote repositories.
Need help? Check the official Git documentation or ask in the comments!