For Windows you can download git from here, on Mac you can use Homebrew, and here is how to install git on Linux:
Windows
Download and install the latest Git for Windows Installer here.
Mac
Download and install the latest Git for Mac installer here.
Installing git on Linux
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get install git
Auto update to latest git
sudo add-apt-repository ppa:git-core/ppa
sudo apt-get update
sudo apt-get upgrade
sudo yum install git
sudo dnf install git
sudo pacman -Sy git
sudo emerge --ask --verbose dev-vcs/git
git config --global user.name "Stefan Pejcic"
Setup default name
git config --global user.email "[email protected]"
Setup default email
You can also associate a name to the remote server address, to avoid typing it every time when you push changes to the server.
git remote add gh https://github.com/stefanpejcic/stefan_init.git
git init
Create a git repository in current directory.
git clone url_or_path
Clone the repository from url_or_path to current directory.
git clone url_or_path
Clone the repository from url_or_path to current directory.
git remote -v
After you made changes to the file, then you can commit your changes to local repository.
git status
Show current status – list all modified files
git add .
Add changes from current directory to local staging area
git commit -m"message"
git commit --amend -m "new commit message"
git reset --soft HEAD^
git reset --hard HEAD^
Examples
git add index.cfm get.cfc
git commit -m"Fixed bug 123."
Commit several files from the directory
git add .
git commit -m"Fixed bug 123."
Commit all files in current directory
git add -A .
git commit -m"Fixed bug 123."
Commit all NEW files in current directory
git log
With Push and Pull you can sync files between your local repository and the remote one.
git pull
Pull files from remote repository to your local one
git push
Push files from local repository to the remote one
Examples
Update your local repository (and working directory) from remote server
git pull https://github.com/stefanpejcic/python.git
You can also force a pull from the server by removing all local changes
git reset --hard
git clean -xdf
git pull remote_server_or_name
You can also request a clean sync (a fresh copy) of a remote repository
git remote add upstream https://github.com/stefanpejcic/python
git fetch upstream
git checkout master
git reset --hard upstream/master
git push origin master --force
With reset / rm commands you can unadd a file from the staging area or delete it entirely from the remote repository.
Unadd a specific file from the staging area
git reset myFileName
Unadd ALL files from the staging area
git reset .
git reset --hard
Delete a specific file (or files) from the local repository
git rm file_name
Delete an entire directory from the local repository
git rm -r dir_name
delete all untracked local files and directories (DANGEROUS)
git clean -xdf
Here’s how to find differences between various areas of git on your local repository: working directory, staging area and HEAD.
Find out when is a line added to a file
git blame file_name
Show state of last commit, staging area and working directory
git status
Get commit IDs
git log file_name
Show difference between last changes
git log -p -2 --color
Get last 3 commit IDs
git log -3
Show ALL difference between working directory & index
git diff --color
If you see no difference, then check staging
git diff --staged
Show difference for 1 file between working directory & index
git diff --color filename
Show difference between index & latest commit (HEAD)
git diff --color --staged ‹commitID›
Show difference between last commit & working directory
git diff --color ‹commitID›
You can revert changes in the working directory all the way up to the last commit
Update the file_name in working directory
git checkout -- file_name
update the dir_full_path to the commit version in HEAD
git checkout HEAD -- dir_full_path
git checkout HEAD -- file_name
git checkout HEAD^ -- file_name
git clean -d -x -f
Use git branch to list all local branches, the current branch is indicated by a asterisk *
Show all local branches
git branch
Show branches and their commits
git show-branch
Create a new branch name from “master”
git branch name master
Switch to a branch by updating files in the current directory
git checkout branch_name
git clone --b branchname --single-branch remote-repo-url
git clone --branch branchname remote-repo-ur
Rename a branch
git branch -m old_name newName
Rename a branch even if a branch with the same name exists
git branch -M old_name newName
git fetch -- all
Merge a branch named name into the current branch
git merge name
Delete a branch (must be merged first)
git branch -d branch_name
Force delete a branch (even if not merged)
git branch -D branch_name
git push {remote_name} –delete {branch_name}
git archive lets you create a copy of your git directory, without the .git data, in zip or tar.gz format.
Archive all files from the current directory to myArchive file
git archive -o ../myArchive.zip HEAD
zip -d xyz some.zip
Unzip content to a new parent directory xyz
Save working directory into temp storage
git stash save
Restore working directory from temp storage
git stash pop
List your stash
git stash list
git log
git log --author <name>
git log --committer <name>
git log --before <date>
git log --after <date>
git log --after <date> --before <date>
git log --pretty=format:"Commit Hash: %H, Author: %aN, Date: %aD"
Example of .gitignore file
~/git/HiddenProject
.git
.gitignore
README.md
HiddenProject.py
Help us keep the git CheatSheet up-to-date and enrich it by sharing the useful git commands that you know with other system administrators.