git.cheat 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. % git
  2. # Set global git user name
  3. git config --global user.name <name>
  4. # Set global git user email
  5. git config --global user.email <email>
  6. # Initializes a git repository
  7. git init
  8. # Clone a git repository
  9. git clone -b <branch_name> <repository> <clone_directory>
  10. # Shallow clone with depth 1 with all branches and submodules
  11. git clone --depth=1 --no-single-branch --recurse-submodules <repository> <clone_directory>
  12. # Rebase upstream master into local/origin master (use if people don't clone your repository)
  13. git fetch <remote_name>
  14. git checkout master
  15. git rebase <remote_name>/master
  16. git fetch --unshallow origin
  17. git push -f origin master
  18. # Merge upstream master into local/origin master (use if people clone your repository)
  19. git fetch <remote_name>
  20. git checkout master
  21. git merge <remote_name>/master
  22. git fetch --unshallow origin
  23. git push -f origin master
  24. # View all available remote for a git repository
  25. git remote --verbose
  26. # Adds a remote for a git repository
  27. git remote add <remote_name> <remote_url>
  28. # Renames a remote for a git repository
  29. git remote rename <old_remote_name> <new_remote_name>
  30. # Remove a remote for a git repository
  31. git remote remove <remote_name>
  32. # Checkout to branch
  33. git checkout <branch>
  34. # Displays the current status of a git repository
  35. git status
  36. # Displays unstaged changes for file
  37. cd <toplevel_directory>; \
  38. git diff <unstaged_files>
  39. # Stage single or multiple files
  40. cd <toplevel_directory>; \
  41. git add <changed_files>;
  42. # Stage all files in project
  43. git add -A
  44. # Create commit for staged files
  45. git commit -m "<commit_description>"
  46. # Create backdated commit for staged files
  47. git commit --date="<number_of_days_ago> days ago" -m "<commit_description>"
  48. # Pushes committed changes to remote repository
  49. git push -u <remote_name> <branch_name>
  50. # Pushes changes to a remote repository overwriting another branch
  51. git push <remote_name> <branch>:<branch_to_overwrite>
  52. # Overwrites remote branch with local branch changes
  53. git push <remote_name> <branch_name> -f
  54. # Pulls changes to a remote repo to the local repo
  55. git pull --ff-only
  56. # Merges changes on one branch into current branch
  57. git merge <branch_name>
  58. # Abort the current conflict resolution process, and try to reconstruct the pre-merge state.
  59. git merge --abort
  60. # Displays log of commits for a repo
  61. git log
  62. # Displays formatted log of commits for a repo
  63. git log --all --decorate --oneline --graph
  64. # Clear everything
  65. git clean -dxf
  66. # Sign all commits in a branch based on master
  67. git rebase master -S -f
  68. # See all open pull requests of a user on Github
  69. navi fn url::open 'https://github.com/pulls?&q=author:<user>+is:open+is:pr'
  70. # Checkout a branch from a fork
  71. git fetch origin pull/<pr_number>/head:pr/<pr_number> \
  72. && git checkout pr/<pr_number>
  73. # Add a new module
  74. git submodule add <repository> <path>
  75. # Update module
  76. git submodule update --init
  77. # Update module without init
  78. git submodule update
  79. # Pull all submodules
  80. git submodule foreach git pull origin master
  81. # Update all submodules
  82. git submodule update --init --recursive
  83. # Skip git hooks
  84. git commit --no-verify
  85. # Create new branch from current HEAD
  86. git checkout -b <new_branch_name>
  87. # Remove commits from local repository (destroy changes)
  88. git reset --hard HEAD~<number_of_commits>
  89. # Remove commits from local repository (keep changes)
  90. git reset --soft HEAD~<number_of_commits>
  91. $ branch: git branch | awk '{print $NF}'
  92. $ toplevel_directory: git rev-parse --show-toplevel
  93. $ unstaged_files: git status --untracked-files=no -s --porcelain | awk '{print $NF}' --- --multi true
  94. $ changed_files: git status --untracked-files=all -s --porcelain | awk '{print $NF}' --- --multi true