CUSTOMISED
Expert-led training for your team
Dismiss
10 Git Commands every developer should know

14 September 2023

10 Git Commands every developer should know

 

Learn Git and GitHub with these 10 Essential Commands

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. 

Introduction to Git and GitHub

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:

  • Track and record changes to code over time
  • Review project history and revert to older versions if needed
  • Create branches to isolate new features or bug fixes
  • Merge branches back together when work is completed
  • Collaborate with other developers on shared repositories
  • Backup code in remote repositories like GitHub

This guide will focus on the following 10 foundational Git commands:

  1. git config
  2. git init
  3. git clone
  4. git add
  5. git commit
  6. git push
  7. git pull
  8. git branch
  9. git merge
  10. git log

With these commands, you'll gain the skills needed to leverage Git and GitHub for version control in your own projects.

1. git config

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.

2. git init

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.

3. git clone

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.

4. git add

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.

5. git commit

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.

6. git push

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.

7. git pull

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.

8. git branch

The git branch command manages branches. Branches allow working on different versions of code independently.

Some common branch operations are:

  • List branches: git branch
  • Create new: git branch new-feature
  • Delete: git branch -d branch-name

Branches are useful for developing features or fixing bugs without impacting the main codebase.

9. git merge

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.

10. git log

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:

  • Show history of current branch: git log
  • Pretty print with summary: git log --oneline --decorate
  • Filter by author: git log --author="John"

Inspecting the git log allows analyzing how the project has evolved over time.

Continue Learning Git and GitHub

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:

  • Configure a GitHub account and repositories
  • Learn more advanced Git workflows and commands
  • Set up Git integration in your code editor like VSCode
  • Use branches and merging for collaboration
  • Explore GitHub features like issues and pull requests
  • Contribute to open source projects on GitHub

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.

Frequently Asked Questions

How do I get started with Git?

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.

What's the benefit of Git branches?

Branches isolate work so you can develop features or fix bugs without impacting the main codebase until ready.

Why use GitHub in addition to Git?

GitHub provides remote hosting of repositories for collaboration, issues tracking, pull requests, and more on top of Git version control.

How do I contribute to a project on GitHub?

You can fork a repository, clone it locally, create a new branch, make your changes, push to your fork, and open a pull request.

What's the best way to learn Git more in-depth?

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.

How do I fix merge conflicts in Git?

Carefully examine where conflicts occur in code and manually edit the files to resolve them, then add the changed files and make a commit.

Conclusion

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

About the author: Daniel West
Tech Blogger & Researcher for JBI Training

CONTACT
+44 (0)20 8446 7555

[email protected]

SHARE

 

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

POPULAR

Rust training course                                                                          React training course

Threat modelling training course   Python for data analysts training course

Power BI training course                                   Machine Learning training course

Spring Boot Microservices training course              Terraform training course

Kubernetes training course                                                            C++ training course

Power Automate training course                               Clean Code training course