|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: 8 Basic GIT Commands Every Newbie Developer Must Know |
| 4 | +author: gaurav |
| 5 | +categories: [ GIT ] |
| 6 | +description: GIT is one of the most important parts of the developer's day to day work. So learning GIT is a must for a newbie developer. In this article, you are going to learn the 8 most important basic GIT commands. |
| 7 | +featured: false |
| 8 | +--- |
| 9 | + |
| 10 | +GIT is one of the most important parts of the developer's day to day work. So learning GIT is a must for a newbie developer. In this article, you are going to learn the 8 most important basic GIT commands. |
| 11 | + |
| 12 | +Below, I have listed down all the 8 commands and then, we will have a look at them one by one. |
| 13 | + |
| 14 | +1. [`git init`](#git-init) |
| 15 | +2. [`git clone`](#git-clone) |
| 16 | +3. [`git add`](#git-add) |
| 17 | +4. [`git commit`](#git-commit) |
| 18 | +5. [`git status`](#git-status) |
| 19 | +6. [`git branch`](#git-branch) |
| 20 | +7. [`git pull`](#git-pull) |
| 21 | +8. [`git push`](#git-push) |
| 22 | + |
| 23 | +## 1. `git init` |
| 24 | +`git init` command initializes brand new GIT repository (locally) and begins tracking the existing directory. |
| 25 | + |
| 26 | +When you hit the `git init` command, git adds a subfolder withing an existing directory that manages all required files for version controlling. |
| 27 | + |
| 28 | +``` |
| 29 | +HP@Gaurav MINGW64 /e/example |
| 30 | +$ git init |
| 31 | +Initialized empty Git repository in E:/example/.git/ |
| 32 | +
|
| 33 | +HP@Gaurav MINGW64 /e/example (master) |
| 34 | +$ |
| 35 | +``` |
| 36 | + |
| 37 | +You are hitting `git init` means you want to initialize the current directory as GIT repository. |
| 38 | + |
| 39 | +The following gif files show initializing a new repository and a hidden subfolder containing all data structure required for version control. |
| 40 | + |
| 41 | + |
| 42 | + |
| 43 | +## 2. `git clone` |
| 44 | + |
| 45 | +`git clone` creates a local copy of a repository that already exists remotely. The local copy is the exact copy of the remote repository, it contains the same files, history, and branches. |
| 46 | + |
| 47 | +``` |
| 48 | +$ git clone <remote-repository-link> |
| 49 | +``` |
| 50 | +``` |
| 51 | +HP@Gaurav MINGW64 /e/directory |
| 52 | +$ git clone https://github.com/gauravkukade/example.git |
| 53 | +Cloning into 'example'... |
| 54 | +remote: Enumerating objects: 16, done. |
| 55 | +remote: Counting objects: 100% (16/16), done. |
| 56 | +remote: Compressing objects: 100% (10/10), done. |
| 57 | +remote: Total 16 (delta 3), reused 7 (delta 2), pack-reused 0 |
| 58 | +Unpacking objects: 100% (16/16), done. |
| 59 | +
|
| 60 | +HP@Gaurav MINGW64 /e/directory |
| 61 | +$ |
| 62 | +``` |
| 63 | + |
| 64 | +You can clone any public repository from the platforms like GitHub, BitBucket, GitLab, and other GIT hosting platforms. |
| 65 | + |
| 66 | +The followig gif shows the `git clone` command |
| 67 | + |
| 68 | + |
| 69 | + |
| 70 | +## 3. `git add` |
| 71 | + |
| 72 | +`git add` stages a change. |
| 73 | + |
| 74 | +If you are done with changes in your code then its necessary to stage that changes and take a snapshot of it to include it in the repository's history. |
| 75 | + |
| 76 | +`git add` does the first step, it stages a change. |
| 77 | + |
| 78 | +``` |
| 79 | +$ git add <path-of-the-file-you-made-changes-in> |
| 80 | +``` |
| 81 | +If you made changes in the multiple files and you want to stage all files in the same command then add the file path of all files separated y a single space. |
| 82 | + |
| 83 | +``` |
| 84 | +$ git add <first-filepath-you-want-to-stage> <second-filepath-you-want-to-stage> <nth-filepath-you-want-to-stage> |
| 85 | +``` |
| 86 | +If you want to stage all the files, then write '.' (dot) after `git add` |
| 87 | +``` |
| 88 | +$ git add . |
| 89 | +``` |
| 90 | +Any staged changes will become the part of the next snapshot and a part of the repository's history. |
| 91 | + |
| 92 | +You can stage and take a snapshot of the current changes in a single command also but is not recommended. |
| 93 | + |
| 94 | +Staging your changes first and then taking snapshot gives you complete control over the repository's history. |
| 95 | + |
| 96 | +The followig gif shows the `git add` command |
| 97 | + |
| 98 | + |
| 99 | + |
| 100 | +## 4. `git commit` |
| 101 | + `git commit` save the snapshot to the repository's history. |
| 102 | + |
| 103 | +`git add` does the first step i.e. staging the changes and `git commit` does the final step i.e. it saves the snapshot to the repository's history. In GIT these two-step completes the changes tracking process. |
| 104 | + |
| 105 | +``` |
| 106 | +$ git commit -m "<meaningful-git-commit-message> |
| 107 | +``` |
| 108 | + |
| 109 | +You can write a meaningful message to the commit. It is recommended to write a commit message in the 'Simple Present Tense'. |
| 110 | + |
| 111 | +If you are committing a new feature to your project then your commit message should be `"Add <feature-name> feature"`. |
| 112 | + |
| 113 | +The followig gif shows the `git commit` command |
| 114 | + |
| 115 | + |
| 116 | + |
| 117 | +It is the simple way to write the commit message but there is a more in-depth way to write a commit message with title and description. We will see that in a separate blogpost. |
| 118 | + |
| 119 | +![GIF showing git commit] |
| 120 | + |
| 121 | +## 5. `git status` |
| 122 | + |
| 123 | +`git status` shows the status of the changes as untracked, modified or staged. |
| 124 | +``` |
| 125 | +HP@Gaurav MINGW64 /e/directory/example (master) |
| 126 | +$ git status |
| 127 | +On branch master |
| 128 | +Your branch is up-to-date with 'origin/master'. |
| 129 | +
|
| 130 | +Changes to be committed: |
| 131 | + (use "git reset HEAD <file>..." to unstage) |
| 132 | +
|
| 133 | + modified: README.md |
| 134 | +
|
| 135 | +Untracked files: |
| 136 | + (use "git add <file>..." to include in what will be committed) |
| 137 | +
|
| 138 | + ex.txt |
| 139 | +
|
| 140 | +
|
| 141 | +HP@Gaurav MINGW64 /e/directory/example (master) |
| 142 | +$ |
| 143 | +
|
| 144 | +``` |
| 145 | +The followig gif shows the `git status` command |
| 146 | + |
| 147 | + |
| 148 | + |
| 149 | +## 6. `git branch` |
| 150 | +`git branch` list the existing branches from the local repository. The current branch will be highlighted in green and marked with an asterisk. |
| 151 | + |
| 152 | +``` |
| 153 | +HP@Gaurav MINGW64 /e/directory/example (master) |
| 154 | +$ git branch |
| 155 | +* master |
| 156 | + new_branch |
| 157 | +
|
| 158 | +HP@Gaurav MINGW64 /e/directory/example (master) |
| 159 | +$ |
| 160 | +``` |
| 161 | +The followig gif shows the `git branch` command |
| 162 | + |
| 163 | + |
| 164 | + |
| 165 | +## 7. `git pull` |
| 166 | + |
| 167 | +`git pull` update the local repository with updates from its remote counterpart. i.e. remote repository. |
| 168 | +``` |
| 169 | +HP@Gaurav MINGW64 /e/directory/example (master) |
| 170 | +$ git pull |
| 171 | +remote: Enumerating objects: 4, done. |
| 172 | +remote: Counting objects: 100% (4/4), done. |
| 173 | +remote: Compressing objects: 100% (3/3), done. |
| 174 | +remote: Total 3 (delta 0), reused 0 (delta 0), pack-reused 0 |
| 175 | +Unpacking objects: 100% (3/3), done. |
| 176 | +From https://github.com/gauravkukade/example |
| 177 | + 15e8755..d7aefb1 master -> origin/master |
| 178 | +Updating 15e8755..d7aefb1 |
| 179 | +Fast-forward |
| 180 | + new-file-added-to-remote-repository.txt | 1 + |
| 181 | + 1 file changed, 1 insertion(+) |
| 182 | + create mode 100644 new-file-added-to-remote-repository.txt |
| 183 | +
|
| 184 | +HP@Gaurav MINGW64 /e/directory/example (master) |
| 185 | +$ |
| 186 | +
|
| 187 | +``` |
| 188 | +If your teammate made commits to the remote branch and you want to reflect these changes in your local environment then you will hit the command `git pull`. |
| 189 | + |
| 190 | +This command will check if there are any updates on the remote branch as compared to your local environment, if yes, then it will update your local environment with these changes. If no, then it will do nothing. |
| 191 | + |
| 192 | +The followig gif shows the `git pull` command |
| 193 | + |
| 194 | + |
| 195 | + |
| 196 | +## 8. `git push` |
| 197 | + |
| 198 | +`git push` updates the remote repository with any commits made locally to a brach |
| 199 | + |
| 200 | +``` |
| 201 | +$ git push origin <branch-name-you-have made commits on> |
| 202 | +``` |
| 203 | +If the branch does not exist on a remote repository then the whole branch with its commits will be pushed to the remote repository. |
| 204 | +``` |
| 205 | +$ git push origin <newly-locally-created-branch-name> |
| 206 | +``` |
| 207 | +``` |
| 208 | +HP@Gaurav MINGW64 /e/directory/example (master) |
| 209 | +$ git push origin master |
| 210 | +fatal: HttpRequestException encountered. |
| 211 | + An error occurred while sending the request. |
| 212 | +Username for 'https://github.com': gauravkukade |
| 213 | +Counting objects: 2, done. |
| 214 | +Delta compression using up to 4 threads. |
| 215 | +Compressing objects: 100% (2/2), done. |
| 216 | +Writing objects: 100% (2/2), 255 bytes | 255.00 KiB/s, done. |
| 217 | +Total 2 (delta 1), reused 0 (delta 0) |
| 218 | +remote: Resolving deltas: 100% (1/1), completed with 1 local object. |
| 219 | +To https://github.com/gauravkukade/example.git |
| 220 | + d7aefb1..dc3d3ef master -> master |
| 221 | +
|
| 222 | +HP@Gaurav MINGW64 /e/directory/example (master) |
| 223 | +$ |
| 224 | +``` |
| 225 | +The followig gif shows the `git push` command |
| 226 | + |
| 227 | + |
| 228 | + |
| 229 | +You are here means you like the post and hence here is your bonus content. |
| 230 | + |
| 231 | +### How to create a new branch locally |
| 232 | + |
| 233 | +You can create a new branch loacally using the following command |
| 234 | +``` |
| 235 | +$ git checkout -b <your-new-branch-name> |
| 236 | +
|
| 237 | +``` |
| 238 | + |
| 239 | + |
| 240 | + |
| 241 | +If you found this article worth, please [Give me a cup of Coffee ☕](https://www.paypal.me/GauravKukade) |
| 242 | + |
| 243 | +If you have queries please let me know in the comment section below. |
0 commit comments