GIT - Distributed Version Control System
What is Git?
Git is a distributed version control system used to track changes in source code during software development. It allows multiple developers to collaborate on a project, keeping a history of changes and enabling features like branching, merging, and collaboration without conflicts.
Git was created by Linus Benedict Torvalds in 2005 for use in the development of the Linux kernel.
In 2002, he was frustrated with the version control systems available at the time, he decided to create his own. According to Linus, he chose to call it "Git," which is British slang for an "unpleasant person".
Git Commands:
Here’s a comprehensive list of Git commands, organized by category. This covers basic and advanced commands used in various scenarios like working with repositories, managing branches, interacting with remote repositories, and more.
1. Basic Git Commands
- git init: Initializes a new Git repository.
- git clone <repository_url>: Clones a repository into a new directory.
- git status: Displays the status of the working directory and staging area.
- git add <file>: Adds files to the staging area.
- git add . : Stages all changes in the current directory.
- git commit -m "message": Commits staged changes with a message.
- git commit --amend -m "new message": Edits the last commit message.
- git log: Displays the commit history.
- git log --oneline: Shows a brief version of the commit history (one line per commit).
- git diff: Shows changes between working directory and staged files.
- git diff --staged: Shows differences between staged changes and the last commit.
- git rm <file>: Removes a file from the working directory and staging area.
- git mv <old> <new>: Renames or moves a file.
2. Branching & Merging
- git branch: Lists all branches in the repository.
- git branch <branch-name>: Creates a new branch.
- git checkout <branch-name>: Switches to a branch.
- git checkout -b <branch-name>: Creates and switches to a new branch.
- git merge <branch-name>: Merges a branch into the current branch.
- git rebase <branch-name>: Applies changes of the current branch on top of another branch.
- git branch -d <branch-name>: Deletes a branch (only if it has been merged).
- git branch -D <branch-name>: Forcefully deletes a branch.
3. Stashing Changes
- git stash: Temporarily saves changes without committing them.
- git stash list: Lists all stashed changes.
- git stash apply: Applies the latest stash.
- git stash apply stash@{n}: Applies a specific stash.
- git stash pop: Applies the latest stash and removes it from the stash list.
- git stash drop: Deletes the latest stash.
- git stash clear: Clears all stashes.
4. Remote Repositories
- git remote -v: Lists remote repositories.
- git remote add <name> <url>: Adds a new remote repository.
- git fetch <remote>: Fetches all branches from the remote repository.
- git pull <remote> <branch>: Fetches changes and merges them into the current branch.
- git push <remote> <branch>: Pushes local changes to the remote repository.
- git remote remove <name>: Removes a remote repository.
- git remote set-url <name> <new-url>: Changes the URL of a remote repository.
5. Resetting & Reverting
- git reset: Unstages changes but keeps them in the working directory.
- git reset <file>: Removes a specific file from the staging area.
- git reset --hard: Resets the working directory and staging area to the last commit.
- git reset --soft <commit>: Resets to a specific commit but keeps the changes in the working directory.
- git revert <commit>: Reverts a specific commit by creating a new commit that undoes the changes.
- git reset HEAD^: Moves the current branch back one commit.
6. Tagging
- git tag: Lists all tags.
- git tag <tag-name>: Creates a new tag.
- git tag -a <tag-name> -m "message": Creates an annotated tag with a message.
- git push <remote> --tags: Pushes all tags to the remote repository.
- git tag -d <tag-name>: Deletes a tag.
7. Branch Management
- git branch --merged: Shows branches that have been merged into the current branch.
- git branch --no-merged: Lists branches that have not been merged.
- git branch -m <new-name>: Renames the current branch.
8. Working with History
- git reflog: Shows a log of all the actions (commits, rebases, resets) that have been done in the repository.
- git log --graph --oneline: Shows a graphical representation of the branch structure.
- git cherry-pick <commit>: Applies a commit from another branch into the current branch.
- git blame <file>: Shows who made each change to a file.
- git show <commit>: Displays detailed information about a specific commit.
9. Submodules
- git submodule add <repository_url>: Adds a submodule to your repository.
- git submodule update --init --recursive: Initializes, updates, and clones all submodules.
- git submodule status: Displays the current status of submodules.
- git submodule foreach git pull origin master: Pulls updates for all submodules.
10. Cleaning Up
- git clean -f: Removes untracked files from the working directory.
- git clean -fd: Removes untracked files and directories.
- git prune: Removes unreachable objects from the repository.
11. Git Configuration
- git config --global user.name "Your Name": Sets the global username.
- git config --global user.email "youremail@example.com": Sets the global email.
- git config --global core.editor "editor": Sets the default text editor for Git.
- git config --global merge.tool <tool>: Specifies the tool to use for resolving merge conflicts.
12. Advanced Git Commands
- git bisect: A tool for binary searching to find which commit introduced a bug.
- git filter-branch: Rewrites Git history by applying filters.
- git archive: Creates a zip or tar archive of the repository.
- git bundle: Packs objects and references into a single file.
These commands cover almost all Git use cases from basic version control to advanced history manipulation and collaboration with other developers.