|
| 1 | + |
| 2 | + |
| 3 | +*The git commands & workflows I use on daily basis.* |
| 4 | + |
| 5 | +<br /> |
| 6 | + |
| 7 | +<img src="./images/suitcase.png" width="10%" /> |
| 8 | + |
| 9 | +## Initialization |
| 10 | + |
| 11 | +```sh |
| 12 | +# paste this in your terminal to change your current working directory to the project directory |
| 13 | +cd your_project_path |
| 14 | + |
| 15 | +# initialize git |
| 16 | +git init |
| 17 | +``` |
| 18 | + |
| 19 | +<br /> |
| 20 | + |
| 21 | +<img src="./images/rocket.png" width="10%" /> |
| 22 | + |
| 23 | +## Commands |
| 24 | + |
| 25 | +⚡️ ***The repetitive commands that I (and everyone else) use regularly.*** |
| 26 | + |
| 27 | +```sh |
| 28 | + |
| 29 | +# connect the remote repo with your local project |
| 30 | +git remote add origin [-repo-url] |
| 31 | + |
| 32 | +# add all untracked files to the staging area |
| 33 | +git add . |
| 34 | + |
| 35 | +# commit the tracked files of the staging area |
| 36 | +git commit -m "commit msg" |
| 37 | + |
| 38 | +# push all the changes to the |
| 39 | +git push -u origin master |
| 40 | + |
| 41 | +``` |
| 42 | + |
| 43 | +🎩 ***Clone a repository in your computer.*** |
| 44 | + |
| 45 | +```sh |
| 46 | +# clone a repo |
| 47 | +git clone [repo_url] |
| 48 | +``` |
| 49 | + |
| 50 | +🌲 ***The git commands you need to know to work with branches.*** |
| 51 | + |
| 52 | +```sh |
| 53 | + |
| 54 | +# list all branches |
| 55 | +git branch |
| 56 | + |
| 57 | +# create a new branch |
| 58 | +git branch [branch_name] |
| 59 | + |
| 60 | +# checkout to the new branch |
| 61 | +git checkout [branch_name] |
| 62 | + |
| 63 | +# OR |
| 64 | + |
| 65 | +# create AND checkout to the new branch |
| 66 | +git checkout -b [branch_name] |
| 67 | + |
| 68 | +# pushing the new branch on |
| 69 | +git push origin [branch_name] |
| 70 | + |
| 71 | +# delete a branch |
| 72 | +git branch -d [branch_name] |
| 73 | + |
| 74 | +``` |
| 75 | + |
| 76 | +🎯 ***Keep your forked repo in sync with the original repository.*** |
| 77 | + |
| 78 | +```sh |
| 79 | + |
| 80 | +# STEP #1: show URLs of remote repositories when listing your current remote connections |
| 81 | +git remote -v |
| 82 | + |
| 83 | +# STEP #2: add upstream |
| 84 | +git remote add upstream [source-repo-url] |
| 85 | + |
| 86 | +# STEP #3: fetching all the new changes from the main repository |
| 87 | +git fetch upstream |
| 88 | + |
| 89 | +# STEP #4: merging the new changes from the original repo to your forked local repo |
| 90 | +git merge upstream/master |
| 91 | + |
| 92 | +# STEP #5: pushing the new changes of forked local repo to the |
| 93 | +git push origin master |
| 94 | + |
| 95 | +``` |
| 96 | + |
| 97 | +<br/> |
| 98 | + |
| 99 | +<img src="./images/workflow.png" width="10%" /> |
| 100 | + |
| 101 | +## Workflows |
| 102 | + |
| 103 | +Open your `.zshrc` or `.bashrc` file. It is located in your root directory. Paste the following shell code there. |
| 104 | + |
| 105 | +```sh |
| 106 | + |
| 107 | +# Keep your forked repo in sync with the original repository with master as the primary branch |
| 108 | +function fetchremotems() { |
| 109 | +git fetch upstream && |
| 110 | +git merge upstream/master && |
| 111 | +git push origin master |
| 112 | +} |
| 113 | + |
| 114 | +# Keep your forked repo in sync with the original repository with master as the primary branch |
| 115 | +function fetchremotemn() { |
| 116 | +git fetch upstream && |
| 117 | +git merge upstream/master && |
| 118 | +git push origin master |
| 119 | +} |
| 120 | + |
| 121 | +# create new branch and checkout to it |
| 122 | +function gcb() { |
| 123 | +git checkout -b "${1}" |
| 124 | +} |
| 125 | + |
| 126 | +# checkout to a branch |
| 127 | +function gc() { |
| 128 | +git checkout "${1}" |
| 129 | +} |
| 130 | + |
| 131 | +# push changes to another branch |
| 132 | +function gbp() { |
| 133 | +git push origin "${1}" |
| 134 | +} |
| 135 | + |
| 136 | +# add, commit, push changes to |
| 137 | +function gacp() { |
| 138 | +git add . && |
| 139 | +git commit -m "${1}" |
| 140 | +git push |
| 141 | +} |
| 142 | + |
| 143 | +``` |
| 144 | + |
| 145 | +💥 ***Fetching changes from the original repo to your forked repo.*** |
| 146 | + |
| 147 | +```sh |
| 148 | + |
| 149 | +cd your_project_path |
| 150 | + |
| 151 | +# do this only once in every forked local repo to add upstream |
| 152 | +git remote add upstream [source-repo-url] |
| 153 | + |
| 154 | +# write the following in the terminal – primary branch: master – whenever you need to fetch the changes |
| 155 | +fetchremotems |
| 156 | + |
| 157 | +# write the following in the terminal – primary branch: main – whenever you need to fetch the changes |
| 158 | +fetchremotemn |
| 159 | + |
| 160 | +``` |
| 161 | + |
| 162 | +🎲 ***Usage of the rest of the workflows.*** |
| 163 | + |
| 164 | +```sh |
| 165 | + |
| 166 | +# To create a new branch and also to checkout to it |
| 167 | +gcb [branch_name] |
| 168 | + |
| 169 | +# To checkout to an existing branch |
| 170 | +gc [branch_name] |
| 171 | + |
| 172 | +# To push changes to another branch |
| 173 | +gbp [branch_name] |
| 174 | + |
| 175 | +# commit |
| 176 | + |
| 177 | +gacp "commit msg" |
| 178 | + |
| 179 | +``` |
| 180 | + |
| 181 | +` NOTE ` I extensively use [Emoji-log](https://.com/ahmadawais/emoji-log) by [Ahmad Awais](http://.com/ahmadawais) in my commit msgs. You should also take a look at its [workflows](https://.com/ahmadawais/emoji-log###THE%20WORKFLOW%20&%20MEANINGS). |
| 182 | + |
| 183 | +## 👨🏻💻 Contributing |
| 184 | + |
| 185 | +Feel free to add your git workflows in the repository. Just make sure you first read the [contributing guidelines](https://.com/msaaddev/git-commands-workflows/blob/master/contributing.md) before making a PR. |
| 186 | + |
| 187 | +## 🔑 License |
| 188 | + |
| 189 | +- [MIT](https://.com/msaaddev/git-commands-workflows/blob/master/LICENSE) |
0 commit comments