|
| 1 | + |
| 2 | +# Git Commands |
| 3 | + |
| 4 | +Here's a list of basic Git commands that I use daily for dev. |
| 5 | + |
| 6 | +## Clone |
| 7 | +Starting with the basics, this command makes a copy of the remote to the local machine. |
| 8 | + |
| 9 | +```bash |
| 10 | +git clone <repo:url> |
| 11 | +``` |
| 12 | +(The repo looks like: https://github.com/ajayns/github-cmds) |
| 13 | + |
| 14 | +## Commit and Push |
| 15 | +This set of commands is used to make changes, save them and push them to the remote, better explained step by step: |
| 16 | + |
| 17 | +First, add the files to git |
| 18 | +```bash |
| 19 | +git add . |
| 20 | +``` |
| 21 | +(`.` will add all files and folders, so file names in its place can be used to add files specifically) |
| 22 | + |
| 23 | +Commit the changes |
| 24 | +```bash |
| 25 | +git commit -m 'Add commit message here' |
| 26 | +``` |
| 27 | +(A commit message is a brief account of changes made, like 'Minor fixes', 'Added login component') |
| 28 | + |
| 29 | +Add the remote to the Git repo |
| 30 | +```bash |
| 31 | +git remote add origin <repo:url> |
| 32 | +``` |
| 33 | + |
| 34 | +Push the changes to the remote |
| 35 | +```bash |
| 36 | +git push -u origin master |
| 37 | +``` |
| 38 | +(`origin` can be replaced with other remotes depending where you want to push to. `master` refers the main branch, it can be changed to whichever branch you'd like to push) |
| 39 | + |
| 40 | +Pushing commits needn't be done everytime you make a commit, instead, multiple commits can be push together from local to remote. |
| 41 | + |
| 42 | +## Branch |
| 43 | + |
| 44 | +Create a new branch from current one |
| 45 | +```bash |
| 46 | +git branch <branch-name> |
| 47 | +``` |
| 48 | + |
| 49 | +Switch to a branch |
| 50 | +```bash |
| 51 | +git checkout <branch-name> |
| 52 | +``` |
| 53 | + |
| 54 | +Create new branch and switch to it |
| 55 | +```bash |
| 56 | +git checkout -b <branch-name> |
| 57 | +``` |
| 58 | + |
| 59 | +Delete a branch |
| 60 | +```bash |
| 61 | +git branch -d <branch-name> |
| 62 | +``` |
| 63 | + |
| 64 | +Merge a branch to master, |
| 65 | +```bash |
| 66 | +git checkout master |
| 67 | +``` |
| 68 | +(making sure you are in the master branch) |
| 69 | +```bash |
| 70 | +git merge <branch-name> |
| 71 | +``` |
| 72 | + |
| 73 | +## Syncing a forked repo |
| 74 | +Clone the remote repo to local system and the following setups will bring the local and remote repos up to date with the source repo. |
| 75 | + |
| 76 | +Add the source remote (where you cloned from) to git, calling it upstream |
| 77 | +```bash |
| 78 | +git remote add upstream <repo:url> |
| 79 | +``` |
| 80 | + |
| 81 | +Fetch all branches from upstream |
| 82 | +```bash |
| 83 | +git fetch upstream |
| 84 | +``` |
| 85 | + |
| 86 | +Switch to master branch |
| 87 | +```bash |
| 88 | +git checkout master |
| 89 | +``` |
| 90 | + |
| 91 | +Rewrite your master branch so that any commits of yours that aren't already in upstream/master are replayed on top of that other branch: |
| 92 | +```bash |
| 93 | +git rebase master |
| 94 | +``` |
| 95 | +(These same steps can be done for branches other than master also) |
| 96 | + |
| 97 | +Finally, push the repo to your forked remote to make it up to date |
| 98 | +```bash |
| 99 | +git push -f origin master |
| 100 | +``` |
| 101 | +(Use the `-f` only when first rebase) |
| 102 | + |
| 103 | + |
| 104 | +## Delete Changes |
| 105 | +To delete all changes made to repo since last commit |
| 106 | +```bash |
| 107 | +git stash -u |
| 108 | +``` |
| 109 | + |
0 commit comments