Git Introduction
A beginner's guide to essential Git commands and configurations.
This guide provides a collection of common Git commands, along with explanations on how to configure and use Git for version control. Whether you're just getting started or need a reference for frequently used commands, this guide is designed to help.
Table of Contents
- Global Configuration
- Set your name
- Set your email
- Set default text editor
- Open Git config settings
- Set line ending preferences
- Useful Git Commands
- Initialize a Git repository
- Stage changes
- Make a commit
- Remove files
- Rename or move files
- View status
- View differences
- View commit history
- Branch management
- Merging changes
- Working with remotes
Global Configuration
Set your name
To configure Git to use your name in commits, run:
Set your email
To set the email associated with your Git commits:
Set default text editor
If you want to set a default text editor, such as VSCode, for commit messages:
Open Git config settings
To open and edit Git's global configuration settings:
Set line ending preferences
For Linux/Mac systems (use Unix-style line endings):
For Windows systems (use Windows-style line endings):
For other options, you can view the Git configuration help:
Useful Git Commands
Initialize a Git repository
To initialize a new Git repository in the current folder:
Stage changes
To stage all changes (deleted, modified, and new files):
Make a commit
To create a new commit with a message:
Remove files
To remove a file from your working tree and stage the removal:
Rename or move files
To rename or move a file:
View status
To check the status of your working directory:
For a more concise, cleaner output of the status:
View differences
To view the unstaged differences in your working directory:
To compare the changes already staged for commit:
View commit history
To view the commit history:
For a simplified, one-line-per-commit view:
Branch management
-
To view the current branch:
git branch -
To view all local and remote branches:
git branch -vva -
To create and switch to a new branch:
git checkout -b <branch-name> -
To delete a local branch:
git branch -d <branch-name> -
To switch between branches:
git switch <branch-name>
Merging changes
To merge changes from one branch into your current branch:
Working with remotes
-
To add a remote repository:
git remote add origin https://github.com/your/repo.git -
To push changes to a remote branch:
git push origin <branch-name> -
If local and remote branch names are different:
git push origin <local-branch>:<remote-branch> -
To create and push to a
mainbranch:git push -u origin main -
To list, remove, or rename remote repositories:
git remote -v
git remote rm <name>
git remote rename <oldname> <newname> -
After pulling changes, to view the history of changes:
git whatchanged
For more details and advanced options, refer to the Git documentation.