Home >Web Front-end >JS Tutorial >Useful GIT Commands and Aliases

Useful GIT Commands and Aliases

Lisa Kudrow
Lisa KudrowOriginal
2025-02-23 10:31:15864browse

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.

Essential Git Commands & Aliases: A Quick Reference

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: Your Command-Line Interface

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>

    Useful GIT Commands and Aliases

  • Launch Gitk (GUI): Visualize your repository's history and changes.

    <code class="language-bash">gitk</code>

    Useful GIT Commands and Aliases

  • 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: Save Time & Effort

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>

    Useful GIT Commands and Aliases

  • 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>

Frequently Asked Questions (FAQs)

  • 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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn