Note: This article needs some editing, so please ignore it or regard it as my personal git cheat sheet.How does Git know what branch you’re currently on? It keeps a special pointer called HEAD. In Git, a lot different than the concept of HEAD in other VCSs, this is a pointer to the local branch you’re currently on.
If you modify a file after you run git add, you have to run git add again to stage the latest version of the file.
Git diff compares what is in your working directory with what is in your staging area. If you want to see what you’ve staged that will go into your next commit, you can use git diff –staged.
If you want to skip the staging area, Git provides a simple shortcut. Providing the -a option to the git commit command makes Git automatically stage every file that is already tracked before doing the commit, letting you skip the git add part.
$ git commit -a -m ‘added new benchmarks’
To remove a file from Git, you have to remove it from your tracked files (more accurately, remove it from your staging area) and then commit. The git rm command does that.
If you want to rename a file in Git, you can run something like
$ git mv file_from file_to
A huge number and variety of options to the git log command are available. One of the more helpful options is -p, which shows the diff introduced in each commit. You can also use -2, which limits the output to only the last two entries.
$ git remote show origin
Git Setup
// Show all your config settings git config --list // Global setup of name and email git config --global user.name "Your Name" git config --global user.email "your_email@whatever.com" // Colors in terminal git config --global color.ui true // Set up Sublime text as default editor git config --global color.editor subl // Setup Line Ending Preferences (UNIX/Mac) git config --global core.autocrlf input git config --global core.safecrlf true // Setup Line Ending Preferences (Windows) git config --global core.autocrlf true git config --global core.safecrlf true
Simple tasks
// Check the current status of the repository, which branch, files to commit etc... git status // Stage (existing or non-existing) files git add myfile.html git status // Git addgit add -A
// stages Allgit add .
// stages new and modified, without deletedgit add -u
// stages modified and deleted, without new // Commit file git commit -m "Added 3 fields" // Amend commit git commit --amend -m "Added 4 fields" // Unstage file git reset HEAD myfile.html // Adding in all the changes to the files in the current directory and below git add .
Git log
// List all previous commits
git log
// Or, on one line
git log --pretty=oneline
git log --pretty=oneline --since='2 days ago'
git log --pretty=oneline --author='Tom Gre' // Case sensitive
git log --pretty=format:"%h %ad | %s%d [%an]" --graph --date=short
// Explained at: http://gitimmersion.com/lab_10.html
git log -p -1 // Show details of latest commit
// All log commands (huge list)
man git-log
Alias commands
Add to .gitconfig in home directory:
[alias] co = checkout ci = commit st = status br = branch hist = log --pretty=format:\"%h %ad | %s%d [%an]\" --graph --date=short type = cat-file -t dump = cat-file -p
Get old version
// Get old version git hist git checkout <hash> // Return to latest version git checkout master
Tagging
git tag v1 // set tag "v1" git tag -d v1 // delete tag // Check out previous version git checkout v1^
Branches
git checkout -b greet // Shortcut for creating and checking out new branch
Merge branches
Let’s merge branch “feature” into branch “development”.
git branch -a // List local branches git checkout feature // Change working dir to feature git merge development // Merge feature with development // ... solve merge conflicts, add and commit them git checkout development // Change working dir to development git merge--no-ff
feature // Merge feature into development, keep new commit object git
branch -d feature
// Delete feature branch
History / Comparing branches
git hist // Shouw history of one branch
git hist --all
git log -p -1
// Show details of latest commit
gitk filename.ext // Show diff of file
// Show commits to push for a certain author
git log origin/myproject..myproject --author=Tom
Undoing commits
git revert HEAD // Undo last commit git revert b7b305e // Undo certain commit git reset --hard b7b305e // Undo certain commit and remove form commit history
Removing files
Clean up working directory.
- Reset modified files with “git reset”.
- Delete untracked files with “git clean”.
git reset --hard git clean -d -f -n // Dry run git clean -d -f -x // Do it
Origin and pushing
git diff --stat origin/mybranch
// Diff between local (also unstaged!) and remote files, i.e. files in differencegit diff --stat origin/mybranch HEAD
// Diff between locally comitted and remote files, i.e. files to be pushedgit diff --stat origin/mybranch
// Diff between local (also unstaged!) and remote files, i.e. files in differencegit diff --stat origin/mybranch HEAD
// Diff between locally comitted and remote files, i.e. files to be pushedgit diff --stat origin/mybranch
// Diff between local (also unstaged!) and remote files, i.e. files in differencegit diff --stat origin/mybranch HEAD
// Diff between locally comitted and remote files, i.e. files to be pushedgit diff --stat origin/mybranch
// Diff between local (also unstaged!) and remote files, i.e. files in differencegit diff --stat origin/mybranch HEAD
// Diff between locally comitted and remote files, i.e. files to be pushedgit diff --stat origin/mybranch
HEAD // Diff between local committed and remote files git pull origin branch_name // Fetch and merge changes from origin git push origin branch_name // Push commits to origin
Conflicts?
Open with git mergetool your favorite diff tool from commandline.
When to Rebase, When to Merge?
Don’t use rebase …
- If the branch is public and shared with others. Rewriting publicly shared branches will tend to screw up other members of the team.
- When the exact history of the commit branch is important (since rebase rewrites the commit history).
Given the above guidelines, one tends to use rebase for short-lived, local branches and merge for branches in the public repository.
Exploring remote branches
git remote // Get remote branch name git remote show origin // Get detailed info on remote branch origin
The convention is to use the name “origin” for the primary centralized repository.
Tracking branches
git branch --track greet origin/greet // Create new local tracking branch git branch --set-upstream greet upstream/greet // Track existing local branch
// gebleven bij http://gitimmersion.com/lab_46.html