{"id":3495,"date":"2024-10-21T10:42:23","date_gmt":"2024-10-21T08:42:23","guid":{"rendered":"https:\/\/nguenkam.com\/blog\/?p=3495"},"modified":"2024-10-21T10:42:23","modified_gmt":"2024-10-21T08:42:23","slug":"useful-git-command-line-tricks","status":"publish","type":"post","link":"https:\/\/nguenkam.com\/blog\/index.php\/2024\/10\/21\/useful-git-command-line-tricks\/","title":{"rendered":"Useful Git Command-Line Tricks"},"content":{"rendered":"\n<p>Git is an essential version control tool for developers. Although GUI tools can simplify some tasks, mastering the Git command line offers deeper control, flexibility, and speed. Here are\u00a0<strong>some Git command-line tricks<\/strong>\u00a0that every developer should know to streamline their workflow.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>1. Set Global Configuration<\/strong><\/h4>\n\n\n\n<p>Ensure your commits are tagged with the correct identity.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git config --global user.name \"Your Name\"\ngit config --global user.email \"you@example.com\"<\/code><\/pre>\n\n\n\n<p>?&nbsp;<strong>Tip:<\/strong>&nbsp;Use&nbsp;<code>--local<\/code>&nbsp;instead of&nbsp;<code>--global<\/code>&nbsp;to set project-specific configurations.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>2. Undo the Last Commit (without losing changes)<\/strong><\/h4>\n\n\n\n<p>If you made a mistake in the last commit, you can undo it.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git reset --soft HEAD~1<\/code><\/pre>\n\n\n\n<p>This leaves your changes staged, so you can amend the commit or fix the issue.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>3. Amend the Last Commit<\/strong><\/h4>\n\n\n\n<p>Forgot to include a change or want to update the commit message?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git add .\ngit commit --amend -m \"Updated commit message\"<\/code><\/pre>\n\n\n\n<p>This updates the previous commit without creating a new one.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>4. Stash Uncommitted Changes<\/strong><\/h4>\n\n\n\n<p>Need to quickly switch branches without committing?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git stash<\/code><\/pre>\n\n\n\n<p>? Retrieve the stash later with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git stash pop\n\n\/\/or\n\ngit stash apply<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>5. View Commit History Graphically<\/strong><\/h4>\n\n\n\n<p>Visualizing the commit history makes it easier to understand the project&#8217;s state.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git log --graph --oneline --all<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>6. Change the Commit Author<\/strong><\/h4>\n\n\n\n<p>Change the author of the last commit.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git commit --amend --author=\"New Author &lt;newauthor@example.com>\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>7. Check Differences in Staged Changes<\/strong><\/h4>\n\n\n\n<p>Use git diff to compare files at different stages.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git diff --staged<\/code><\/pre>\n\n\n\n<p>This shows the changes that are staged but not yet committed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>8. Find a Bug with Bisect<\/strong><\/h4>\n\n\n\n<p>Use git bisect to find the commit that introduced a bug.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git bisect start\ngit bisect bad  # Current commit is bad\ngit bisect good &lt;commit-hash>  # A known good commit<\/code><\/pre>\n\n\n\n<p>Git will walk through the commit history to identify the problematic commit.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>9. Cherry-Pick Specific Commits<\/strong><\/h4>\n\n\n\n<p>Want to bring a specific commit from another branch?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git cherry-pick &lt;commit-hash><\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>10. List All Branches (Local and Remote)<\/strong><\/h4>\n\n\n\n<p>See which branches are available.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git branch -a<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>11. Clean Untracked Files and Directories<\/strong><\/h4>\n\n\n\n<p>Quickly remove unwanted files that are not tracked by Git.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git clean -fd<\/code><\/pre>\n\n\n\n<p>? Use&nbsp;<code>-n<\/code>&nbsp;for a dry run to preview what will be removed.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>12. Track an Upstream Branch<\/strong><\/h4>\n\n\n\n<p>Keep your local branch in sync with a remote branch.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git branch --set-upstream-to=origin\/main<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>13. View the File at a Specific Commit<\/strong><\/h4>\n\n\n\n<p>Check a file&#8217;s state at a particular commit.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git show &lt;commit-hash>:path\/to\/file<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>14. Edit the .gitignore After Committing<\/strong><\/h4>\n\n\n\n<p>If you forgot to ignore certain files, update .gitignore.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo \"node_modules\/\" >> .gitignore\ngit rm -r --cached node_modules\/\ngit commit -m \"Update .gitignore\"<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>15. Revert a Pushed Commit<\/strong><\/h4>\n\n\n\n<p>Undo changes from a specific commit without changing history.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git revert &lt;commit-hash><\/code><\/pre>\n\n\n\n<h4><strong>16. Fetch Only Metadata<\/strong><\/h4>\n\n\n\n<p>Want to avoid fetching the whole repository?<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git fetch --dry-run<\/code><\/pre>\n\n\n\n<p>This lets you see what would be fetched without actually downloading data.<\/p>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<h4><strong>17. Blame a Line of Code<\/strong><\/h4>\n\n\n\n<p>Find out who wrote a specific line in a file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>git blame path\/to\/file<\/code><\/pre>\n\n\n\n<hr class=\"wp-block-separator\"\/>\n\n\n\n<p>These  Git command-line tricks can make your development process smoother, whether you are working alone or with a team. While GUI tools offer convenience, mastering the Git command line provides more control over your workflows. Try out these commands and elevate your Git skills!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Git is an essential version control tool for developers. Although GUI tools can simplify some tasks, mastering the Git command line offers deeper control, flexibility, and speed. Here are\u00a0some Git command-line tricks\u00a0that every developer should know to streamline their workflow. 1. Set Global Configuration Ensure your commits are tagged with the correct identity. ?&nbsp;Tip:&nbsp;Use&nbsp;&#8211;local&nbsp;instead of&nbsp;&#8211;global&nbsp;to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":807,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[60],"tags":[936,935,934,933,932],"_links":{"self":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3495"}],"collection":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/comments?post=3495"}],"version-history":[{"count":1,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3495\/revisions"}],"predecessor-version":[{"id":3496,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/posts\/3495\/revisions\/3496"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media\/807"}],"wp:attachment":[{"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/media?parent=3495"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/categories?post=3495"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/nguenkam.com\/blog\/index.php\/wp-json\/wp\/v2\/tags?post=3495"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}