Skip to content

Latest commit

 

History

History
411 lines (375 loc) · 17.5 KB

01_git_resources.md

File metadata and controls

411 lines (375 loc) · 17.5 KB

Git Version Control

Git Commands
When a # is used, this indicates a comment
When a <> is used, this indicates the location for a file, tag, hash, etc.
Favorite Git Commands
git commit -a -m 'added new benchmarks' # Adds all changes and commits in one line
Configuration Settings
git config --global user.name "David Yakobovitch" # Set a name for commits
git config user.name # View what is username, and same option for other config parameters
git config --global user.email "[email protected]" # Set an e-mail for commits
git config --global core.askpass # If wrong password typed, remind Github/Bitbucket/Gitlab to ask for password
git config --global core.autocrlf input # Disable CR LF message
git config --global core.autocrlf false # Disable CR LF message
git config --global core.editor "code" # Set your preferred text editor with Git
git config --list # View configuration settings
git config --help # View help for configuration options
git config --global rerere.enabled true # Avoid Repeated Merge conflicts
git config --global http.proxy http://user:[email protected] # Configure proxy credentials
git config --global https.proxy http://user:[email protected]
git config --global alias.co checkout # create git alias for commands git co would now work
git config --global alias.unstage 'reset HEAD--' # creates unstage command, I.e., (git unstage fileA)
Clone a repository from Github
git clone [_url_here] [directory_name_here] [local_file_path_here]
git clone <dir> <new_dir_name> # Creates a clone of directory as new repository locally
git clone -o hello_world # Default remote branch named as hello_world/master
Pull a Remote Repository
git pull # Equivalent to git fetch, and then auto git merge origin/master 
Remote repositories
git remote -v # View the remote repository location
git remote # View the remote repository name
git remote show <repo_name> # Shows info about the remote repository
git remote show origin
git remote add <shortname> <url> # Adds the repo as a remote to original repo
git remote add origin url #becomes master 
git remote rename pb paul # Rename remote repository shortnames 
git remote remove paul # Removes a remote repository connection
git ls-remote 
Initalize a local git on host machine
git init [directory_name]
git init # inside the directory
Create a new directory or file
mkdir [directory_name] #create directories
touch [file_name.extension] #create empty files
echo "text goes here" > file.txt #creates new file with text values
Stage changes to the active directory
git add . # all changes to be logged
git add [file.txt] # logs a file change
git add [file-1.text] [file-2.txt] # logs file changes for specific files
git add -h # View all flag options with add command
Commit changes to the git log
git commit -m "message goes here"
git commit --message "message goes here"
git commit --author="Vlad Dracula <[email protected]>" # chan
git commit --amend -m "comment" #Amends most recent commit for small update, shorter than a reset w/ commit
git commit -a -m 'added new benchmarks' # Adds all changes and commits in one line
git commit --author="David Yakobovitch <[email protected]>" # Change author for a specific commit message
git commit --amend                  # start $EDITOR to edit/amend the message
Status - show changes to the version controlled files
git status
git status --short # See abbreviated changes
git status -s # See abbreviated changes
Show changes before and after staging as colored-words
git diff # Shows changed before git add
git diff --staged # Shows changed after git add before git commit
git diff --cached # Same as git diff --staged
git diff HEAD <hash> <file_name> # Show the most recent changes for file on a Hash commit
git diff HEAD~1 <file_name> # Shows changes to file one previous version
git diff master..<branch_name> #Display differences between branches
git diff format-patch master..<branch_name> # Generates a file with patch for each commit reachable in the branch but not from the master
View completed commits
git log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
git log --pretty=oneline --max-count=2
git log --pretty=oneline --since='15 minutes ago'
git log --pretty=oneline --since='2.weeks'
git log --pretty=oneline --since='2008-01-15'
git log --pretty=oneline --since='2 years 1 day 3 minutes ago'
git log --pretty=oneline --until='15 minutes ago'
git log --pretty=oneline --all
git log --all --pretty=format:'%h %cd %s (%an)' --since='7 days ago'
git log --patch cats.txt # Shows log and diff together # Could use -p as alternative
git log -2 # Shows 2 most recent commits
.gitignore files
[text_editor_name] .gitignore
nano .gitgnore
*.a # Do ignore all files with this extension
.[oa] # Ignore files ending in .o or .a
*~ # Ignore all files ending in tilda ~
/TODO # only ignore the TODO file in the current directory, not subdir/TODO
!lib.a # Do not ignore this file
doc/*.txt # Ignore only files in doc directory ending in extension
doc/**/*.pdf # Ignore all files in doc directory and sub-directories ending in extension 
build/ # Ignore all files in directory named build
/heart # Ignore the heart file in current directory
echo filename >> .gitingore # add it to .gitignore to avoid re-adding it
Tags: A replacement for Hash strings - Reference points in time
git tag -l # View the tag history as a list
git tag -l "v1.8.5*" # View tags in 1.8.5 series
git tag v1 # Adds a tag to a commit
git tag <tag_name> <hash> # Adds a tag to a commit named with its hash 
git tag -d tagname # Deletes the tag name
git tag -a v1.4 -m "my version 1.4" # Creates annotated tag with a message
git show v1.4 # Shows the info about the tag and commit attached, no other info 
git tag -a v1.2 <hashid> # Attached a tag to a hashid
git tag -d <tagname> # Deletes a tag
git push origin --delete <tagname> # Delete a tag from remote
Checkout: Recover previous verisons
git checkout v1 # Checkout the branch to a specific commit with tag
git checkout v2~1 # Checkouts 1 previous version with tag
git checkout v2^1 # Checkouts 1 previous version with tag
git checkout <file_name> #Checkouts a specific filename # Removes local version
git checkout . # Reset all uncomitted code
git checkout HEAD~1 # Roll back one version
git checkout <hash> <file_name> # Roll back one version for a file
git checkout -f master <file_name> # Checks out one previous commit for file from Github/Bitbucket/Gitlab
git checkout master
Remove a file
git rm <filename> # Stages or adds the file for removal before commit
git rm --cached <filename> # Keep file in working tree, but remove from staging
Reset to undo changes
git reset HEAD <filename> # Undoes changes to the file from recent commit, but is available to recommit
git reset --hard <tag or hash> # Undoes commit back to the previous version
git fetch <shortname> # Downloads data from remote repo, but does not merge
git reset HEAD <file_name> # Removes staged files
git reset HEAD~2        # Undoes last two commits, keep changes
git reset --hard HEAD~2 # Undoes last two commits, discard changes  
git reset <file_name>      # Removes file; same operation as git remove --cached <file_name>
git reset --soft HEAD~1 # Soft reset against messy code, but still exists so you can fix and re-commit 
git reset --hard HEAD~1 # Permanently erases previous commit
Revert commits to previous states
git revert HEAD #Reverts changes after committing if accidently committed
git revert c761f5c              # reverts the commit with the specified id
git revert HEAD^                # reverts the second to last commit
git revert develop~4..develop~2 # reverts a whole range of commits
git revert -n HEAD # undo the last commit, but don't create a revert commit 
History: View all commits
# First, be sure to set in ~/.gitconfig an [alias] for hist
# [alias]
# hist = log --pretty=format:'%h %ad | %s%d [%an]' --graph --date=short
git hist master --all # Show all commit history
git hist --all # Show all history, even when a hard reset is performed, as all history is preserved
git hist --stat # Shows changes per each file as well
# Note: HEAD is the currently checked out commit.
Display the most recent commit
git show # Displays the most recent commit on the HEAD
git show HEAD~2 mars.txt # View version to roll back
git show HEAD^ or git show HEAD^1 or git show HEAD~ # View the parent of the HEAD commit
git show HEAD^^ # View the grandparent or second parent of the HEAD commit
git show <tag or hash>:<file_name> # View old version of the file
Rev-list: Display git quantity difference
git rev-list master..<your_branch> | wc -l # View how many commits ahead your branch is over the master/origin
Display the commit in a pretty-print format
git cat-file -p <commit-ID> #can be run on the tree (points to blobs), blob (sub-directory of files) or commit (points to tree) 
Re-load shell with updates to profile or config
source ~./profile
source ~/.gitconfig 
Dump files to your terminal
cat .git/objects/ce/<commit> | inflate | wc -c # Display wordcoutn of an object (requirs ruby installation)
cat .git/objects/ce/<commit> | inflate | hexdump -C # Explore hex characters
Gitk or Visual interactive (Personally recommend Fork software for Mac/Windows)
gitk # Visual interactive of all commits in a git repository
Push updates to Online repository
git push <remote> <branch> # Push upstream to remote from a branch your updates
git push origin master # Pushes the updates to the master branch
git push -u origin master
git push --force # forces a push, could have issues with conflicts
git push shared master # Push changes to a remote shared repository 
git push origin <tagname> # Pushes the tag to remote server, otherwise is only local
git push origin --tags # Transfer all local tags to remote server not already there
git push origin --delete <remote> # Deletes remote branch from main remote
Merging Branches
git merge feature # The feature branch merges into your active branch 
git merge master feature 
git merge-base feature master 
git merge origin/master # Can locally merge changes after a git fetch if desired
git mergetool # Launches your default merge tool to handle merges
Branching
git branch <branch_name> # Creates a new branch
git checkout -b <branch_name> # Creates a new branch and switches to the branch 
git checkout <remote_branch> # Creates and tracks branch if only 1 version on remote available 
git checkout -b sn origin/branch # Creates branch with short alias name sn 
git branch # View all local current branches
git branch -a # View all branches
git branch -d <branch_name> # Deletes a branch 
git branch --track <branch_name> origin/<branch_name> # Add a local branch that tracks a remote branch
git branch -r # Examine branches being tracked from remote repositories
git branch -v # View the last commit on each branch 
git branch -vv # Shows which local branches being tracked 
git fetch --all; git branch -vv # For up-to-date tracking data
git branch --merged # View which branches already merged into currently pointed branch 
git branch --no-merged # Show branches not yet merged
Fetching Remote Updates
git fetch # Retrieves updates from remote repo but does not merge them
Bare Repositories
git clone --bare hello hello.git # Used for sharing commits without files
Rebase or merge branches together and other commands
git rebase -i master
git rebase -i HEAD~2 # be presented with last 3 commits --root # for local
git rebase --onto HEAD [commitID] master 
git rebase --continue 
git rebase --skip 
git rebase --interactive 
git rebase --interactive origin branch
git rebase --abort #cancels the rebase 
git stash # code available for later
git stash list # see the changes 
git stash apply #applies stash 
git stash apply stash@{1}
git stash branch 'debugging-branch' # saves stashes in new branch 
git stash drop stash@{2} #drops one stash at a time 
git stash clear # removes all stashes 
git bisect start #launches git bisect utility 
git bisect bad [commitID] #indicate where a problem exists
git bisect good [commitID] #git searches for where issue occured and resolves 
git bisect bad [no commit ID needed]  # if still bad 
git bisect good #once it's good 
git bisect reset #reset branch to normal working state after 
pick [commitID] # a screen will appear and you can change all before pick to say squash to merge them
squash [commitID] # squash specific id into the pick 
git checkout -- Gemfile # reset specified path 
ctrl + shift + c #gitbash copy for PC 
ctrl + v #pastes for gitbash 
unset SSH_ASKPASS #asks for password 
Gitbash Shortcuts
  • Ctrl-a Move to the start of the line.
  • Ctrl-e Move to the end of the line.
  • Ctrl-b Move back one character.
  • Alt-b Move back one word.
  • Ctrl-f Move forward one character.
  • Alt-f Move forward one word.
  • Ctrl-] x Where x is any character, moves the cursor forward to the next occurance of x.
  • Alt-Ctrl-] x Where x is any character, moves the cursor backwards to the previous occurance of x.
  • Ctrl-u Delete from the cursor to the beginning of the line.
  • Ctrl-k Delete from the cursor to the end of the line.
  • Ctrl-w Delete from the cursor to the start of the word.
  • Esc-Del Delete previous word (may not work, instead try Esc followed by Backspace)
  • Ctrl-y Pastes text from the clipboard.
  • Ctrl-l Clear the screen leaving the current line at the top of the screen.
  • Ctrl-x Ctrl-u Undo the last changes.
  • Ctrl-_ does the same
  • Alt-r Undo all changes to the line.
  • Alt-Ctrl-e Expand command line.
  • Ctrl-r Incremental reverse search of history.
  • Alt-p Non-incremental reverse search of history.
  • !! Execute last command in history
  • !abc Execute last command in history beginning with abc
  • !abc:p Print last command in history beginning with abc
  • !n Execute nth command in history
  • !$ Last argument of last command
  • !^ First argument of last command
  • ^abc^xyz Replace first occurance of abc with xyz in last command and execute it
  • Aliases for .profile or [alias] for .gitconfig
    alias gaa='git add .'
    alias gc='git commit'
    alias ga='git add'
    alias gaaa='git add --all'
    alias gau='git add --update'
    alias gb='git branch'
    alias gbd='git branch --delete '
    alias gcm='git commit --message'
    alias gcf='git commit --fixup'
    alias gco='git checkout'
    alias gcob='git checkout -b'
    alias gcom='git checkout master'
    alias gcos='git checkout staging'
    alias gcod='git checkout develop'
    alias gd='git diff'
    alias gda='git diff HEAD'
    alias gi='git init'
    alias glg='git log --graph --oneline --decorate --all'
    alias gld='git log --pretty=format:"%h %ad %s" --date=short --all'
    alias gm='git merge --no-ff'
    alias gma='git merge --abort'
    alias gmc='git merge --continue'
    alias gp='git pull'
    alias gpr='git pull --rebase'
    alias gr='git rebase'
    alias gs='git status'
    alias gss='git status --short'
    alias gst='git stash'
    alias gsta='git stash apply'
    alias gstd='git stash drop'
    alias gstl='git stash list'
    alias gstp='git stash pop'
    alias gsts='git stash save'
    
    References
    FAQs: