Essential Git Tips and Commands After One Year of Use
This article shares a comprehensive one‑year‑long Git usage summary, covering essential commands for configuration, SSH key management, remote URL changes, viewing history, handling push errors, submodule operations, branch management, conflict resolution, and best practices for committing and reverting code.
After using Git for a year, I compiled a practical summary that covers most everyday scenarios and helps you work more efficiently.
Configure Git user name and email
git config --global user.name "xxx"
git config --global user.email "[email protected]"View current configuration:
git config --listGenerate and use an SSH key
Check for an existing key: cd ~/.ssh If none exists, generate a new one: ssh-keygen -t rsa -C "[email protected]" Press Enter three times to accept defaults; you will obtain id_rsa and id_rsa.pub. Do not modify the keys after creation.
Change remote repository URL
git remote set-url origin [email protected]:res_dev_group/test.git
git remote -vView file modification history
Show concise log for a file: git log --pretty=oneline <filename> Inspect a specific commit:
git show 356f6def9d3fb7f3b9032ff5aa4b9110d4cca87eFix push default warning
Git 1.x defaults to matching, while Git 2.x defaults to simple. To use the older behavior:
git config --global push.default matchingUse submodules for shared code
Add a submodule: git submodule add <repo_url> <path> Remove a submodule (edit .gitmodules then run): git rm --cached <submodule_path> Initialize submodules after cloning:
git submodule update --init --recursiveCancel a staged file
git reset HEAD <file>...Delete files
Remove from index and filesystem:
git rm file1
git commitRemove only from index (keep in filesystem):
git rm --cached file1
git commitRollback to a previous commit
git reset --hard 248cba8e77231601d1189e3576dc096c8986ae51Pull again if you need to revert the rollback.
Compare historical versions
git log
git show 4ebd4bbc3ed321d01484a4ed206f18ce2ebde5ca
git diff c0f28a2ec490236caa13dec0e8ea826583b49b7a 2e476412c34a63b213b735e5a6d90cd05b014c33Branch management
git checkout -b new_branch
git branch
git checkout master
git merge new_branch
git branch -d new_branchResolve merge conflicts
Conflict markers look like:
a123
<<<<<<< HEAD
b789
=======
b45678910
>>>>>>> 6853e5ff961e684d3a6c02d4d06183b5ff330dcc
cKeep your changes (between <<<<<<< HEAD and =======) and the incoming changes (between ======= and >>>>>>>), then commit.
Typical push failure and recovery
If git push fails because the remote has newer commits, first pull: git pull Resolve any conflicts, then repeat the usual workflow:
git add ...
git commit -m "..."
git pushSuccessful commit workflow
git status
git diff
git add dirname1/filename1.py dirname2/filenam2.py
git commit -m "fixed: modify upload logic"
git pushUnderstanding GitHub Pull Requests
Fork the original repository (Repo A) to your account (Repo A2), work on A2, then create a Pull Request on GitHub to ask the owner of Repo A to merge your changes.
Common error handling
Pathspec not found
git checkout master
git pull
git checkout new_branchLarge file push errors (HTTP 411/413)
Increase the HTTP post buffer size: git config http.postBuffer 524288000 If the error persists, ensure your SSH key is correctly added to the remote repository and retry the push.
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.
MaGe Linux Operations
Founded in 2009, MaGe Education is a top Chinese high‑end IT training brand. Its graduates earn 12K+ RMB salaries, and the school has trained tens of thousands of students. It offers high‑pay courses in Linux cloud operations, Python full‑stack, automation, data analysis, AI, and Go high‑concurrency architecture. Thanks to quality courses and a solid reputation, it has talent partnerships with numerous internet firms.
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.
