List of Git commands
1. Configuration
git config: Set Git configuration values (user name, email, etc.)git config --global user.name "Your Name": Set global user namegit config --global user.email "youremail@example.com": Set global user emailgit config --list: List all Git configuration values
2. Repository Initialization
git init: Initialize a new Git repository in the current directorygit clone <repository-url>: Clone an existing repository
3. Checking Repository Status
git status: Show the status of changes (modified, staged, etc.)git diff: Show changes between commits, commit and working directory, etc.git diff --staged: Show differences between staged changes and the last commit
4. Branching
git branch: List all branchesgit branch <branch-name>: Create a new branchgit branch -d <branch-name>: Delete a branch (if fully merged)git branch -D <branch-name>: Force delete a branch (even if not merged)git branch -m <new-branch-name>: Rename the current branchgit branch -m <old-branch-name> <new-branch-name>: Rename a non-current branch
git checkout <branch-name>: Switch to a different branchgit checkout -b <branch-name>: Create and switch to a new branchgit switch <branch-name>: Switch to a different branch (alternative togit checkout)
5. Committing Changes
git add <file>: Stage changes (specific file)git add .: Stage all changes in the current directorygit commit -m "message": Commit changes with a messagegit commit --amend: Amend the last commit (edit commit message or add changes)git commit --no-edit --amend: Amend the last commit without changing the commit message
6. Viewing Commit History
git log: View the commit historygit log --oneline: View the commit history in a compact formatgit log --graph --oneline --decorate: View a graphical representation of the commit history
git show <commit-hash>: Show the details of a specific commit
7. Remote Repositories
git remote -v: List remote repositories and their URLsgit remote add <name> <repository-url>: Add a new remote repositorygit remote remove <name>: Remove a remote repositorygit push <remote> <branch>: Push local changes to a remote repositorygit push <remote> --delete <branch>: Delete a remote branchgit pull <remote> <branch>: Fetch and merge changes from a remote repositorygit fetch <remote>: Fetch changes from a remote repository without merginggit fetch --all: Fetch all branches from all remotesgit push --set-upstream <remote> <branch>: Set the upstream branch for the current branch
8. Merging and Rebasing
git merge <branch>: Merge the specified branch into the current branchgit rebase <branch>: Rebase the current branch onto the specified branchgit merge --abort: Abort a merge process (if there are conflicts)git rebase --abort: Abort a rebase process
9. Stashing Changes
git stash: Stash changes (save them temporarily)git stash pop: Apply the most recent stash and remove it from the stash listgit stash apply: Apply a stash without removing itgit stash list: List all stashed changesgit stash drop: Remove a specific stash from the list
10. Resolving Conflicts
git mergetool: Launch the merge tool to resolve conflictsgit diff: Show the differences between conflicting changes
11. Undoing Changes
git reset <commit>: Reset the current branch to a specific commitgit reset --soft <commit>: Keep changes stagedgit reset --mixed <commit>: Keep changes but unstage themgit reset --hard <commit>: Discard all changes
git revert <commit-hash>: Create a new commit that undoes a specific commitgit checkout -- <file>: Discard changes to a file (revert it to the last committed state)
12. Viewing Differences
git diff: Show changes between the working directory and the index (staging area)git diff --staged: Show changes between the staged files and the last commit
13. Cleaning Up
git clean -f: Remove untracked filesgit clean -fd: Remove untracked files and directoriesgit clean -f -n: Show which files would be removed bygit clean(dry run)
14. Tags
git tag: List all tagsgit tag <tag-name>: Create a new taggit tag -d <tag-name>: Delete a taggit push origin <tag-name>: Push a tag to the remote repositorygit push --tags: Push all tags to the remote repository
15. Configuration and Aliases
git config --global <key> <value>: Set a global Git configuration keygit config --global alias.<alias-name> <command>: Create a custom Git alias
16. Troubleshooting and Diagnostics
git fsck: Verify the integrity of the Git repositorygit reflog: Show the history of theHEADand where it pointed at different timesgit show-ref: Show references to all commits, tags, and branches
17. Interactive Commands
git rebase -i <commit-hash>: Interactive rebase to rewrite historygit add -p: Add changes interactively
Summary of Common Commands
| Action | Command |
|---|---|
| Initialize a repository | git init |
| Clone a repository | git clone <repository-url> |
| Check status | git status |
| Stage a file | git add <file> |
| Commit changes | git commit -m "message" |
| View commit history | git log |
| Create a new branch | git branch <branch-name> |
| Switch branches | git checkout <branch-name> |
| Merge branches | git merge <branch-name> |
| Push changes to remote | git push origin <branch-name> |
| Pull changes from remote | git pull origin <branch-name> |
| Fetch updates from remote | git fetch origin |
| Revert a commit | git revert <commit-hash> |
| Stash changes | git stash |
| Apply stashed changes | git stash pop |
| Remove untracked files | git clean -f |
| Create a tag | git tag <tag-name> |
These commands should cover most of your Git needs. For detailed information on any specific command, you can use git <command> --help.
No images available.