├── .gitignore ├── README.md ├── main.md └── resources.md /.gitignore: -------------------------------------------------------------------------------- 1 | 2 | # Created by https://www.gitignore.io/api/linux,macos,windows 3 | 4 | ### Linux ### 5 | *~ 6 | 7 | # temporary files which can be created if a process still has a handle open of a deleted file 8 | .fuse_hidden* 9 | 10 | # KDE directory preferences 11 | .directory 12 | 13 | # Linux trash folder which might appear on any partition or disk 14 | .Trash-* 15 | 16 | # .nfs files are created when an open file is removed but is still being accessed 17 | .nfs* 18 | 19 | ### macOS ### 20 | # General 21 | .DS_Store 22 | .AppleDouble 23 | .LSOverride 24 | 25 | # Icon must end with two \r 26 | Icon 27 | 28 | # Thumbnails 29 | ._* 30 | 31 | # Files that might appear in the root of a volume 32 | .DocumentRevisions-V100 33 | .fseventsd 34 | .Spotlight-V100 35 | .TemporaryItems 36 | .Trashes 37 | .VolumeIcon.icns 38 | .com.apple.timemachine.donotpresent 39 | 40 | # Directories potentially created on remote AFP share 41 | .AppleDB 42 | .AppleDesktop 43 | Network Trash Folder 44 | Temporary Items 45 | .apdisk 46 | 47 | ### Windows ### 48 | # Windows thumbnail cache files 49 | Thumbs.db 50 | ehthumbs.db 51 | ehthumbs_vista.db 52 | 53 | # Dump file 54 | *.stackdump 55 | 56 | # Folder config file 57 | [Dd]esktop.ini 58 | 59 | # Recycle Bin used on file shares 60 | $RECYCLE.BIN/ 61 | 62 | # Windows Installer files 63 | *.cab 64 | *.msi 65 | *.msix 66 | *.msm 67 | *.msp 68 | 69 | # Windows shortcuts 70 | *.lnk 71 | 72 | 73 | # End of https://www.gitignore.io/api/linux,macos,windows -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 13 | 14 | # Git-Cheat-Sheet 15 | 16 | ## What is Git? 17 | 18 | [Git](https://github.com/git/git) is the most widely used modern version control system in the world today. 19 | Git is a mature, actively maintained open source project originally developed in 2005 by Linus Torvalds - 20 | the famous creator of the Linux kernel. 21 | 22 | ## What is this repo about? 23 | 24 | A cheatsheet for git commands. 25 | [`main.md`](./main.md) contains commonly used git commands. 26 | [`resources.md`](./resources.md) contains resources for learning git. 27 | 28 | ## Contributing 29 | 30 | Git-Cheat-Sheet welcomes any contribution. Please refer to our style guide for submitting patches and additions. 31 | 32 | ### Installation 33 | 1. Fork our repo from [here.](https://github.com/aSquare14/Git-Cheat-Sheet) 34 | 2. In the console, download a copy of your forked repo with `git clone https://github.com/your_github_username/Git-Cheat-Sheet.git`. 35 | 3. Enter the **Git-Cheat-Sheet** directory with `cd Git-Cheat-Sheet`. 36 | 4. Make changes to the file you want, commit your changes and submit your Pull Request. 37 | 38 | Note: Since this is an active project please rebase from master before submitting a Pull Request to avoid merge conflicts. 39 | 40 | ### Style guide 41 | How to add git commands in `main.md`: 42 | 43 | * **Case 1: The command's category already exists.** 44 | This should be the markdown format. 45 | ``` 46 | `$ git command` 47 | - Command Description 48 | ``` 49 | * **Case 2: The command's category does not exist.** 50 | This should be the markdown format. 51 | ``` 52 | ## Category 53 | `$ git command` 54 | - Command description. 55 | ``` 56 | 57 | ### Tracking Issues 58 | 59 | Please post any bugs, questions, or ideas on our 60 | [issues page](https://github.com/aSquare14/Git-Cheat-Sheet/issues). 61 | 62 | ### Labelling Issues 63 | 64 | If you create an issue, please tag it with the appropriate label. -------------------------------------------------------------------------------- /main.md: -------------------------------------------------------------------------------- 1 | # GIT CHEATSHEET 2 | 3 | ## Help and Documentation 4 | `$ git help ` 5 | - Display help information about Git. 6 | - With no options and no COMMAND or GUIDE given, the synopsis of the git command and a list of the most commonly used Git commands are printed on the standard output. 7 | - Note that `git --help ...` is identical to `git help ...` 8 | 9 | ## Converting a folder into a working Git repository 10 | 11 | - For projects using repositories on GitHub 12 | - `cd ` to change to the directory you want to use as a repository 13 | - `git init` to initialize the repository 14 | - `git add .` to prepare all of the local files to be sent to the remote repository 15 | - `git commit -m "message"` to name your commit 16 | - `git remote add origin ` to set the remote location of your repository 17 | - *This can be found on the primary Code tab of a GitHub project on the web. Repositories not hosted through services like GitHub or BitBucket require setting up a git daemon* 18 | - `git push -u origin master` to submit your commit to the remote repository 19 | 20 | ## Initializing Git 21 | These commands are related to the inizialitazion process for git repositories on your local machine. 22 | 23 | `$ git init` 24 | - It will create a new git repository in the current directory. 25 | 26 | `$ git init --bare` 27 | - It will create a new bare repository. 28 | 29 | A bare repository is a bit different from a regular one, it doesn't .git folder, history is stored in the project root, also if you try to `git clone --bare` from a remote repository (e.g. github) you will lost track of it's origin since usually bare repositories are supposed to be served at the users as remote endpoints. 30 | 31 | ## Cloning Repositories 32 | 33 | `$ git clone ` 34 | - Clones repository to local machine. 35 | 36 | ### Shallow cloning 37 | `$ git clone --depth=1` 38 | 39 | Git supports the notion of a “shallow clone”, which is a more succinctly meaningful way of describing a local repository with history truncated to a particular depth during the clone operation. By providing an argument of --depth 1 to the clone command, the process will copy only the latest revision of everything in the repository. This can be a lifesaver for Git servers that might otherwise be overwhelmed by CI/CD automation. 40 | 41 | ## Common local commands 42 | `$ git log` 43 | - Shows history of past commits 44 | 45 | `$ git log --oneline` 46 | - Shows history of past commits in summary which contains only commit id and commit message. 47 | 48 | `$ git blame ` 49 | - Shows what revision and author last modified each line of a file upto the last commit. 50 | 51 | `$ git status` 52 | - Shows the last modified files 53 | 54 | `$ git stash` 55 | - Git stash temporarily shelves or stashes changes made to your working copy so you can work on something else, and come back and re-apply them later on. 56 | 57 | `$ git stash save "message"` 58 | - Same as above but also annotates the stash with a description. This will be shown when running `$ git stash list` and helps provide context if you have multiple stashes at a time. 59 | 60 | `$ git stash pop` 61 | - Get back stashed commits 62 | 63 | `$ git stash pop stash@{}` 64 | - Pop only a specific index from the stash. You can find the index by running `git stash list`. 65 | 66 | `$ git stash list` 67 | - Lists all stashed changesets 68 | 69 | `$ git stash drop` 70 | - Discards the most recently stashed changeset 71 | 72 | `$ git commit --amend -m “updated commit msg”` 73 | - Updates commit message 74 | 75 | `$ git commit --amend --author “new author name ”` 76 | - Update the author of that commit 77 | 78 | ## How to commit changes to a particular branch . 79 | 80 | `$ git add .` or `git add -A` 81 | - Add all untracked files. 82 | 83 | `$ git commit -m “name of commit”` 84 | - Commit Files 85 | 86 | `$ git commit --amend (if you want to amend your commit message)` 87 | - If you want to edit your commit message. 88 | 89 | `$ git commit -a --amend -C HEAD` 90 | - Add all the untracked files to the last commit without changing the commit message. 91 | 92 | `$ git push origin ` 93 | - (eg: $ git push origin master). Push your changes. 94 | 95 | ## Commands related to branching 96 | It is a good practice to make a new branch for every new PR you make. Also,name the branches according to the work you are doing. It will be easier. 97 | 98 | `$ git checkout -b mybranch` 99 | - Creates a new branch named mybranch. 100 | 101 | `$ git checkout mybranch` 102 | - Move to a different branch. 103 | 104 | `$ git checkout -` 105 | - Return to the previously checked out branch. 106 | 107 | `$ git branch` 108 | - Check if you're on the right branch. Now,you’re on the new branch ! Commit your changes here. 109 | 110 | `$ git branch -a` 111 | - To list all the local and as well as the remote branches. 112 | 113 | `$ git fetch -p` 114 | - Delete local branches which doesn’t exist on remote anymore. 115 | 116 | `$ git branch -d branch-name` 117 | - Delete branch temporarily 118 | 119 | `$ git branch -D branch-name` 120 | - Delete branch permanently 121 | 122 | `$ git merge branch-name` 123 | - Merge branch with the current branch. 124 | 125 | `$ git mergetool` 126 | - Run merge conflict resolution tools to resolve merge conflicts. 127 | 128 | 129 | ## Renaming branch 130 | 131 | If you want to rename branch you have to run this set of commands. 132 | 133 | `$ git branch -m old_branch new_branch` 134 | - Rename branch locally. 135 | 136 | `$ git push origin :old_branch` 137 | - Then delete the old branch from origin. 138 | 139 | `$ git push --set-upstream origin new_branch` 140 | - Push the new branch and set local branch to track the new remote. 141 | 142 | 143 | ## Squashing X commits together 144 | 145 | `$ git rebase -i ` 146 | - Eg: ( $ git rebase -i HEAD~2 ) => (rebasing 2 commits starting from HEAD) 147 | - Then edit the ‘pick’ to ‘squash’ in front of all those commits which you want to squash. 148 | - Commit your new squashed commits. 149 | 150 | 151 | ## Squashing all commits up to root 152 | 153 | `$ git rebase -i --root master` 154 | - This will ‘squash’ all commits up to Initial (first) commit. 155 | 156 | 157 | ## Removing a commit from in between the commit history 158 | 159 | If you need to delete more than just the last commit use rebase. 160 | 161 | Example- 162 | 163 | ``` 164 | commit 1 2c6a45b Adding public method to access protected method(HEAD) 165 | commit 2 ae45fab Updates to database interface 166 | commit 3 77b9b82 Improving database interface 167 | ``` 168 | 169 | Using the git log above we want to remove the following commit; 2 (ae45fab). 170 | 171 | `$ git rebase --onto repair~2 repair~1 repair` 172 | 173 | - This command will delete commit 2 174 | 175 | Generalized Format for writing command is as follows: 176 | 177 | `$ git rebase --onto ~ ~ ` 178 | 179 | - Use rebase tool to rebase a series of commits onto the HEAD they were originally based on instead of moving them to another one. 180 | 181 | - Then give branch name along with first commit to be removed. 182 | 183 | - Then give branch name along with first commit to be kept. 184 | 185 | - Above command could also be used to remove one or more consecutive commits.For example if you want to remove commit 2&3, command would be as follows: 186 | 187 | ` $ git rebase --onto repair~3 repair~1 repair ` 188 | 189 | 190 | ## Going back to a previous commit in commit history 191 | `$ git reset --hard ` 192 | - The ^ symbol after HEAD defines the selected commit. HEAD is the current one, so for each '^' it goes back 1 commit in history before the current. 193 | - Note that this command will move the HEAD pointer to the specified commit and all uncommitted changes to files will be discarded. 194 | 195 | `$ git reset --soft ` 196 | - The ^ symbol after HEAD defines the selected commit. HEAD is the current one, so for each '^' it goes back 1 commit in history before the current. 197 | - Note that this command will move the HEAD pointer to the specified commit and all files that differ from the version in the selected commit will be moved to the staged area. 198 | 199 | 200 | ## Making sure your repository is up-to-date with the original/upstream repository 201 | Note: `< >` should not be included in commit message. Example, `git fetch upstream master`. 202 | 203 | `$ git remote add upstream ` 204 | - Note that "upstream" is the name I chose to give the repo, you can name it anything. 205 | 206 | `$ git fetch upstream ` 207 | - Fetch the latest changes. Alternatively, you can do `git pull upstream ` but it adds an extra merge commit. 208 | 209 | `$ git rebase upstream/` 210 | - Puts your changes on top. 211 | 212 | `$ git push origin --force` 213 | 214 | `$ git log` 215 | - To make sure rebase is done and you can see the commits. 216 | 217 | `git diff origin/master` 218 | - See differences between local changes and master 219 | 220 | 221 | ## Rebasing 222 | `$ git rebase --abort` 223 | - To quit the rebase process 224 | 225 | `$ git rebase --continue` 226 | - To finish the rebase process 227 | 228 | ## How to undo a mistaken git rebase 229 | `$ git reflog ` 230 | 231 | ``` 232 | >> 73d836b testBranch@{0}: rebase finished: refs/heads/testBranch onto e806e41f1fe22624e6546abd65c332c934214891 233 | 234 | >> 129e6d3 testBranch@{1}: commit: some sort of commit message 235 | ``` 236 | - Find the head commit of the branch before the rebase began, in this case `testBranch@{1}` 237 | 238 | `$ git reset --hard ` 239 | - Return to that commit using `git reset` 240 | - For example, in this case the command would be `$ git reset --hard testBranch@{1}` 241 | 242 | 243 | ## Checking the difference between any two particular commits 244 | 245 | `$ git diff ` 246 | - To check difference between any two commits by using their commit id. One can also use short git commit id which is provided by using `$ git log --oneline'` command 247 | 248 | `$ git diff ` 249 | - To check difference between the latest commit relative to a particular old commit. 250 | 251 | `$ git diff --cached ` 252 | - To check difference between changed staged for the next commit relative to a particular commit ie its `` or `HEAD` if relative to the latest commit. 253 | 254 | `$ git show --oneline HEAD` 255 | - To show which files were changed by the latest commit. 256 | 257 | ## Add gitignore to existing repo 258 | 259 | - [Adding gitignore to existing repo](https://stackoverflow.com/questions/19663093/apply-gitignore-on-an-existing-repository-already-tracking-large-number-of-files) 260 | 261 | ## Removing all files from git 262 | 263 | - [Removing files from git](https://stackoverflow.com/questions/2047465/how-can-i-delete-a-file-from-git-repo) 264 | -------------------------------------------------------------------------------- /resources.md: -------------------------------------------------------------------------------- 1 | #### This document contains resources to learn git. (Alphabetically arranged) 2 | 3 | ## Table of Contents 4 | - [Git Cheat Sheets](#git-cheat-sheets) 5 | - [Tutorials](#tutorial) 6 | - [Workflow](#workflow) 7 | 8 | ## Git Cheat Sheets 9 | 10 | Links to Git cheatsheets 11 | 12 | - [Atlassian Git Cheatsheet](https://www.atlassian.com/git/tutorials/atlassian-git-cheatsheet) 13 | - [GitHub Git Cheatsheet (PDF)](https://services.github.com/on-demand/downloads/github-git-cheat-sheet.pdf) 14 | - [GitLab Git Cheatsheet (PDF) ](https://about.gitlab.com/images/press/git-cheat-sheet.pdf) 15 | 16 | ## Tutorials 17 | 18 | Tons of learning materials on the Web. 19 | 20 | - [Atlassian](https://confluence.atlassian.com/get-started-with-sourcetree/work-using-git-847359053.html) 21 | - [Egghead Git tutorial](https://egghead.io/browse/tools/git) 22 | - [Git Branching](https://learngitbranching.js.org/) 23 | - [Git Handbook](https://guides.github.com/introduction/git-handbook/) 24 | - [Git-it](https://github.com/jlord/git-it-electron#what-to-install) 25 | - [Github Learning Lab](https://lab.github.com/) 26 | - [Git Official Document](https://git-scm.com/docs/user-manual.html) 27 | - [Git Tips](https://github.com/git-tips/tips) 28 | - [Git Tower's Cheatsheet](https://www.git-tower.com/blog/git-cheat-sheet) 29 | - [Katacoda Git Tutorial](https://www.katacoda.com/courses/git) 30 | - [Learn-git on CodeAcademy](https://www.codecademy.com/learn/learn-git) 31 | - [Learn Git in 30 minutes](https://tutorialzine.com/2016/06/learn-git-in-30-minutes) 32 | - [Oh shit, git!](https://ohshitgit.com/) 33 | - [Pluralsight Git Course](https://www.pluralsight.com/courses/code-school-git-real?gclid=EAIaIQobChMIlIu99OTn3QIVWgwrCh3SuAJKEAAYASAAEgI6JvD_BwE&aid=7010a000002BWq6AAG&promo=&oid=&utm_source=non_branded&utm_medium=digital_paid_search_google&utm_campaign=IN_Dynamic&utm_content=&s_kwcid=AL!5668!3!277681681323!b!!g!!&ef_id=WyW-tQAABZggfylD:20181002124627:s) 34 | - [ProGit Book](https://git-scm.com/book/en/v2) 35 | - [Visualizing Git](http://git-school.github.io/visualizing-git/) 36 | 37 | ## Workflow 38 | 39 | Helps you to improve your Git workflow. 40 | 41 | - [Atlassian's Git Workflow Comparisons](https://www.atlassian.com/git/tutorials/comparing-workflows) 42 | - [Introducing GitFlow](https://datasift.github.io/gitflow/IntroducingGitFlow.html) 43 | - [Git Distributed Workflow](https://git-scm.com/book/it/v2/Distributed-Git-Distributed-Workflows) 44 | - [Github Flow](http://scottchacon.com/2011/08/31/github-flow.html) 45 | - [GitLab Flow](https://about.gitlab.com/2014/09/29/gitlab-flow/) 46 | * [Resources to learn Git](https://try.github.io/) 47 | --------------------------------------------------------------------------------