└── git-commands.txt /git-commands.txt: -------------------------------------------------------------------------------- 1 | What is Source code management tool? 2 | 3 | - Version Control: It allows teams of developers to keep track of changes to their codebase. 4 | 5 | - Collaboration: It enables multiple developers to work together on the same project. 6 | 7 | - Continuous Integration/Continuous Deployment (CI/CD) 8 | It integrates with various CI/CD tools (like GitHub Actions, Jenkins, and Travis CI) to automate testing, building, and deploying your code. 9 | 10 | Example: 11 | 12 | 1. Git hub 13 | 2. Bit Bucket 14 | 3. Clear case 15 | 4. SVN 16 | 17 | ________________________ 18 | Git Hub and git 19 | ________________________ 20 | 21 | 1. Create git hub account 22 | 2. Install git in local system 23 | 3. Configure name & email using following commands (One time configuration) 24 | 25 | git config --global user.name "" 26 | git config --gloabl user.email "" 27 | 28 | Let's go over the explanation of Git Architecture in detail: 29 | 30 | 1) Working Tree 31 | The Working Tree (also called the Working Directory or Workspace) is where all the files for your project are stored on your local machine. 32 | 33 | 2) Staging Area 34 | The Staging Area (also called the Index) is where files are prepared before being committed to the repository. 35 | When you make changes in your working directory and want to save them to the local repository, you first add them to the staging area using the git add command. 36 | Once files are in the staging area, they are ready to be committed. The staging area holds a snapshot of your changes and prepares them for the next commit. 37 | In short, it's a "pre-commit" area where you gather all the changes you want to be part of the next commit. 38 | 39 | 3) Local Repository 40 | The Local Repository is the place where Git stores all your project’s history. It's stored on your local machine. 41 | When you commit changes (git commit), the changes are recorded into your local repository. 42 | This local repository contains a .git folder that holds all of the project’s version history, configuration settings, and other important Git-related data. 43 | Commits in the local repository allow you to revert to previous versions, branch out, and compare changes locally without affecting the central repository. 44 | 45 | 4) Central Repository 46 | The Central Repository (also called the Remote Repository) is usually hosted on a platform like GitHub, GitLab, or Bitbucket. 47 | It is shared by multiple contributors and serves as the main repository that everyone pulls from and pushes to. 48 | Developers clone this repository to create their local copies and regularly push their commits to share their changes, as well as pull others' changes from the central repository. 49 | This central repository is accessible to all team members. 50 | 51 | 52 | 53 | 54 | ############## 55 | Git Commands 56 | ############# 57 | 58 | 1. git init 59 | ________________ 60 | Purpose: Initializes a new Git repository in the current directory. 61 | 62 | Usage: You run this command in a project folder to convert it into a Git repository. It creates a .git directory where Git stores all its configuration files and version history. 63 | 64 | Example: 65 | git init 66 | ________________________________________________________________________________________________________________________ 67 | 2. git status 68 | _______________ 69 | Purpose: Shows the current status of the working directory and staging area. 70 | Usage: This command is used to check whether there are any modified, added, or deleted files in the working directory that need to be staged or committed. 71 | 72 | git status 73 | 74 | It will show if files are untracked, modified, or staged. 75 | 76 | ________________________________________________________________________________________________________________________ 77 | 78 | 3 git add 79 | _______________ 80 | 81 | Purpose: Adds files or changes to the staging area (index) in preparation for a commit. 82 | Usage: You specify the file(s) to add or use a wildcard (e.g., git add . to add all files). 83 | 84 | 85 | git add test.txt 86 | git add . # To add all changes 87 | ________________________________________________________________________________________________________________________ 88 | 89 | 4 git commit 90 | _______________ 91 | Purpose: Commits the staged files to the local repository, saving your changes along with a commit message. 92 | Usage: This records your staged changes and adds them to the local repository’s history. 93 | 94 | git commit -m "Commit message describing changes" 95 | ________________________________________________________________________________________________________________________ 96 | 97 | 5) git push 98 | _______________ 99 | Purpose: Pushes the committed changes from the local repository to the central repository (remote repo like GitHub). 100 | Usage: After committing locally, use this to share your changes with others by sending them to the central repository. 101 | 102 | git push origin main # Push changes to the "main" branch 103 | ________________________________________________________________________________________________________________________ 104 | 105 | 6) git restore 106 | _______________ 107 | Purpose: Used to undo changes in the working directory and the staging area. 108 | Usage: 109 | If a file is unstaged, git restore will discard local changes made to that file. 110 | If a file is staged, git restore --staged will unstage the file but keep the changes in the working directory. 111 | Examples: 112 | 113 | git restore test.txt 114 | Unstage a staged file (without discarding changes): 115 | 116 | git restore --staged test.txt 117 | 118 | ________________________________________________________________________________________________________________________ 119 | 7) git log 120 | __________ 121 | Purpose: Displays the commit history, including commit IDs, author information, dates, and commit messages. 122 | Usage: This is useful to view the history of changes made to the project. 123 | 124 | git log 125 | 126 | To view a simpler summary, you can add options: 127 | 128 | git log --oneline 129 | 130 | ________________________________________________________________________________________________________________________ 131 | 8) git rm 132 | _______________ 133 | Purpose: Removes a file from both the working directory and the staging area, and it schedules the deletion for the next commit. 134 | Usage: If you want to delete a file, this command removes it and stages the change for the commit. 135 | 136 | git rm file_name.txt 137 | git commit -m "Remove test.txt" 138 | git push origin main 139 | ___________________________________________________________________________________________________________________________ 140 | 141 | 9) git clone 142 | _______________ 143 | Purpose: Downloads a copy of an existing Git repository (usually from a remote central repository) to your local system. 144 | Usage: This command is typically used when you want to start working on an existing project and need a copy of the central repository on your machine. 145 | 146 | git clone https://github.com/username/repository.git 147 | ___________________________________________________________________________________________________________________________ 148 | 149 | 10) git pull 150 | _______________ 151 | Purpose: Fetches and merges changes from the central repository to your local repository. 152 | Usage: Use this to keep your local repository up to date with changes made by other collaborators. However, conflicts may arise if your local changes clash with those on the central repo. 153 | 154 | git pull origin main 155 | 156 | Note: Conflicts occur when there are changes in the same part of a file in both your local and the central repository. You’ll need to manually resolve these conflicts. 157 | _____________________________________________________________________________________________________________________________ 158 | 159 | 11) git gui 160 | _______________ 161 | Purpose: Opens the Git GUI tool, which is a graphical user interface (GUI) for interacting with Git repositories. 162 | Usage: Some users prefer to use a GUI rather than the command line to manage their repositories. The git gui tool provides a more visual interface for Git operations like commit, branch, and status management. 163 | 164 | Example: git gui 165 | 166 | This will open a graphical tool where you can perform many of the Git operations like staging, committing, and viewing the commit history without needing to use 167 | ______________________________________________________________________________________________________________________ 168 | 169 | 170 | git pull versus git fetch 171 | ___________________________ 172 | a. git pull 173 | 174 | -> Downloads changes from the remote repository (e.g., GitHub, GitLab) to the local repository. 175 | -> Automatically merges the fetched changes into your current working branch. 176 | -> May cause merge conflicts if there are conflicting change 177 | 178 | Example: 179 | git pull 180 | _______________ 181 | 182 | b. git fetch 183 | _______________ 184 | -> Downloads the latest changes from the remote repository but does not merge them into your working branch. 185 | -> This is a safe operation because it allows you to review the changes before merging. 186 | -> The fetched changes are stored in origin/, and you can manually merge them when ready. 187 | 188 | Example: 189 | git fetch 190 | git merge 191 | 192 | ______________________________________________________________________________________________________________________ 193 | 12. git stash: 194 | _______________ 195 | -> git stash is a command used in Git to temporarily save changes that are not yet committed to work on something else without losing your modifications. 196 | -> when you run git stash, all your modified but unstaged or staged changes are temporarily stored in a hidden area 197 | 198 | example: 199 | git stash - to store changes in temp area 200 | git stash apply - to get the changes from temp area back 201 | 202 | 203 | 204 | 205 | 13. git cherrypic: 206 | __________________ 207 | -> git cherry-pick is used to apply a specific commit from one branch to another without merging the entire branch. 208 | 209 | Interview Question? 210 | 211 | You have done today 5 commits, but you want to merge only 3rd commit to particular branch, how will you do? 212 | Answer: git cherrypic 213 | 214 | Note: git merge will merge all the commit to a particular branch 215 | 216 | _______________ 217 | 218 | 219 | --------------------------------------------------------------------------------