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/MethodDescriptionCode Example
git addUsed 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 amUsed to apply patches emailed to the repository.git am < patchfile.patch
git branchAllows creating an isolated environment within the repository to make changes.git branch <new-branch>
git checkoutAllows viewing and switching between existing branches.git checkout <existing-branch>
git checkout mainAllows switching to the main branch.git checkout main
git cloneAllows creating a copy of a remote repository.git clone <repository-url>
git commitAllows taking staged snapshots of changes and committing them to the project.git commit -m "Your commit message here"
git config --global user.emailExample 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 daemonUsed to allow anonymous downloads from the repository.git daemon --reuseaddr --verbose
git diffHelps identify and compare changes, allowing others to review your code.git diff example.txt
git fetchUsed to transfer changes from the remote repository to your local repository.git fetch <options> <remote-name> <branch-name>
git fetch upstream/mainUsed to grab upstream branches.git fetch upstream main:upstream-main
git format-patchGenerates or prepares email submissions for Linux kernel-style public forum workflows.git format-patch -n <number_of_commits>
git http-backendProvides a server-side implementation of Git-over-HTTP, allowing both fetch and push services.git clone --bare /path/to/repos/myrepo.git
cd myrepo.git
git update-server-info
git initUsed to initialize a new local Git repository.git init <directory>
git instawebAllows setting up a web front-end for Git repositories.git instaweb -p 8080
git logEnables browsing previous changes made to a project.git log -p filename
git mergeUsed to merge changes from a specified branch into the active branch.git merge feature_branch
git merge upstream/mainMerges changes from the 'upstream/main' branch into the current branch.git merge upstream/main
git pullUsed to transfer changes from the remote repository to your local repository and merge them.git pull origin main
git pull downstreamPulls changes from the main branch of a downstream repository.git pull downstream main
git pull upstreamPulls changes from the "upstream" repository into the current branch.git pull upstream main
git pushUsed to push all committed changes to the remote repository.git push origin your_branch_name
git remoteA 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 upstreamAdds the original repository as a new remote repository labeled "upstream".git remote add upstream https://github.com/original/repo.git
git remote renameRenames a remote repository (e.g., from origin to new-origin).git remote rename origin new-origin
git remote -vAllows viewing the remotes associated with the local repository.git remote -v
git request-pullExample 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-branch
2. git request-pull <base> <head> <repository>
git rerereReuses recorded resolutions of previously resolved merge conflicts.git rerere
git rerere diff
git resetUndoes changes made to files in your working directory.git reset HEAD~1
git revertUsed to undo botched commits by creating a new commit.git revert HEAD
git send-emailExample 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.patch
2. git send-email --to [email protected] patches/*.patch
git-shellUsed as a restricted login shell for shared central repository users.sudo usermod -s /usr/bin/git-shell gituser
git statusAllows viewing the state of the working directory and the staged snapshot.git status
git versionDisplays the current Git version installed on your system.git --version

Related entries: