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.
To configure Git to use your name in commits, run:
git config --global user.name "Your name"
To set the email associated with your Git commits:
git config --global user.email "[email protected]"
If you want to set a default text editor, such as VSCode, for commit messages:
git config --global core.editor "code --wait"
To open and edit Git’s global configuration settings:
git config --global -e
For Linux/Mac systems (use Unix-style line endings):
git config --global core.autocrlf input
For Windows systems (use Windows-style line endings):
git config --global core.autocrlf true
For other options, you can view the Git configuration help:
git config -h
To initialize a new Git repository in the current folder:
git init
To stage all changes (deleted, modified, and new files):
git add .
To create a new commit with a message:
git commit -m "Initial commit"
To remove a file from your working tree and stage the removal:
git rm <file>
To rename or move a file:
git mv <old-filename> <new-filename>
To check the status of your working directory:
git status
For a more concise, cleaner output of the status:
git status -s
To view the unstaged differences in your working directory:
git diff
To compare the changes already staged for commit:
git diff --staged
To view the commit history:
git log
For a simplified, one-line-per-commit view:
git log --oneline
-
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>
To merge changes from one branch into your current branch:
git merge <branch-name>
-
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
main
branch: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.