D

DGit and the Command Line

D.1

Git and the Command Line

CREATE A REPOSITORY   GIT ADD AND COMMIT
$ git init   $ git add .
Create a new local repository   Add all current changes to the staging area
 
$ git clone git@github.com:<repo-name>   $ git add <file>
Clone an existing repository from GitHub   Add all current changes in a file to the staging area
 
Status and history   $ git commit
$ git status   Commit all staged changes (will prompt for a message)
Show the status of a repository  
  $ git commit –m "commit message"
$ git diff   Commit all staged changes with a message
Show changes in tracked files  
  $ git commit -a
$ git log   Stage and commit all changes (will prompt for a message)
Show all commits, starting with newest in verbose form  
  $ git commit –am "commit message"
$ git log --oneline   Stage and commit all changes with a message
Show all commits—one commit per line  
  BRANCHES
$ git log --oneline --graph   $ git branch -av
Show all commits —one commit per line with branch graph   List all branches
 
$ git log –p <file>   $ git branch <new-branch>
Show changes for a specific file   Create a new branch based on the current head
 
$ git blame <file>   $ git checkout <branch>
Who change what and when in a specific file   Switch to a different branch
 
TAGS   $ git branch -d <branch>
$ git tag --list   Delete a branch
List all tags  
  $ git checkout --track <remote/branch>
$ git tag <tag-name>   Create a new tracking branch based on a remote branch
Tag the current commit point with a lightweight tag  
  MERGE
$ git tag –a <tag-name>   $ git merge <branch>
Tag the current commit point with an annotated tag   Merge <branch> in to the current branch
 
$ git tag –d <tag-name>   $ git mergetool
Delete a tag   Start the configured merge tool
 
$ git tag <tag-name> <commit-hash>   UPDATE AND PUBLISH
Tag a specific commit hash   $ git remote add <connection-name>
  Add new remote repository, named <connection-name>
Reset and checkout  
$ git reset --hard HEAD   $ git remote -v
Discard all local changes in your working directory   List all configured remotes
 
$ git reset --hard <commit>   $ git remote show <remote>
Reset to a specific commit point (discards working changes)   Show specific information about a remote
 
$ git reset --mixed <commit>   $ git fetch <remote>
Reset to a specific commit point (preserving unstaged changes)   Download all changes from <remote>,but don‘t integrate into head
 
$ git reset --soft <commit>   $ git pull
Reset to a specific commit point (preserving uncommitted changes)   Download changes and directly merge into current branch
 
$ git reset <tag-name> --hard   $ git pull <remote> <branch>
Reset to a specific tag (discards working changes)   Download changes from specific repository and branch, directly
 
$ git reset <tag-name> --mixed   $ git push
Reset to a specific tag (preserving unstaged changes)   Publish local changes on current branch to the current remote
 
$ git reset <tag-name> --soft   $ git push <remote> <branch>
Reset to a specific tag (preserving uncommitted changes)   Publish local changes to specific repository and branch
 
$ checkout –b <newbranch>   $ git branch -dr <remote/branch>
Create a new branch based on a previous commit point   Delete a branch on the remote
 

D.2

Git Bash instruction summary

DIRECTORIES AND TERMINAL   FILES
$ clear   $ rm <file>
Clears the terminal window   Delete <file>
 
$ pwd   $ rm -f <file>
Display path of current working directory   Force delete <file> (if it is in use)
 
$ cd <directory>   $ mv <file-old> <file-new>
Change directory to <directory>   Rename <file-old> to <file-new>
 
$ cd ..   $ mv <file> <directory>
Change to parent directory   Move <file> to <directory> (will overwrite an existing file)
 
$ rm -r <directory>   $ cp <file> <directory>
Delete <directory>, use –rf to force delete   Copy <file> to <directory> (will overwrite an existing file)
 
$ ls   $ cp -r <directory1> <directory2>
List directory contents (ls can be replaced with dir)   Copy <directory1> and its contents to <directory2> (will overwrite)
 
$ ls -la   $ touch <file>
List detailed directory contents, including hidden files (same as dir)   Create file
 
$ mkdir <directory>   OUTPUT
Create new <directory>   $ cat <file>
  Output the contents of <file>
SEARCH  
$ find <dir> -name "<file>"   $ less <file>
Find all files named <file> inside <dir> (wildcards *, ? can be used)   Output the contents of <file> with pagination
 
$ grep "<text>" <file>   $ head <file>
Output all occurrences of <text> inside <file> (add -i for case-insensitivity)   Output the first 10 lines of <file>
 
$ grep -rl "<text>" <dir>   $ <cmd> > <file>
Search for all files containing <text> inside <dir>   Direct the output of <cmd> into <file>
use >> instead to append the output of <cmd> to <file>
 



End flourish image