|
| 1 | +# Git |
| 2 | + |
| 3 | +## Setup |
| 4 | + |
| 5 | +```bash |
| 6 | +# Check git version |
| 7 | +git --version |
| 8 | + |
| 9 | +# Set username and email |
| 10 | +git config --global user.name "Your Name" |
| 11 | +git config --global user.email "[email protected]" |
| 12 | + |
| 13 | +# Set default branch as main |
| 14 | +git config --global init.defaultBranch main |
| 15 | + |
| 16 | +# Set automatic command line colouring for git |
| 17 | +git config --global color.ui auto |
| 18 | + |
| 19 | +# List all global git config variables |
| 20 | +git config --list |
| 21 | + |
| 22 | +# ssh setup |
| 23 | +# Add id_ed25519.pub as ssh key on Github |
| 24 | +# Path in Linux : ~/.ssh/id_ed25519.pub |
| 25 | +# Path in Windows : /c/Users/you/.ssh/id_ed25519.pub |
| 26 | +ssh-keygen -t ed25519 -C "[email protected]" |
| 27 | + |
| 28 | +# OR |
| 29 | + |
| 30 | +# Store credentials |
| 31 | +git config --global credential.helper store |
| 32 | +``` |
| 33 | + |
| 34 | +## Start |
| 35 | + |
| 36 | +```bash |
| 37 | +# Create a local git repository |
| 38 | +cd [path] |
| 39 | +git init |
| 40 | +``` |
| 41 | + |
| 42 | +## Stage and Snapshot |
| 43 | + |
| 44 | +```bash |
| 45 | +# Check the status of a repository. (View unstaged, staged and modified files) |
| 46 | +git status |
| 47 | + |
| 48 | +# Add a file from working area to staging area |
| 49 | +git add [path] |
| 50 | + |
| 51 | +# Create a snapshot of your staged changes |
| 52 | +git commit -m "[descriptive message]" |
| 53 | + |
| 54 | +# Change the commit message of last commit |
| 55 | +git commit --amend -m "[new message]" |
| 56 | + |
| 57 | +# Display the difference of what is changed but not staged |
| 58 | +git diff |
| 59 | + |
| 60 | +# Display the difference of what is staged but not commited |
| 61 | +git diff --staged |
| 62 | + |
| 63 | +# Unstage a file while retaining the changes in working directory |
| 64 | +git reset [file] |
| 65 | +``` |
| 66 | + |
| 67 | +## Branch & Merge |
| 68 | + |
| 69 | +```bash |
| 70 | +# List your branches |
| 71 | +git branch |
| 72 | + |
| 73 | +# Create a new branch at the current commit |
| 74 | +git branch [branch_name] |
| 75 | + |
| 76 | +# Switch to another branch |
| 77 | +git checkout [branch_name] |
| 78 | + |
| 79 | +# Create and swtich to the branch |
| 80 | +git checkout -b [branch_name] |
| 81 | + |
| 82 | +# Merge the specified branch's history into the current one |
| 83 | +git merge [branch_name] |
| 84 | + |
| 85 | +# Show commit history of current branch |
| 86 | +git log |
| 87 | +``` |
| 88 | + |
| 89 | +## Remote repositories |
| 90 | + |
| 91 | +```bash |
| 92 | +# Clone a remote repository |
| 93 | +git clone [url] |
| 94 | + |
| 95 | +# Fetch the meta-data of remote repository |
| 96 | +git fetch |
| 97 | + |
| 98 | +# Transmit local changes to remote repository |
| 99 | +git push |
| 100 | + |
| 101 | +# Fetch and merge any commits from the remote branch |
| 102 | +git pull |
| 103 | +``` |
| 104 | + |
| 105 | +## Rewrite history |
| 106 | + |
| 107 | +```bash |
| 108 | +# Apply commits of current branch ahead of specified one |
| 109 | +git rebase [branch_name] |
| 110 | + |
| 111 | +# Reset to a specified commit and clear the staging area |
| 112 | +git reset --hard [commit] |
| 113 | + |
| 114 | +# Reset to a specified commit without clearing the staging area |
| 115 | +git reset --soft [commit] |
| 116 | +``` |
| 117 | + |
| 118 | +## Ignoring patterns |
| 119 | + |
| 120 | +```bash |
| 121 | +# Add files that you want to ignore in .gitignore file |
| 122 | +*.txt |
| 123 | +node_modules |
| 124 | +``` |
| 125 | + |
| 126 | +## Temporary commits |
| 127 | + |
| 128 | +```bash |
| 129 | +# Save modified and staged changes |
| 130 | +git stash |
| 131 | + |
| 132 | +# List stashes |
| 133 | +git stash list |
| 134 | + |
| 135 | +# Apply changes from top stash stack |
| 136 | +git stash apply |
| 137 | + |
| 138 | +# Drop changes from top stash stack |
| 139 | +git stash drop |
| 140 | + |
| 141 | +# Apply and drop from top stash stack |
| 142 | +git stash pop |
| 143 | +``` |
| 144 | + |
| 145 | +--- |
| 146 | + |
| 147 | +# Extras |
| 148 | + |
| 149 | + |
| 150 | + |
| 151 | + |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + |
0 commit comments