In today‘s fast-paced world of software development, efficient collaboration and version control are paramount. This is where Git and GitHub, two cornerstones of modern development workflows, step i
n. Whether you’re a seasoned coder or just starting your journey, understanding these tools is essential. In this article, we’ll demystify Git and show you how GitHub can supercharge your collaboration efforts.
What is Git?
At its core, Git is a distributed version control system. Think of it as a digital time machine for your code. It allows you to track changes, work on different features simultaneously, and collaborate with others without the chaos of managing multiple versions manually.
Here are some key Git concepts:
Repository (Repo): This is where Git stores all the files, history, and information about your project. It’s like a project folder on steroids.
Commit: A commit is like taking a snapshot of your project at a specific point in time. It records changes you’ve made and allows you to revert to that state if needed.
Branch: Branches are parallel versions of your repository. You can work on a new feature or bug fix without affecting the main codebase.
Merge: Merging combines changes from one branch into another. It’s how you bring your feature branch’s changes into the main codebase.
Pull Request (PR): A PR is a request to merge changes from one branch into another. It’s a way to collaborate and review code before it’s merged.
Git offers several advantages:
Version Control: Say goodbye to “final_version,” “final_version2,” and other confusing file names. Git tracks changes at a granular level.
Collaboration: Multiple team members can work on the same project simultaneously without conflicts.
Backup: With Git, your code is stored in multiple places, reducing the risk of data loss.
Experimentation: Branching allows you to experiment without affecting the main code.
What is GitHub?
GitHub is a web-based platform built around Git. It adds a social and collaborative layer to version control. With GitHub, you can host Git repositories online, making it easier to work with others, even if they’re continents away.
Here’s what GitHub brings to the table:
Remote Repositories: Store your Git repositories in the cloud, accessible from anywhere.
Collaboration: Multiple people can work on the same project, and GitHub tracks their changes.
Pull Requests: Propose changes, have discussions, and review code before merging.
Issues: Track bugs, enhancements, and tasks for your project.
GitHub Actions: Automate your software workflows, from build and test to deploy.
- Install Git: On windows, you can download the Git installer from the official Git website at git-scm.com and run the executable.
- The Git installer for Windows is a wizard that guides you through the installation process.
- Select Components: This screen lets you choose which features of Git you want to install, such as the Git Bash, the Git GUI, the Git LFS, and the Windows Explorer integration. You can also choose where to install Git on your system. By default, all the features are selected and the installation path is C:\Program Files\Git. You can change these options according to your preferences.
- Choosing the default editor used by Git: This screen lets you choose which text editor you want to use with Git. By default, it is Vim, a command-line editor. You can also choose from other options, such as Notepad++, Visual Studio Code, or Nano. I have chosen VS code.
- Adjusting your PATH environment: This screen lets you choose how you want to access Git . We will choose the recommended option that is from the command line and also from 3rd party software.
- Choosing the HTTPS transport backend : Choose the default option that is use the OpenSSL library, which is more compatible with other Git clients and servers.
- Configuring the line ending conversions: This screen lets you choose how you want Git to handle the line endings in your files. Choose the option Checkout Windows-style, commit Unix-style line endings . This is the recommended option for Windows users who work with cross-platform projects.
- Configuring the terminal emulator to use with Git Bash: This screen lets you choose which terminal emulator you want to use with the Git Bash. Use MinTTY, a terminal emulator that provides a Unix-like environment on Windows. It supports features such as resizing, colors, Unicode, and mouse events.
- Configuring extra options: This screen lets you choose some extra options for Git, such as enabling file system caching, enabling Git Credential Manager. You can check these options.
- Configuring Experimental options : You can check both the options and click install.
To open Git from the start menu, you can follow these steps:
- Click on the Start button on the bottom left corner of your screen, or press the Windows key on your keyboard.
gitin the search box that appears. You should see a list of programs related to Git, such as Git Bash, Git GUI, and Git CMD.
- Click on the program that you want to use. For example, if you want to use Git Bash, which is a Unix-style terminal that allows you to run Git commands, click on Git Bash. A new window will open with a prompt that looks something like this:
- Similarly you can also open GIT GUI. But in this tutorial we will use command line tool.
Create a file and Open it in Git Bash :
- Create the specific file first which you want to commit in Git
- Navigate to the folder that contains the file you want to open
- Right-click on an empty space in the folder go to “Show more option” and select “Git Bash Here”. This will open a new Git Bash window in the current folder.
- Initialize a new Git repository: Type
git initand press Enter It will initialize empty Git repository into your project . You should see a message like this: Initialized empty Git repository in C:/Users/ninet/Desktop/JsCode/.git/
- This means that Git has created a hidden folder named
.gitin your project folder, which contains all the necessary files and information for your Git repository. You can verify this by typing
ls -aand pressing Enter. You should see something like this: ./ ../ .git/ test.js .
- The dot (
.) and the double dot (
..) are special entries that represent the current directory and the parent directory, respectively. Other than
.gitwhich is the hidden directory(you can’t see it in normal file explorer) the other visible entry is your file i.e. test.js, which we have created earlier.
- Check the status of your repository: Ensure that
test.jsis listed as an untracked file. Which means Git is saying test.js is still not added into Git. So we need to add this file.
test.jsto the staging area:
git add test.jsThis command stages the existing file
- Check the status again: Once again run the status command . You will see that
test.jsis now listed as a new file to be committed.
- Now the file has been added to Git and that’s why test.js is showing in green color. But we have to Commit the changes.
- The command is git commit -m “Initial commit” test.js and press enter. -m flag is used to add commit messages.
- Configure your Git user details:
- git config –global user.email “[email protected]”
- git config –global user.name “Your Name”
- Replace “[email protected]” with your email and “Your Name” with your name. These details are associated with your commits otherwise you will get this error. The
--globalflag ensures that these details are used for all your Git repositories on your machine.
- Now I am repeating the git commit -m “Initial commit” test.js command again. This time it will be successful.
- now we will re-run git status again – you will get the message nothing to commit and our working tree is clean.
- We are done with the Git part, lets move on to GitHub.
- Create a GitHub Account: If you don’t have one, sign up for a free GitHub account here.
- Otherwise go to GitHub and log in.
- Click on the “+” sign in the top right corner and select “New repository.”
- In the “Repository name” field, enter the name you prefer, such as “JsCode.” I have chosen this name because this is also our project name on Git.
- Add an optional description to provide more details about your project.
- You can choose to initialize this repository with a README file. This is a good practice as it helps others understand your project, and it will be the first commit in your repository.
- You can add a .gitignore file to specify which files or directories should be ignored by Git (e.g., node_modules, build/).
- You can also add a license if you want to specify how others can use your code.
- For now i am not doing anything to keep it simple , but you might want to add all these in your real project.
- Click the “Create repository” button to create the new repository on GitHub.
- Once the repository is created, we have to push our existing repository which we have already created on Git command line.
- GitHub will provide you with a URL for your repository (in our case https://github.com/studynine/JsCode.git ). You will use this URL when adding the remote in your local Git repository.
You have to run in Git command prompt below commands .
git remote add origin https://github.com/studynine/JsCode.git
Ignore this ( git branch -M main) cause we are already on the main branch . If you go back to Git Bash terminal the branch name is highlighted as main.
git push -u origin main
- The first time you push to GitHub, Git will prompt you for your GitHub credentials.
- Enter your GitHub username and password when prompted.
- If you have two-factor authentication enabled on GitHub, you’ll need to use a personal access token instead of your password.
- After successfully entering your credentials or personal access token, your changes should be pushed to the GitHub repository(just refresh the page). This process ensures a secure connection between your local Git repository and your GitHub repository.
Git and GitHub are essential tools for modern developers. They streamline collaboration, enhance code quality, and provide a safety net for your projects. As you continue your coding journey, explore advanced Git topics, like branching strategies and rebasing, and dive into GitHub’s features for project management and automation.
Remember, the best way to learn is by doing. Start small, experiment, collaborate, and watch your Git skills grow. Happy coding!
Interested in taking your Git and GitHub skills to the next level? Check out our Advanced Git and GitHub Course for in-depth knowledge and hands-on projects.