Technical blog on Git and GitHub; how to set up git, creating a repository, making commits, pushing, pulling etc
anthony AMAJOH
Posted on August 4, 2024
GIT
Git is a DevOps tool used for source code management. It is a distributed version control system. It tracks changes in source code during software development. Git allows for multiple developers to collaborate on a project.
GITHUB
Github is a web-based platform for version control using Git. It provides hosting for software development and version control. It facilitates collaboration among developers.
DIFFERENCE BETWEEN GIT AND GITHUB
Git is a version control system that lets you manage and keep track of your source code history. GitHub is a cloud-based hosting service that lets you manage Git repositories.
GITHUB FEATURES
Some features of Github include but not limited to the following
a. Repository hosting
b. Pull requests for code reviews
c. Issues for bug tracking and feature requests. You can state issues and track actions
d. Github actions for CI/CD
KEY CONCEPTS IN GIT.
i. Repository (Repo)
A Repository is a database containing all project files and their revision history.
ii. Commit
Commit refers to changes made to the repository. Each commit has a unique ID (hash #) with a detailed message explaining what the commit is about
iii. Branch
A parallel version of the repository. You can have more than one branch. They help you create another environment when you want to add a new feature or fix a bug. It allows for separate development paths
iv. Merge
Combining changes from different branches.
v. Clone
Copying a repository to your local environment to work on.
SETTING UP GIT
https://git-scm.com/downloads
https://github.com/signup
1a. Download and install Git from the link below
https://git-scm.com/downloads
b. Sign up on Github using the below link
https://github.com/signup
- Configuration After downloading and installing Git. Go to the windows search bar at the bottom left of your system and type in "gitbash". The gitbash terminal will pop up
The first thing we do is to configure the git
a. Set your username.
To set your username, enter the following commands in the gitbash terminal. Then press enter
git config --global user.name "your github username" and press enter
b. Set your email
To set your email, enter the commands below
git config --global user.email "Your registered email with github" and press enter
BASIC GIT COMMANDS
i. Checking Status
git status
This command shows the status of changes made. It shows the added files, and the ones that are yet to be added.
ii. Adding Changes
a. git add (file)
To stage changes for a particlar file
b. git add .
To add all the files that you have made changes to. To stage all changes.
iii. Committing Changes
git commit -m "commit message"
iv. Viewing History
git log
View commit history
v. Branching and Merging
a. Create a new branch
git branch (branch-name)
b. Switching Branches
git checkout (branch-name)
Switch to a specified branch
c. Merging Branches
git merge (branch-name)
Merge changes from the specified from the specified branch into the current branch
WORKING WITH REMOTE REPOSITORIES
Remote repositories are versions of your project that are hosted on the Internet or network somewhere.
Git commands when working with a remote Repository.
i. Adding a Remote Repository
git remote add origin (repository URL)
ii. Pushing Changes
git push origin (branch-name)
Push changes to the remote repository
iii. Pulling Changes
git pull origin (branch-name)
Fetch and merge changes from the remote repository
iv. Fetching Changes
git fetch origin
Fetch changes from the remote repository without merging
CREATING A REPOSITORY, MAKING A COMMIT, PUSHING, PULLING ETC.
Since the user.name and email have been configured already on Git. To create a folder; we enter the command below in gitbash terminal
mkdir GitLaab
This will create project/folder.
Next, you run the command below
cd GitLaab
To change directory. This will take you to the directory you created.
To initialize a new Repository; you enter the following command
git init
The next command will open the project in Visual Studio Code.
code .
This command opens the created folder in Visual Studio Code
For the project, we will try to push a simple index.html file to github.
On Visual Studio Code click on the project folder to open the folder. Then, you open a new file by clicking on the + New file icon beside the project folder and name the newly created file. Since we are working with an html file, we will name it index.html.
The next step is to import and paste the html file in the Newly created file and save.
Next, we open Terminal in VSC by clicking on Terminal and selecting New Terminal.
To view the status, you enter the below command
git status
Next, you run the following command to add all the files you have
git add .
You can run (git status) again to what information you have
You can observe that on running the command; the index file changed color from red to green. This shows that the file has been added.
The next stage is to commit by running the next command.
git commit -m "Adding index.html"
The next thing is to go to Github.com and create a Repository; then, connect the repository to VSC.
To do this, click on the drop down beside the + at the top right of the page; select New Repository.
Give the Repository a name. The Repository can either be Public or Private.
Tick add a README file.This is for documentation
Click 'create Repository'
Go to 'Code'. It is green in color
Copy the Link
Add the copied repository URL to the local machine by entering the following command.
git remote add origin (Repository URL)
This will make the connection between the local machine and Github.
After connecting the local machine and Github, next thing is to push the file to Github with the below command
git push origin master
This will redirect you to signin to your Github account
You can run status command to view the status of the changes.
git status
Go to Github, click on master branch. This will reveal the index.html file that was successfully pushed to Github.
In the same manner, you can add other files to the repository.
Go to VSC, click on the folder and add a New file. Add a README.md file
Enter a text in the README.md file that was created and save.
Run (git status) to see the information you currently have.
git add. To add the file
git commit -m "Adding README.md"
git push origin master
To push the file to Github
Login to your Github account
Open the README.md file to view it, and also edit if necessary
To edit, click on the edit pen
a. Edit
b. Commit message
c. Save
git pull origin master
This will pull the changes made from Github to the local machine
Posted on August 4, 2024
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.
Related
November 29, 2024