Git & GitHub - Git Basics
Introduction to basic Git commands and concepts
Understanding the basic commands and concepts of Git is essential for efficient version control. This guide introduces fundamental Git commands and concepts that every developer should know.
Key Points:
- Git is a powerful version control system used to track changes in source code.
- Basic Git commands include
init
,clone
,add
,commit
,status
,log
,branch
,checkout
, andmerge
. - Understanding these commands is crucial for managing your code efficiently.
Initializing a Repository
The first step in using Git is to initialize a repository. This can be done with the git init
command, which creates a new Git repository in your project directory.
# Initialize a new Git repository
$ git init
Cloning a Repository
To start working on an existing project, you can clone a repository using the git clone
command. This copies the repository from a remote source to your local machine.
# Clone a repository
$ git clone https://github.com/user/repository.git
Adding Changes
After making changes to your files, you need to add them to the staging area using the git add
command. This prepares the changes to be committed to the repository.
# Add a single file to the staging area
$ git add filename.txt
# Add all changes to the staging area
$ git add .
Committing Changes
To save your changes to the repository, use the git commit
command. This records the changes in the repository along with a descriptive message.
# Commit changes with a message
$ git commit -m "Descriptive commit message"
Checking the Status
The git status
command displays the state of the working directory and the staging area. It shows which changes have been staged, which haven't, and which files aren't being tracked by Git.
# Check the status of your repository
$ git status
Viewing Commit History
The git log
command shows the commit history of the repository. This allows you to see all the commits that have been made, along with their messages, authors, and dates.
# View commit history
$ git log
Branching and Merging
Branches in Git allow you to work on different versions of a project simultaneously. The git branch
command lists, creates, or deletes branches, while the git checkout
command switches between branches. Merging combines changes from different branches.
# List all branches
$ git branch
# Create a new branch
$ git branch new-branch
# Switch to the new branch
$ git checkout new-branch
# Merge changes from new-branch into the main branch
$ git checkout main
$ git merge new-branch
Summary
This guide introduced the basic Git commands and concepts necessary for version control. Understanding and using these commands will help you manage your code efficiently and collaborate effectively with other developers.