- Index
- What is Git
- What is GitHub?
- Git status
- Staging files
- Commits
- Get Started with Git
- Configure Git
- Ignoring files
- Important Points to Remember
- Resources
What is Git
Git is a distributed version control system (VCS). It's a system that keeps a record of changes to our project files over time. It enables us to record project changes and go back to a specific version of the files, at any given point in time. This system can be used by many people to efficiently work together and collaborate on team projects, where each developer can have their own version of the project, distributed on their computer. Later on, these individual versions of the project can be merged and adapted into the main version of the project.
Basically, it's a massively popular tool for coordinating parallel work and managing projects among individuals and teams. Needless to say, knowing how to use Git is one of the most important skills for any Software Developer nowadays
- and it's definitely a great addition to your resume!
What is GitHub?
GitHub is a hosting platform for Git repositories. You can use Git on its own without Github (and other similar platforms), but it's difficult without github to collaborate or share your code with others.
- Git is the version control system, the tool that tracks changes to our files over time
- Github is a hosting service for projects that use Git.
Using GitHub, we can upload a local project repository to a remote cloud-based GitHub repository. We can also interact with public repositories published by other developers.
ℹ️ GitHub could even be looked at as a social networking website for developers. Users can follow each other, give ratings, share or fork, like code via stars, comment via issues, collaborate and communicate.
GitHub allows developers to utilize, change or improve software from its repositories. Each repository contains all project files and the code history. Repositories can have multiple collaborators and can be either public or private.
GitHub is also a popular way developers to publish their project portfolio online. It's an easy way to showcase skills and experience to potential employers or clients. It's an important technology to be familiar with, especially for a new developer who is just starting out.
-
Version Control: Git keeps a record of every change you make to your project, so it has the ability to remember every change you make to your code. It's like time-travel! So, if you make a mistake you can easily go back to a previous version if you need to.
-
Sharing with Friends: You can share your code with your friends or anyone around the world, and they can marvel at your creations, give suggestions, take inspiration, or even add their own touches to your works.
-
Collaboration: . Multiple people can work together on the same project simultaneously, and Git helps merge everyone's work seamlessly and makes it effortless for everyone to contribute their parts.
-
Backup: Git Repository ensures safety and protects your work from getting lost or damaged. Even if your computer misbehaves, your projects are securely stored and can be accessed from another computer.
A Git repository is a container for a project that is tracked by Git.
Imagine a repository as a special place that stores your creative project, just like a treasure chest for your computer code and files. Git repository is a magical box that keeps track of every change you make to your code and files over time. The Repository is like your personal storage area in the digital world. It helps you keep track of your code, artworks, or any other digital wonders you create. Instead of saving your work scattered all over your computer, you place everything inside this repository.
We can single out two major types of Git repositories:
- Local repository - an isolated folder stored on your own computer, where you can work on the local version of your project.
- Remote repository - generally stored outside of your isolated local system, usually on a online server i.e github.com. It's especially useful when working in teams, this is the place where you can share your project code, see other people's code and integrate it into your local version of the project, and also push your changes to the remote repository.
CLI (command-line interface) allows you to interact with a computer using text-based commands instead of relying on Graphical user interfaces (GUIs), the CLI provides a powerful and efficient way to perform various tasks and operations directly from the Terminal also called shell. All terminals work almost similarly, CLI tools offer efficiency, flexibility, and scripting capabilities, making them powerful resources for developers, system administrators, and tech-savvy users.
Types of terminals:
- Bash
- Command prompt or CMD
- PowerShell
Git is a CLI Tool, which we can access with our system terminals. 🔴 All commands will be ran on the root of the project source directory.
git status
We can use the git add command to add our files to the staging area, which allows them to be tracked.
We can add a specific file to the staging area with the following command:
git add file.js
To add multiple files, we can do this:
git add file.js file2.js file3.js
Instead of having to add the files individually, we can also add all the files inside the project folder to the staging area:
git add .
By default, this adds all the files and folders inside the project folder to the staging area, from where they are ready to be committed and tracked.
git add
will stage the files that will be part of my commit
tip: make small commits
A commit is a snapshot of our code at a particular time, which we are saving to the commit history of our repository. Commit is like page in history book that has its own unique id and can never be changed.
After adding all the files that we want to track to the staging area with the git add
command, we are ready to make a commit.
To commit the files from the staging area, we use the following command:
git commit -m "Commit message"
Inside the quotes, we should write a commit message which is used to identify it in the commit history.
The commit message should be a descriptive summary of the changes that you are committing to the repository.
After executing that command, you will get the technical details about the commit printed in the terminal. And that's basically it, you have successfully made a commit in your project!
you can see Commit history on github.com
- Install Git from the official website.
- If everything went well, it should return the Git version that is installed on your computer.
- If you don't have already Sign up for an account at github.com.
- Once inside, you'll find your own Repositories.
- Create a new project or artwork by simply clicking the
New
button and giving it a unique name. - To initialize a local repository and start tracking your project, in terminal and navigate to the main folder of your project, then type
git init
. This command will generate a hidden.git
folder in your project, where Git stores all internal tracking data for the current repository. We don't need to look and change in .git. - Keep your repos public and share your code with world.
- Add your digital creations to the repository – your code, documents, images, or anything you create!
Open Terminal and type the commands given below to configure it. Replace the values inside the quotes with your name and email address.
git config --global user.name "Your Name"
git config --global user.email "yourname@example.com"
GitHub recently changed the default branch on new repositories from master to main, change the default branch for Git using this command:
git config --global init.defaultBranch main
To verify things are working properly, enter these commands and verify that the output matches your name and email address:
git config --list
YOU ARE READY TO GO!
To copy a github repo to your computer
git clone <link>
git add .
git commit -m "my message"
To send changes to your remote repository
git push origin main
If you have not cloned an existing repository and want to connect your repository to a remote server, you need to add it with
git remote add origin <link of your repo>
To update your local repository to the newest commit, execute
git pull
To ignore files or folders that you don't want to be tracked or added to the staging area, you can create a file called .gitignore
in your main project folder. Inside of that file, you can list all the file and folder names that you definitely do not want to track
- Repository is just a folder in Git.
- Always make a repo before starting to code.
- Keep committing code after finishing ever feature.
- Make a Pull Request after your work is done.
- Never close your computer without pushing code to Github.
- Keep you commit messages relevant to your feature.
After learning the basic commands, dive deep into the following:
- Cloning a repo
- Forking a repo
- Difference between cloning and forking
- Difference between origin and upstream
- Branches
- Merge Conflicts
- Pull Requests
Doing this small activity will give you a hands-on experience and help cement your knowledge:
- Fork this very repo.
- Clone it on your local machine.
- Create a new branch <Your_Name>.
- Edit README, add a new line and write, "<Your_Name>: I did it!".
- Commit the changes to your local repo.
- Push the code to your remote repo (the forked one).
- Send a pull request to me so I can accept your changes in this original repo.