Master Essential Git Commands for Efficient Development
Posted by Anonymous and classified in Design and Engineering
Written on in
English with a size of 12.56 KB
Essential Git Commands and Code Examples
| Package/Method | Description | Code Example |
|---|---|---|
| git add | Used to move changes from the working directory to the staging area. | git add sample.md |
| git add . | Allows moving all changed files into the staging area for GitHub repositories. | git add . |
| git am | Used to apply patches emailed to the repository. | git am < patchfile.patch |
| git branch | Allows creating an isolated environment within the repository to make changes. | git branch <new-branch> |
| git checkout | Allows viewing and switching between existing branches. | git checkout <existing-branch> |
| git checkout main | Allows switching to the main branch. | git checkout main |
| git clone | Allows creating a copy of a remote repository. | git clone <repository-url> |
| git commit | Allows taking staged snapshots of changes and committing them to the project. | git commit -m "Your commit message here" |
| git config --global user.email | Example 1: Sets a global email configuration for Git. Example 2: Sets a global username configuration for Git. | 1. git config --global user.email "[email protected]"2. git config --global user.name "Your Name" |
| git daemon | Used to allow anonymous downloads from the repository. | git daemon --reuseaddr --verbose |
| git diff | Helps identify and compare changes, allowing others to review your code. | git diff example.txt |
| git fetch | Used to transfer changes from the remote repository to your local repository. | git fetch <options> <remote-name> <branch-name> |
| git fetch upstream/main | Used to grab upstream branches. | git fetch upstream main:upstream-main |
| git format-patch | Generates or prepares email submissions for Linux kernel-style public forum workflows. | git format-patch -n <number_of_commits> |
| git http-backend | Provides a server-side implementation of Git-over-HTTP, allowing both fetch and push services. | git clone --bare /path/to/repos/myrepo.gitcd myrepo.gitgit update-server-info |
| git init | Used to initialize a new local Git repository. | git init <directory> |
| git instaweb | Allows setting up a web front-end for Git repositories. | git instaweb -p 8080 |
| git log | Enables browsing previous changes made to a project. | git log -p filename |
| git merge | Used to merge changes from a specified branch into the active branch. | git merge feature_branch |
| git merge upstream/main | Merges changes from the 'upstream/main' branch into the current branch. | git merge upstream/main |
| git pull | Used to transfer changes from the remote repository to your local repository and merge them. | git pull origin main |
| git pull downstream | Pulls changes from the main branch of a downstream repository. | git pull downstream main |
| git pull upstream | Pulls changes from the "upstream" repository into the current branch. | git pull upstream main |
| git push | Used to push all committed changes to the remote repository. | git push origin your_branch_name |
| git remote | A command to manage the set of tracked repositories. | git remote add upstream https://github.com/original/repo.git |
| git remote add origin <URL> | Adds a remote repository named "origin" with the specified URL. | git remote add origin https://github.com/yourusername/your-repo.git |
| git remote add upstream | Adds the original repository as a new remote repository labeled "upstream". | git remote add upstream https://github.com/original/repo.git |
| git remote rename | Renames a remote repository (e.g., from origin to new-origin). | git remote rename origin new-origin |
| git remote -v | Allows viewing the remotes associated with the local repository. | git remote -v |
| git request-pull | Example 1: Creates a summary of changes for upstream to pull. Example 2: Generates a summary of pending changes for an email request. | 1. git request-pull origin/main your-branch2. git request-pull <base> <head> <repository> |
| git rerere | Reuses recorded resolutions of previously resolved merge conflicts. | git rereregit rerere diff |
| git reset | Undoes changes made to files in your working directory. | git reset HEAD~1 |
| git revert | Used to undo botched commits by creating a new commit. | git revert HEAD |
| git send-email | Example 1: Sends email submissions without MUA corruption. Example 2: Sends a collection of patches as emails. | 1. git send-email [email protected] path/to/patchfile.patch2. git send-email --to [email protected] patches/*.patch |
| git-shell | Used as a restricted login shell for shared central repository users. | sudo usermod -s /usr/bin/git-shell gituser |
| git status | Allows viewing the state of the working directory and the staged snapshot. | git status |
| git version | Displays the current Git version installed on your system. | git --version |