280536da96ad249616bb4196745f543e35195c79
Git/Git commands you must know.md
| ... | ... | @@ -0,0 +1,143 @@ |
| 1 | +https://towardsdev.com/git-commands-you-must-know-d217bf710502 |
|
| 2 | + |
|
| 3 | +# Git commands you must know | Ludivine Achouri | Towards Dev |
|
| 4 | +[ |
|
| 5 | + |
|
| 6 | + |
|
| 7 | + |
|
| 8 | + |
|
| 9 | + |
|
| 10 | +](https://medium.com/@la.dev?source=post_page---byline--d217bf710502--------------------------------) |
|
| 11 | + |
|
| 12 | +[ |
|
| 13 | + |
|
| 14 | + |
|
| 15 | + |
|
| 16 | + |
|
| 17 | + |
|
| 18 | +](https://towardsdev.com/?source=post_page---byline--d217bf710502--------------------------------) |
|
| 19 | + |
|
| 20 | +Photo by [Yancy Min](https://unsplash.com/@yancymin?utm_source=unsplash&utm_medium=referral&utm_content=creditCopyText) on [Unsplash](https://unsplash.com/) |
|
| 21 | + |
|
| 22 | +Git is a very important tool for any developer. I had a hard time understanding it at first, and I’m sure some of you are still a bit lost. |
|
| 23 | + |
|
| 24 | +So here is a rundown of 13 important git commands to better manage your workflow. There are many more commands available that you can find [here](https://git-scm.com/docs), but these are the most useful and frequently used ones and they will definitely speed up your coding process. |
|
| 25 | + |
|
| 26 | +If you are not sure to understand what Git is, or if maybe you don’t know the difference between Git and Github, I have an article explaining everything available [here](https://medium.com/p/cba1ce44e7d7/). |
|
| 27 | + |
|
| 28 | +1\. git clone |
|
| 29 | +------------- |
|
| 30 | + |
|
| 31 | +If you want to clone a remote repository use : **_git clone <repository url>_** |
|
| 32 | + |
|
| 33 | +2\. git init |
|
| 34 | +------------ |
|
| 35 | + |
|
| 36 | +To start a new project in an empty repository or to initialize an existing project, use this command in the project root : **_git init_** |
|
| 37 | + |
|
| 38 | +3\. git fetch |
|
| 39 | +------------- |
|
| 40 | + |
|
| 41 | +With **git fetch** you will get the latest updates of the repository, changes in the code and the new branches that were created. |
|
| 42 | + |
|
| 43 | +4\. git checkout |
|
| 44 | +---------------- |
|
| 45 | + |
|
| 46 | +With this command, you can switch branches : **_git checkout_** **_<branch name>_** |
|
| 47 | + |
|
| 48 | +To create a new branch and switch to it : **_git checkout -b_** **_<branch name>_** |
|
| 49 | + |
|
| 50 | +5\. git add |
|
| 51 | +----------- |
|
| 52 | + |
|
| 53 | +This command selects the files you updated, and that you wish to send to the remote repository. |
|
| 54 | + |
|
| 55 | +You can add a specific file with **_git add_** _<_**_file path>_** or add all the files with **_git add -A_** _or_ **_._** |
|
| 56 | + |
|
| 57 | +6\. git stash |
|
| 58 | +------------- |
|
| 59 | + |
|
| 60 | +If there are files that you don’t wish to commit now, you can set them aside or “stash” them. |
|
| 61 | + |
|
| 62 | +You first need to add them with the previous command, and then use **_git stash_**_._ |
|
| 63 | + |
|
| 64 | +To revert those changes, use **_git stash apply_** . |
|
| 65 | + |
|
| 66 | +7\. git commit |
|
| 67 | +-------------- |
|
| 68 | + |
|
| 69 | +Once you made some changes in the code and you saved it, you have to **commit** them. You are creating a little “container” of updates that you describe with a message : |
|
| 70 | + |
|
| 71 | +**_git commit -m_** **_“commit message”_** |
|
| 72 | + |
|
| 73 | +Example : **_git commit -m_** **_“added a new react component”_** |
|
| 74 | + |
|
| 75 | +8\. git push |
|
| 76 | +------------ |
|
| 77 | + |
|
| 78 | +This command will send your commits to the remote branch : **_git push_** |
|
| 79 | + |
|
| 80 | +9\. git revert |
|
| 81 | +-------------- |
|
| 82 | + |
|
| 83 | +You can revert commits you created. In order to do that, you will need to know the hash code of the commit. To get that hash code, use the command **_git log — oneline_.** |
|
| 84 | + |
|
| 85 | + |
|
| 86 | + |
|
| 87 | +This will display a list of commits you previously created alongside a little code, that’s what you are looking for. |
|
| 88 | + |
|
| 89 | +If I wanted to revert that first commit, I will use the command **_git revert 7804f7c._** |
|
| 90 | + |
|
| 91 | +Exit the screen that will appear with **shift + q** or type **:qa** in the terminal. A new commit will created reverting the last changes without deleting the previous commit. |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + |
|
| 95 | +10\. git pull |
|
| 96 | +------------- |
|
| 97 | + |
|
| 98 | +Using **_git pull_** will fetch all the changes from the remote repository and merge any remote changes in the current local branch. |
|
| 99 | + |
|
| 100 | +11\. git branch |
|
| 101 | +--------------- |
|
| 102 | + |
|
| 103 | +**_git branch_** gives you the list of all the branches of the repository. |
|
| 104 | + |
|
| 105 | +You can also create new branches like this : |
|
| 106 | + |
|
| 107 | +**_git branch <new branch name>_** |
|
| 108 | + |
|
| 109 | +You can delete a branch as well : |
|
| 110 | + |
|
| 111 | +**_git branch -d <branch name>_** |
|
| 112 | + |
|
| 113 | +12\. git status |
|
| 114 | +--------------- |
|
| 115 | + |
|
| 116 | +The **_git status_** command gives you information about the current branch. |
|
| 117 | + |
|
| 118 | +You will know if the current branch is up to date, if you need to **push**, or **pull,** if content is **staged, unstaged** or **untracked** and if files were **created, modified** or **deleted.** |
|
| 119 | + |
|
| 120 | +13\. git merge |
|
| 121 | +-------------- |
|
| 122 | + |
|
| 123 | +**Git merge** is the command that “merges” or combine branches commits. Once you are done working on your branch, the last thing to do is to merge your branch with the main branch. |
|
| 124 | + |
|
| 125 | +First you need to be on the branch you want your branch to be merged into. |
|
| 126 | + |
|
| 127 | +**Example** : If you want to merge your branch **_“my-new-feature”_** into the branch called ”**main**”, you need to checkout into **_“main”_** and run the command here. |
|
| 128 | + |
|
| 129 | +If you don’t remember how to switch branches, type : |
|
| 130 | + |
|
| 131 | +**_git checkout main_** |
|
| 132 | + |
|
| 133 | +Before merging branches you should **_always_** fetch the latest updates: |
|
| 134 | + |
|
| 135 | +**_git fetch_** |
|
| 136 | + |
|
| 137 | +Now, you can merge you branch into the main branch : |
|
| 138 | + |
|
| 139 | +**_git merge my-new-feature_** |
|
| 140 | + |
|
| 141 | +Knowing these commands is very important, but I also advise you to use a software to manage git. [Github Desktop](https://desktop.github.com/) is available for free to handle your GitHub repositories. You can find other alternatives like [GitKraken](https://www.gitkraken.com/). |
|
| 142 | + |
|
| 143 | +I hope my article helped you understand all of this a little bit more ! Please don’t hesitate to ask questions if you need ! |
|
| ... | ... | \ No newline at end of file |