Common Git Commands and Their Usage
This article provides a comprehensive overview of essential Git commands, covering repository initialization, staging, committing, branching, merging, remote synchronization, tag management, and configuration, enabling readers to effectively manage version control workflows in software projects.
Git's four main components are introduced, followed by a detailed list of commonly used commands.
1. Initialize a repository: git init 2. Add files to the staging area:
git add filename # add a specific file
git add -u # add modified or deleted tracked files
git add -A # add all changes, including untracked files
git add . # add all files in the current directory
git add -i # interactive mode3. Commit staged changes to the local repository:
git commit -m "commit message" # normal commit
git commit -a -m "msg" # skip staging, commit all modified files4. Check repository status: git status 5. Compare differences:
git diff # working tree vs. index
git diff branchname # working tree vs. a branch
git diff HEAD # working tree vs. last commit
git diff commitID filePath # specific file vs. a past version
git diff --stage # index vs. last commit
git diff tag # changes since a tag
git diff branchA branchB # compare two branches
# add --stat to see file change statistics6. View commit history:
git log # list commits
git log -p -n <number> # show patches for recent commits
git log --stat # summary of changes per commit
git log --name-only # list changed files only
git log --name-status # list added/modified/deleted files
git log --oneline # compact one‑line per commit
git log --graph --all --oneline # graphical branch history
git log --author="name" # filter by author
git log --grep="text" # filter by commit message
git log -S"text" # search for added/removed text
git log filename # history of a specific file7. Roll back changes:
git reset HEAD^ # undo last commit
git reset HEAD^^ # undo two commits
git reflog # view reference log
git reset --hard <commitID> # reset to a specific commit (hard)
# reset options: --soft (move HEAD only), --mixed (reset index), --hard (reset working tree)8. Push to remote repository: git push -u origin master 9. Delete a file from the repository: git rm filename 10. Replace working‑tree files with a version from the repository: git checkout -- test.txt 11. Add a remote origin:
git remote add origin [email protected]:username/repo.git12. Clone a remote repository: git clone [email protected]:username/repo.git 13. Create and switch to a new branch:
git checkout -b dev # creates and checks out 'dev'
# equivalent to:
git branch dev
git checkout dev14. List branches: git branch 15. Merge a branch:
git merge dev # merge 'dev' into current branch
git merge --no-ff -m "merge with no-ff" dev # create a merge commit even when fast‑forward is possible16. Delete a branch: git branch -d dev 17. Visualize branch history:
git log --graph --pretty=oneline --abbrev-commit18. Show remote information:
git remote # list remotes
git remote -v # detailed URLs19. Configure Git settings:
# Set user identity globally
git config --global user.name "Your Name"
git config --global user.email "[email protected]"
# Verify configuration
git config --global user.name
git config --global user.email
# List configurations
git config --global --list
git config --local --list
git config --system --list
git config --list
# Enable colored output
git config --global color.ui true20. Revert a specific commit:
git revert HEAD # revert the latest commit
git revert <commitID> # revert a specific commit21. Pull a remote branch to local:
git checkout -b localBranch remoteBranch # create and switch to a new local branch tracking remote
git fetch origin remote:local # fetch without checkout
git branch --set-upstream-to=origin/remote localBranch # set upstream tracking22. Tag operations:
git tag <tag> # create tag at HEAD
git tag # list tags
git tag <tag> <commitID> # tag a specific commit
git show <tag> # display tag details23. Synchronize updates from remote:
git fetch origin master # fetch latest from remote master
# compare and merge as needed; fetch is safer than pullAdditional promotional section about a Java content creator follows, which is not part of the technical guide.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Java Captain
Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
