14 September 2023
Version control is an essential tool for developers. It enables tracking changes to code over time and collaborating with others. Git and GitHub are commonly used together for version control and source code management.
This guide will teach you the fundamentals of Git and GitHub through the 10 most important commands. With these essential skills, you'll be able to use Git and GitHub effectively for projects. This guide is intended as a resource for students on our Git for Beginners Course.
Git is a distributed version control system that allows teams to track changes and collaborate on code. GitHub is a web-based platform built around Git for hosting repositories and facilitating collaboration.
Here are some key benefits of learning Git and GitHub:
This guide will focus on the following 10 foundational Git commands:
With these commands, you'll gain the skills needed to leverage Git and GitHub for version control in your own projects.
The git config
command allows you to configure user information, preferences, and other options for your Git setup.
Some common configurations are:
Identity:
bash
Copy code
git config --global user.name "Your Name" git config --global user.email [email protected]
Default editor:
bash
Copy code
git config --global core.editor vim
Aliases:
bash
Copy code
git config --global alias.co checkout
Using git config
appropriately sets up Git to work optimally for you.
The git init
command initializes a new local Git repository. This sets up all the necessary files and directories that Git needs to begin tracking changes to a project.
Here's how to initialize a new repo in the current directory:
bash
Copy code
git init
This will create a .git
subfolder that contains the Git data for the new repository. To initialize a repository in a different directory, provide the path:
bash
Copy code
git init ../new-repo
The git init
command is usually the first step in starting version control with Git for a new project.
The git clone
command creates a local copy of a remote Git repository. This enables you to download a repository hosted somewhere like GitHub and work with it locally.
For example, to clone the Git Flight Rules repository on GitHub:
bash
Copy code
git clone https://github.com/k88hudson/git-flight-rules.git
This will create a git-flight-rules
directory containing the code and Git history. You can use different protocols like SSH with git@
as well.
Cloning is the easiest way to get an existing Git project downloaded locally to start contributing to it.
The git add
command stages changed files for commit. This flags files that have changed to be included in the next commit.
You can add individual files:
bash
Copy code
git add file1.txt
Or patterns like all text files:
bash
Copy code
git add *.txt
Or stage all changed files:
bash
Copy code
git add .
Staging files with git add
is the first step in recording changes to the repository.
The git commit
command saves staged changes along with a commit message. This creates a new commit with the staged changes and adds it to the local repository history.
A commit message summarizes what is changed:
bash
Copy code
git commit -m "Fix typo in introduction"
Committing is how you move changes through different states in the Git workflow.
The git push
command sends local commits to a remote repository like GitHub. This updates the remote with local changes so they are visible to others.
For example:
bash
Copy code
git push origin main
This pushes the main
branch to the origin
remote repository. Commonly this synchronizes a local repo with GitHub.
Pushing shares code so other developers can access it from the remote.
The git pull
command fetches the latest changes from a remote repository and integrates them into the local codebase. This is an easy way to stay updated with changes from collaborators.
For example:
bash
Copy code
git pull origin main
This pulls down commits from the main
branch on the origin
remote and merges them into the local main
branch.
Regularly pulling remote changes avoids merge conflicts and keeps you up-to-date.
The git branch
command manages branches. Branches allow working on different versions of code independently.
Some common branch operations are:
git branch
git branch new-feature
git branch -d branch-name
Branches are useful for developing features or fixing bugs without impacting the main codebase.
The git merge
command merges branches together. This allows combining separate branches of development into one integrated version.
For example:
bash
Copy code
git checkout main git merge new-feature
This merges the changes from new-feature
into the current main
branch.
Branch merging with Git enables isolated collaboration on code that is later unified.
The git log
command displays the commit history and details. Reviewing Git history makes it easy to understand incremental changes and rollback as needed.
Some useful git log
options are:
git log
git log --oneline --decorate
git log --author="John"
Inspecting the git log
allows analyzing how the project has evolved over time.
With this solid foundation of essential Git commands, you're ready to use version control for your projects locally and on GitHub.
Some ways to build on these skills:
Git and GitHub are critical tools for developers. Learning these key commands provides the basics to then keep growing your skills. The benefits are huge - history tracking, collaboration, and backup. Put these commands into practice and see how Git and GitHub can transform your next project.
Start by installing Git on your computer if you haven't already. Then git init
initializes a new repository locally to begin version control on a project.
Branches isolate work so you can develop features or fix bugs without impacting the main codebase until ready.
GitHub provides remote hosting of repositories for collaboration, issues tracking, pull requests, and more on top of Git version control.
You can fork a repository, clone it locally, create a new branch, make your changes, push to your fork, and open a pull request.
Use Git every day for your projects. Refer to guides like these flight rules. And take a deep dive with books like Pro Git which is free online.
Carefully examine where conflicts occur in code and manually edit the files to resolve them, then add the changed files and make a commit.
Version control with Git and collaboration on GitHub are essential skills for developers in 2023. This guide provided a solid introduction to get started with 10 fundamental Git commands. With this foundation, you can now pursue Git mastery through daily practice and further learning. The investment will pay off in your ability to build better software.
Check our our article Git and GitHub for Beginners: A Step-by-Step Guide or you might like to take one of our courses in Git and Github
CONTACT
+44 (0)20 8446 7555
Copyright © 2024 JBI Training. All Rights Reserved.
JB International Training Ltd - Company Registration Number: 08458005
Registered Address: Wohl Enterprise Hub, 2B Redbourne Avenue, London, N3 2BS
Modern Slavery Statement & Corporate Policies | Terms & Conditions | Contact Us