Home >Web Front-end >JS Tutorial >Useful GIT Commands and Aliases
This document provides a concise guide to useful Git commands and aliases, enhanced with visual aids. Let's streamline the content for improved readability and SEO.
This guide covers frequently used Git commands and helpful aliases to boost your workflow. If you have additional commands to share, please comment below!
Git Bash, a command-line tool for Windows, provides a powerful interface for Git operations. (For more details, see the msysgit project website).
Useful Git Commands:
View all branches:
<code class="language-bash">git branch --all</code>
Launch Gitk (GUI): Visualize your repository's history and changes.
<code class="language-bash">gitk</code>
Add & Commit changes: -a
stages all changes, -m
adds a commit message.
<code class="language-bash">git commit -a -m "Your commit message"</code>
Search repository content: Find "CSS" in all .js
files, for example.
<code class="language-bash">git grep "css" -- *.js</code>
Create a zipped backup: Creates a zip archive of the master
branch (replace master
with your branch name).
<code class="language-bash"> git archive --format=zip master^ > backup-$(date +%d-%m-%Y).zip ``` *(Improved date formatting)*</code>
View local Git configuration:
<code class="language-bash">cat .git/config</code>
Git aliases significantly shorten frequently used commands. (See the official Git documentation for more details on alias configuration).
Pretty Git Log: Displays a visually appealing log history.
<code class="language-bash">git config --global alias.history "log --abbrev-commit --pretty=oneline --graph --decorate" # Usage: git history</code>
Show Last Commit:
<code class="language-bash">git config --global alias.last "log -1 HEAD" # Usage: git last</code>
Reset to Last Commit: Use with caution!
<code class="language-bash">git config --global alias.resetlast "reset --hard HEAD" # Usage: git resetlast</code>
Purpose of Git Commands: Git commands manage and track changes in your codebase, enabling collaboration and version control.
Creating Git Aliases: Use git config --global alias.<alias_name> "<command>"</command></alias_name>
to define a shortcut. For example: git config --global alias.co checkout
Understanding Gitk: Gitk provides a visual interface to explore your repository's history.
git fetch
vs. git pull
: fetch
downloads changes; pull
downloads and merges them.
Undoing a Commit: Use git revert <commit_hash></commit_hash>
to create a new commit that reverses changes. git reset
can also be used but is more destructive.
Viewing Repository History: Use git log
to see commit history.
Resolving Merge Conflicts: Manually edit conflicted files, choose the correct changes, and commit the resolution. Tools like git mergetool
can assist.
Cloning a Repository: Use git clone <repository_url></repository_url>
.
Switching Branches: Use git checkout <branch_name></branch_name>
.
Deleting a Branch: Use git branch -d <branch_name></branch_name>
(ensure you're not on the branch you're deleting).
This revised version is more concise, uses stronger headings, and improves the overall presentation. The FAQ section provides clear and concise answers. The date formatting in the backup command is also enhanced for better functionality.
The above is the detailed content of Useful GIT Commands and Aliases. For more information, please follow other related articles on the PHP Chinese website!