What is Git
Git is a version control system that allows multiple developers in a team collaborate on a project in real time, by tracking changes to source codes.
Key Git Concepts
- Repository - A folder where Git stores your project's code and its history of changes. It can be stored Locally or Remotely.
- Clones - A copy of a remote repository on your computer.
- Pull - Getting the lates changes from a remote repository.
- Push - Saving your changes to a remote repository.
- Commit - A snapshot of changes made to the repository.
- Branch - A parallel version of the repository.
- Merge - Combining changes from different branches.
What is Github
GitHub is a cloud-based platform based on Git, which hosts code repositories and helps developers collaborate.
Setting up Git
Git can be downloaded from git-scm.com
- For windows: Simply download and run the installer to install Git and Gitbash
- For MacOS: Open terminal and run
$ brew install wget
to install brew, then run$ brew install git
.
Configuring Git
Git requires your Username and email address to label your commits.
To add Username, run
$ git config --global user.name "Username"
To add Email Address
$ git config --global user.email "[email protected]"
Creating a Repository
using terminal on MacOS, or Git bash on Windows, create a directory for your project.
$ mkdir git-lab
Change the working directory to the one we just created.
$ cd git-lab
Initialize Git
To create our Repository in this directory, run
$ git init
A simple Html file can be created via the notepad or using the terminal.
$ touch index.html
To quickly add text to the html file
$ echo "this is an index file for my first repo" > index.html
Then we check the status of our file
$ git status
At this point, it is untracked. This means it is present in our folder, but it's absent from the staging area.
So we need to tell Git to track our file.
$ git add index.html
or git add -all
, git add -A
or git add .
to add all the files in the directory.
To unstage a file
git restore --staged index.html
Then check the status.
Commit
To save the staged file(s)
$ git commit -m "My first commit"
To see previous commit history,
$ git log
Branches
If we wanted to add changes to our project without affecting the main project, we create a branch.
$ git branch "parallel-repo"
To see all branches
$ git branch
To switch between branches
$ git checkout parallel-repo
To delete a branch
$ git branch -d parallel-repo
Merging
To combine changes from one branch into another, first switch to the branch you want to merge into(main or master)
$ git checkout main
Now we merge
$ git merge parallel-repo
Creating a Remote Repo
Go to github.com and create a new repository.
Copy git remote add origin https://github.com/username/repository.git
, then paste in terminal and run.
Finally run,
$ git push -u origin main
Git would ask to authorize Github, then the local commit gets pushed to the Github repository.
Top comments (4)
You wrote a good post, but i have a suggestion for you.
Thank you for this, I'll look to improve on this.
Good read
Thank you Victor