Celestina Odili
Posted on August 3, 2024
Check out My Repository
Table of Contents
- Installation
- Configuration
- Initializing a Repository
- Checking Status
- Adding Files
- Making Commits
- Pushing Changes
- Pulling Changes
Introduction
Git and GitHub have become essential tools for developers worldwide. Git is a version control system that tracks changes in source code, while GitHub is a web-based platform for hosting Git repositories. Mastering Git and GitHub is crucial for modern software development and devops. This guide will walk you through setting up Git, creating a repository and using the basic commands like commits, pushing and pulling.
Setting Up Git and GitHub
Installation
To get started with Git,
- Click download to download and install.
- Choose your local computer OS. For windows, choose the appropriate for your configuration and install.
Sign up on GitHub to register a GitHub account.
Install visual Studio code
Configuration
After installing Git, initialize your git in git bash and configure your username and email. These details will be associated with your commits.
- On your local computer task bar search for git bash and run as administrator.
Configure Username. Run the command below personalizing the username and press enter.
git config --global user.name "Your GitHub username"
Configure email. Run the command below personalizing the email and press enter.
git config --global user.email "your github email address"
Creating a Repository
A Git repository is where your project files and their revision history are stored.
Create a Directory
- Run this command to create a new directory called gitlab3.
mkdir gitlab3
- Run the command below to change to the new directory created.
cd gitlab3
Initializing a Repository
To initialize a new repository, run the following command
git init
This will initialize empty git repository in the local directory.
Move to Visual studio code (optional)
You can complete all task with git bash but if you do enjoy the GUI feel of visual studio code while working on the terminal, you can navigate to visual studio code by running the following command.
code .
This will launch a new visual studio code interface.
- Create a simple html file called Index.html
- go to the menu, click on the 3 dot select terminal and then new terminal.
Basic Git Commands
Now that you have your code ready, you can carry out any other command.
- On the terminal, run git commands like check git status, git add, git commit, git push and git pull.
Checking Status
git status
Adding Files
Add your file to the staging area.
git add filename
replace _filename _ with the name of the file you want to add. For example
git add index.html
- To add all changes:
git add .
Making Commits
Commits are snapshots of your project at a given point in time. Once your changes are staged with the add command, commit them with a message:
git commit -m "Your commit message"
example:
git commit -m "initial commit"
Pushing Changes
Pushing command sends your commits to a remote repository, like GitHub. Hence, the remote repository has to exist first before a push command can take effect.
Creating Remote Repository in GitHub
- go github.com and sign in
- click on + to add new repository
- Give it a name.
- choose public or private
- Add README file for your documentation
- Click create repository
- go to code and copy the repository url.
- connect the repository to the local machine. use the following command.
git remote add origin repo url
replace repo url with your git hub repository url
For Example,
git remote add origin https://github.com/nnenne/mywebapp.git
- Push changes
git push origin master
- sign in to GitHub when prompted and authorize git ecosystem. This is only applicable when you are pushing for the first time.
Go back to GitHub and notice the file is now in GitHub.
add a readme file on visual studio code. Afterwards do git add, git commit and git push.
Pulling Changes
Pulling fetches and merges changes from the remote repository to your local repository.
- Edit any of the file on GitHub, commit changes
- On your terminal, type the pull command below and notice the changes now reflect.
git pull origin master
Check out My Repository
Posted on August 3, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.