|
| 1 | +# Git Commands Cheat Sheet |
| 2 | + |
| 3 | +--- |
| 4 | + |
| 5 | +## Configuration |
| 6 | + |
| 7 | +### Set up global config |
| 8 | + |
| 9 | +- Set your name: |
| 10 | + `git config --global user.name "Your Name"` |
| 11 | +- Set your email: |
| 12 | + `git config --global user.email "you@example.com"` |
| 13 | +- Check current settings: |
| 14 | + `git config --list` |
| 15 | + |
| 16 | +--- |
| 17 | + |
| 18 | +## Repository Management |
| 19 | + |
| 20 | +### Initialize and Clone |
| 21 | + |
| 22 | +- Initialize a new Git repository: |
| 23 | + `git init` |
| 24 | +- Clone an existing repository: |
| 25 | + `git clone <repository_url>` |
| 26 | + |
| 27 | +--- |
| 28 | + |
| 29 | +## Staging and Committing |
| 30 | + |
| 31 | +### Adding and Committing Changes |
| 32 | + |
| 33 | +- Add a specific file to the staging area: |
| 34 | + `git add file.txt` |
| 35 | +- Add all changes to the staging area: |
| 36 | + `git add .` |
| 37 | +- Commit changes with a message: |
| 38 | + `git commit -m "Your commit message"` |
| 39 | +- Commit all changes directly (skip staging): |
| 40 | + `git commit -a -m "Your commit message"` |
| 41 | + |
| 42 | +--- |
| 43 | + |
| 44 | +## Branch Management |
| 45 | + |
| 46 | +### Working with Branches |
| 47 | + |
| 48 | +- List all branches: |
| 49 | + `git branch` |
| 50 | +- Create a new branch: |
| 51 | + `git branch new-branch` |
| 52 | +- Switch to a branch: |
| 53 | + `git checkout branch-name` |
| 54 | +- Create and switch to a branch: |
| 55 | + `git checkout -b new-branch` |
| 56 | +- Rename the current branch: |
| 57 | + `git branch -m new-name` |
| 58 | +- Delete a branch: |
| 59 | + `git branch -d branch-name` |
| 60 | + |
| 61 | +--- |
| 62 | + |
| 63 | +## Merging and Rebasing |
| 64 | + |
| 65 | +### Combining Changes |
| 66 | + |
| 67 | +- Merge a branch into the current branch: |
| 68 | + `git merge branch-name` |
| 69 | +- Rebase the current branch onto another branch: |
| 70 | + `git rebase branch-name` |
| 71 | + |
| 72 | +--- |
| 73 | + |
| 74 | +## Remote Repositories |
| 75 | + |
| 76 | +### Connecting to a Remote |
| 77 | + |
| 78 | +- Add a remote repository: |
| 79 | + `git remote add origin <repository_url>` |
| 80 | +- List remote repositories: |
| 81 | + `git remote -v` |
| 82 | +- Remove a remote repository: |
| 83 | + `git remote remove origin` |
| 84 | + |
| 85 | +### Pushing and Pulling |
| 86 | + |
| 87 | +- Push changes to a remote repository: |
| 88 | + `git push origin branch-name` |
| 89 | +- Pull changes from a remote repository: |
| 90 | + `git pull origin branch-name` |
| 91 | +- Push all branches to the remote: |
| 92 | + `git push --all origin` |
| 93 | +- Push tags to the remote: |
| 94 | + `git push --tags` |
| 95 | + |
| 96 | +--- |
| 97 | + |
| 98 | +## Status and Logs |
| 99 | + |
| 100 | +### Checking Repository State |
| 101 | + |
| 102 | +- Check the status of the working directory: |
| 103 | + `git status` |
| 104 | +- View commit history: |
| 105 | + `git log` |
| 106 | +- View a simplified commit history: |
| 107 | + `git log --oneline` |
| 108 | +- View changes in files: |
| 109 | + `git diff` |
| 110 | + |
| 111 | +--- |
| 112 | + |
| 113 | +## Stash Management |
| 114 | + |
| 115 | +### Save and Restore Work in Progress |
| 116 | + |
| 117 | +- Stash changes: |
| 118 | + `git stash` |
| 119 | +- List stashed changes: |
| 120 | + `git stash list` |
| 121 | +- Apply the most recent stash: |
| 122 | + `git stash apply` |
| 123 | +- Apply and remove the most recent stash: |
| 124 | + `git stash pop` |
| 125 | +- Clear all stashes: |
| 126 | + `git stash clear` |
| 127 | + |
| 128 | +--- |
| 129 | + |
| 130 | +## Tagging |
| 131 | + |
| 132 | +### Annotating Versions |
| 133 | + |
| 134 | +- Create a lightweight tag: |
| 135 | + `git tag tag-name` |
| 136 | +- Create an annotated tag: |
| 137 | + `git tag -a tag-name -m "Tag message"` |
| 138 | +- Push tags to a remote: |
| 139 | + `git push origin tag-name` |
| 140 | +- List all tags: |
| 141 | + `git tag` |
| 142 | +- Delete a tag: |
| 143 | + `git tag -d tag-name` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +## Undoing Changes |
| 148 | + |
| 149 | +### Revert, Reset, and Clean |
| 150 | + |
| 151 | +- Undo the last commit (keep changes): |
| 152 | + `git reset HEAD~1` |
| 153 | +- Undo the last commit (discard changes): |
| 154 | + `git reset --hard HEAD~1` |
| 155 | +- Remove untracked files: |
| 156 | + `git clean -f` |
| 157 | +- Revert a commit: |
| 158 | + `git revert commit-hash` |
| 159 | + |
| 160 | +--- |
| 161 | + |
| 162 | +## Collaboration |
| 163 | + |
| 164 | +### Resolving Conflicts |
| 165 | + |
| 166 | +- Start a merge with conflicts: |
| 167 | + `git merge branch-name` |
| 168 | +- Resolve conflicts in files, then mark resolved: |
| 169 | + `git add file.txt` |
| 170 | +- Continue after resolving conflicts: |
| 171 | + `git merge --continue` |
| 172 | + |
| 173 | +--- |
| 174 | + |
| 175 | +## Advanced Commands |
| 176 | + |
| 177 | +### Cherry-Pick and Bisect |
| 178 | + |
| 179 | +- Apply a specific commit to the current branch: |
| 180 | + `git cherry-pick commit-hash` |
| 181 | +- Start a bisect to find a buggy commit: |
| 182 | + `git bisect start` |
| 183 | +- Mark the current commit as good: |
| 184 | + `git bisect good` |
| 185 | +- Mark the current commit as bad: |
| 186 | + `git bisect bad` |
| 187 | + |
| 188 | +### Squash Commits |
| 189 | + |
| 190 | +- Interactively rebase to squash commits: |
| 191 | + `git rebase -i HEAD~n` |
| 192 | + |
| 193 | +--- |
| 194 | + |
| 195 | +## Aliases |
| 196 | + |
| 197 | +### Create Custom Shortcuts |
| 198 | + |
| 199 | +- Create a custom alias: |
| 200 | + `git config --global alias.co checkout` |
| 201 | +- Use the alias: |
| 202 | + `git co branch-name` |
| 203 | + |
| 204 | +--- |
| 205 | + |
| 206 | +## Tips and Tricks |
| 207 | + |
| 208 | +### Useful Options |
| 209 | + |
| 210 | +- Clone only the latest commit: |
| 211 | + `git clone --depth 1 <repository_url>` |
| 212 | +- List files changed in a commit: |
| 213 | + `git show --name-only commit-hash` |
| 214 | +- Show changes for a specific file: |
| 215 | + `git log -p file.txt` |
0 commit comments