Table of Contents
Git Basics
Clarification
Git ≠ GitHub
There are many Git repository hosting services, and GitHub wasn't even the first. Some popular alternatives are GitLab and Bitbucket. But there are many others. Also, Git is not the only VCS in use today. It was originally created independently of any company to support the development of the Linux operating system. It was later adopted by a number of developers of open-source software for their projects before eventually becoming the de-facto standard VCS it is today for both open-source and proprietary projects.
Basic operations
- Create a git repository in the current directory:
git init
- Create a new directory with a git repository in it:
git init directory-name
- Stage a changed file or a new file for the next commit:
git add filename
- Stage all changed and new files for the next commit:
git add .
- Unstage a file you have staged for the next commit:
git rm --cached filename
- Commit the staged files:
git commit -m "A message about this commit."
- Get status of repository:
git status
- Get history of repository:
git log
- Delete a file:
git rm filename
- Rename or move a file:
git mv source destination
Use the git rm
and git mv
commands to delete and move/rename files instead of the standard tools in your operating system. Doing so will simplify staging.
Branching and merging
- A git repository has a default branch called main. (In older configurations this may be master.)
- To create a new branch:
git branch branch-name
- Switch to an existing branch:
git checkout branch-name
- Create a new branch and immediately switch to it:
git checkout -b branch-name
- Merge changes in another branch into the current branch:
git merge branch-name
- Delete a branch that you're not currently in:
git branch -d branch-name
The new git switch
command is an alternative to some git branch
operations. However, as of this writing, it is still experimental.
Ignoring files
- To make git ignore some files in your project, create a
.gitignore
file in the project directory.- .gitignore
# Ignore specific files my-passwords.txt banking-info.docx # Ignore compiled file(s) *.exe *.o # Ignore all files in directory foo foo/*
The file name must be .gitignore
, not .gitignore.txt
or gitignore.txt
. To create a .gitignore
file in Windows, open the Git Bash shell in the directory where you want the file and enter:
touch .gitignore
GUI interface
git gui
: opens a GUI for performing common git functions.gitk
: opens a GUI commit browsing tool.- Lots of other third party Git tools are available.
More git stuff
- My Git tips.
- Roger Dudler's git - the simple guide goes beyond what's here, is beginner friendly, and is quite good.