└── README.md /README.md: -------------------------------------------------------------------------------- 1 | ## Overview 2 | 3 | These are a list of useful commands from [this](https://www.youtube.com/watch?v=Loav1kbA640) video on Youtube by Siraj Raval. 4 | 5 | 6 | 7 | # Useful Git Commands 8 | 9 | ## About it 10 | > Have you recently started using Git? This should give you the base commands you need to perform the most common actions in Git. If you find a command that is not here, or could be explained better, please don't hesitate in * [Contributing](#contributing). Cheers! 11 | 12 | ## Table of contents 13 | 14 | * [Install git](#install-git) 15 | * [Setting up git](#setting-up-git) 16 | * [Applying colour to git ](#applying-colour-to-git) 17 | * [Initializing a repository in an existing directory](#initializing-a-repository-in-an-existing-directory) 18 | * [Checking the status of your files](#checking-the-status-of-your-files) 19 | * [Staging files](#staging-files) 20 | * [Stashing files](#stashing-files) 21 | * [Committing files](#committing-files) 22 | * [Branching and merging](#branching-and-merging) 23 | * [Resetting](#resetting) 24 | * [Git remote](#git-remote) 25 | * [Git grep](#git-grep) 26 | * [Git blame](#git-blame) 27 | * [Git log](#git-log) 28 | * [Checking what you are committing](#checking-what-you-are-committing) 29 | * [Useful Commands](#useful-commands) 30 | * [Useful Alias](#useful-alias) 31 | * [Contributing](#contributing) 32 | 33 | #### Git 34 | 35 | Git is a distributed version control system, very easy to learn and supper fast! 36 | 37 | #### Install Git 38 | 39 | There are a few different ways to install git (from source or for Linux) but the purpose of this page is to focus on git commands, so I am going to assume you are installing git on a Mac. 40 | 41 | To view other ways of installing it visit the [Git official site](http://git-scm.com/book/en/Getting-Started-Installing-Git) 42 | 43 | Click [here](http://git-scm.com/download/mac) to download and install Git 44 | 45 | ##### Setting up git 46 | 47 | ```sh 48 | $ git config --global user.name "User Name" 49 | 50 | $ git config --global user.email "email" 51 | ``` 52 | 53 | ##### Applying colour to git 54 | 55 | ```sh 56 | $ git config --global color.ui true 57 | ``` 58 | 59 | ##### Initializing a repository in an existing directory 60 | 61 | If you’re starting to track an existing project in Git, you need to go to the project’s directory and type: 62 | 63 | ```sh 64 | $ git init 65 | ``` 66 | This creates a new subdirectory named .git that contains all of your necessary repository files — a Git repository skeleton. At this point, nothing in your project is tracked yet. 67 | 68 | To start version-controlling existing files you should start by tracking those files and do an initial commit. To accomplish that you should start with a few `$ git add` that specifies the files you want to track followed by a commit. 69 | 70 | ```sh 71 | $ git add 72 | $ git add README 73 | $ git commit -m 'Initial project version' 74 | ``` 75 | #### Checking the status of your files 76 | 77 | The main tool you use to determine which files are in which state is the `$ git status` command. If you run this command directly after a clone, you should see something like this: 78 | 79 | ```sh 80 | $ git status 81 | # On branch master 82 | nothing to commit (working directory clean) 83 | ``` 84 | 85 | If you add a new file to your project, and the file didn't exist before, when you run a `$ git status` you should see your untracked file like this: 86 | 87 | ```sh 88 | $ git status 89 | # On branch master 90 | # Untracked files: 91 | # (use "git add ..." to include in what will be committed) 92 | # 93 | # README 94 | nothing added to commit but untracked files present (use "git add" to track) 95 | ``` 96 | 97 | #### Staging files 98 | 99 | After initializing a git repository in the chosen directory, all files will now be tracked. Any changes made to any file will be shown after a `$ git status` as changes not staged for commit. 100 | 101 | To stage changes for commit you need to add the file(s) - or in other words, stage file(s). 102 | 103 | ```sh 104 | # Adding a file 105 | $ git add filename 106 | 107 | # Adding all files 108 | $ git add -A 109 | 110 | # Adding all files changes in a directory 111 | $ git add . 112 | 113 | # Choosing what changes to add (this will got through all your changes and you can 'Y' or 'N' the changes) 114 | $ git add -p 115 | ``` 116 | 117 | #### Stashing files 118 | 119 | Git stash is a very useful command, where git will 'hide' the changes on a dirty directory - but no worries you can re-apply them later. The command will save your local changes away and revert the working directory to match the HEAD commit. 120 | 121 | ```sh 122 | # Stash local changes 123 | $ git stash 124 | 125 | # Stash local changes with a custom message 126 | $ git stash save "this is your custom message" 127 | 128 | # Re-apply the changes you saved in your latest stash 129 | $ git stash apply 130 | 131 | # Re-apply the changes you saved in a given stash number 132 | $ git stash apply stash@{stash_number} 133 | 134 | # Drops any stash by its number 135 | $ git stash drop stash@{0} 136 | 137 | # Apply the stash and then immediately drop it from your stack 138 | $ git stash pop 139 | 140 | # 'Release' a particular stash from your list of stashes 141 | $ git stash pop stash@{stash_number} 142 | 143 | # List all stashes 144 | $ git stash list 145 | 146 | # Show the latest stash changes 147 | $ git stash show 148 | 149 | # See diff details of a given stash number 150 | $ git diff stash@{0} 151 | ``` 152 | 153 | #### Committing files 154 | 155 | After adding/staging a file, the next step is to commit staged file(s) 156 | 157 | ```sh 158 | # Commit staged file(s) 159 | $ git commit -m 'commit message' 160 | 161 | # Add file and commit 162 | $ git commit filename -m 'commit message' 163 | 164 | # Add file and commit staged file 165 | $ git commit -am 'insert commit message' 166 | 167 | # Amending a commit 168 | $ git commit --amend 'new commit message' or no message to maintain previous message 169 | 170 | # Squashing commits together 171 | $ git rebase -i 172 | This will give you an interface on your core editor: 173 | # Commands: 174 | # p, pick = use commit 175 | # r, reword = use commit, but edit the commit message 176 | # e, edit = use commit, but stop for amending 177 | # s, squash = use commit, but meld into previous commit 178 | # f, fixup = like "squash", but discard this commit's log message 179 | # x, exec = run command (the rest of the line) using shell 180 | 181 | # Squashing commits together using reset --soft 182 | $ git reset --soft HEAD~number_of_commits 183 | $ git commit 184 | ** WARNING: this will require force pushing commits, which is OK if this is on a branch before you push to master or create a Pull Request. 185 | ``` 186 | 187 | #### Branching and merging 188 | 189 | ```sh 190 | # Creating a local branch 191 | $ git checkout -b branchname 192 | 193 | # Switching between 2 branches (in fact, this would work on terminal as well to switch between 2 directories - $ cd -) 194 | $ git checkout - 195 | 196 | # Pushing local branch to remote 197 | $ git push -u origin branchname 198 | 199 | # Deleting a local branch - this won't let you delete a branch that hasn't been merged yet 200 | $ git branch -d branchname 201 | 202 | # Deleting a local branch - this WILL delete a branch even if it hasn't been merged yet! 203 | $ git branch -D branchname 204 | 205 | # Remove any remote refs you have locally that have been removed from your remote (you can substitute to any remote branch) 206 | $ git remote prune origin 207 | 208 | # Viewing all branches, including local and remote branches 209 | $ git branch -a 210 | 211 | # Viewing all branches that have been merged into your current branch, including local and remote 212 | $ git branch -a --merged 213 | 214 | # Viewing all branches that haven't been merged into your current branch, including local and remote 215 | $ git branch -a --no-merged 216 | 217 | # Viewing local branches 218 | $ git branch 219 | 220 | # Viewing remote branches 221 | $ git branch -r 222 | 223 | # Rebase master branch into local branch 224 | $ git rebase origin/master 225 | 226 | # Pushing local branch after rebasing master into local branch 227 | $ git push origin +branchname 228 | ``` 229 | 230 | #### Fetching and checking out remote branches 231 | 232 | ```sh 233 | # This will fetch all the remote branches for you. 234 | $ git fetch origin 235 | 236 | # With the remote branches in hand, you now need to check out the branch you are interested in, giving you a local working copy: 237 | $ git checkout -b test origin/test 238 | 239 | # Deleting a remote branch 240 | $ git branch -rd origin/branchname 241 | $ git push origin --delete branchname or $ git push origin:branchname 242 | ``` 243 | 244 | #### Merging branch to trunk/master 245 | 246 | ```sh 247 | # First checkout trunk/master 248 | $ git checkout trunk/master 249 | 250 | # Now merge branch to trunk/master 251 | $ git merge branchname 252 | 253 | # To cancel a merge 254 | $ git merge --abort 255 | ``` 256 | 257 | #### Updating a local repository with changes from a Github repository 258 | 259 | ```sh 260 | $ git pull origin master 261 | ``` 262 | 263 | #### Tracking existing branch 264 | 265 | ```sh 266 | $ git branch --set-upstream-to=origin/foo foo 267 | ``` 268 | 269 | #### Resetting 270 | 271 | ```sh 272 | # Mixes your head with a give sha 273 | # This lets you do things like split a commit 274 | $ git reset --mixed [sha] 275 | 276 | # Upstream master 277 | $ git reset HEAD origin/master -- filename 278 | 279 | # The version from the most recent commit 280 | $ git reset HEAD -- filename 281 | 282 | # The version before the most recent commit 283 | $ git reset HEAD^ -- filename 284 | 285 | # Move head to specific commit 286 | $ git reset --hard sha 287 | 288 | # Reset the staging area and the working directory to match the most recent commit. In addition to unstaging changes, the --hard flag tells Git to overwrite all changes in the working directory, too. 289 | $ git reset --hard 290 | ``` 291 | 292 | #### Git remote 293 | 294 | ```sh 295 | # Show where 'origin' is pointing to and also tracked branches 296 | $ git remote show origin 297 | 298 | # Show where 'origin' is pointing to 299 | $ git remote -v 300 | 301 | # Change the 'origin' remote's URL 302 | $ git remote set-url origin https://github.com/user/repo.git 303 | 304 | # Add a new 'origin' 305 | # Usually use to 'rebase' from forks 306 | $ git remote add [NAME] https://github.com/user/fork-repo.git 307 | ``` 308 | 309 | #### Git grep 310 | 311 | ```sh 312 | # 'Searches' for parts of strings in a directory 313 | $ git grep 'something' 314 | 315 | # 'Searches' for parts of strings in a directory and the -n prints out the line numbers where git has found matches 316 | $ git grep -n 'something' 317 | 318 | # 'Searches' for parts of string in a context (some lines before and some after the grepped term) 319 | $ git grep -C 'something' 320 | 321 | # 'Searches' for parts of string and also shows lines BEFORE the grepped term 322 | $ git grep -B 'something' 323 | 324 | # 'Searches' for parts of string and also shows lines AFTER the grepped term 325 | $ git grep -A 'something' 326 | ``` 327 | 328 | #### Git blame 329 | 330 | ```sh 331 | # Show alteration history of a file with the name of the author 332 | $ git blame [filename] 333 | 334 | # Show alteration history of a file with the name of the author && SHA 335 | $ git blame [filename] -l 336 | ``` 337 | 338 | #### Git log 339 | 340 | ```sh 341 | # Show a list of all commits in a repository. This command shows everything about a commit, such as commit ID, author, date and commit message. 342 | $ git log 343 | 344 | # List of commits showing commit messages and changes 345 | $ git log -p 346 | 347 | # List of commits with the particular expression you are looking for 348 | $ git log -S 'something' 349 | 350 | # List of commits by author 351 | $ git log --author 'Author Name' 352 | 353 | # Show a list of commits in a repository in a more summarised way. This shows a shorter version of the commit ID and the commit message. 354 | $ git log --oneline 355 | 356 | # Show a list of commits in a repository since yesterday 357 | $ git log --since=yesterday 358 | 359 | # Shows log by author and searching for specific term inside the commit message 360 | $ git log --grep "term" --author "name" 361 | ``` 362 | 363 | #### Checking what you are committing 364 | 365 | ```sh 366 | # See all (non-staged) changes done to a local repo 367 | $ git diff 368 | 369 | # See all (staged) changes done to a local repo 370 | $ git diff --cached 371 | 372 | # Check what the changes between the files you've committed and the live repo 373 | $ git diff --stat origin/master 374 | ``` 375 | 376 | #### Useful commands 377 | 378 | ```sh 379 | # Check if a sha is in production 380 | $ git tag --contains [sha] 381 | 382 | # Number of commits by author 383 | $ git shortlog -s --author 'Author Name' 384 | 385 | # List of authors and commits to a repository sorted alphabetically 386 | $ git shortlog -s -n 387 | 388 | # List of commit comments by author 389 | $ git shortlog -n --author 'Author Name' 390 | # This also shows the total number of commits by the author 391 | 392 | # Number of commits by contributors 393 | $ git shortlog -s -n 394 | 395 | # Undo local changes to a File 396 | $ git checkout -- filename 397 | 398 | # Shows more detailed info about a commit 399 | $ git cat-file sha -p 400 | 401 | # Show number of lines added and removed from a repository by an author since some time in the past. 402 | $ git log --author="Author name" --pretty=tformat: --numstat --since=month | awk '{ add += $1; subs += $2; loc += $1 - $2 } END { printf "added lines: %s, removed lines: %s, total lines: %s\n", add, subs, loc }' 403 | ``` 404 | 405 | #### Useful alias 406 | To add an alias simply open your .gitconfig file on your home directory and include the alias code 407 | 408 | ```sh 409 | # Shows the log in a more consisted way with the graph for branching and merging 410 | lg = log --color --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset' --abbrev-commit 411 | ``` 412 | 413 | ### Contributing 414 | 415 | 1. Fork it! 416 | 2. Create your feature branch: `git checkout -b my-new-feature` 417 | 3. Commit your changes: `git commit -m 'Add some feature'` 418 | 4. Push to the branch: `git push -u origin my-new-feature` 419 | 5. Submit a pull request - cheers! 420 | --------------------------------------------------------------------------------