Git cheat sheet

Git Cheat Sheet

Git is a distributed version control system that helps developers on efficient team collaboration and track and manage changes to their code. It tracks changes to files, facilitates collaboration, and enables developers to work on different features simultaneously. Git maintains log of changes, allows users to revert to previous versions, and makes it easy to merge contributions into a unified codebase. It is widely used in software development to improve code management, collaboration, codebase integrity, well-documented and organized project history.

Git Initialization and Configuration:

  • git init: Initializes a new Git repository in the current directory. It creates a new .git directory to store version control information and starts tracking changes to files in the repository.
  • git config --global user.name "Your Name": Sets your global username for commits. This is the name that will be associated with your commits across all repositories on your system.
  • git config --global user.email "[email protected]": Sets your global email for commits. This email will be associated with your commits across all repositories on your system.

Adding and Committing:

  • git status: Shows the status of your working directory and staging area. It displays information about modified, staged, and untracked files.
  • git add <file>: Adds changes from the working directory to the staging area. It prepares changes for the next commit.
  • git add .: Adds all changes from the working directory to the staging area. It stages all modifications, deletions, and new files.
  • git commit -m "Your commit message": Commits staged changes with a descriptive message. It permanently records the changes in the repository’s history.

Branching and Merging:

  • git branch: Lists all branches in the repository, with an asterisk (*) indicating the current branch.
  • git branch <branch-name>: Creates a new branch with the given name. It creates a new pointer to the current commit, allowing you to develop features or bug fixes independently.
  • git checkout <branch-name>: Switches to the specified branch. It updates the working directory to match the selected branch’s last commit.
  • git merge <branch-name>: Merges changes from the specified branch into the current branch. It combines the changes and creates a new commit.

Remotes and Collaboration:

  • git remote add origin <remote-url>: Adds a remote repository with the given URL. The remote repository will be referenced by the name “origin.”
  • git remote -v: Lists all remote repositories and their associated URLs.
  • git push -u origin <branch-name>: Pushes the local branch to the remote repository. The “-u” option sets up the tracking relationship between the local and remote branches.
  • git pull origin <branch-name>: Pulls changes from the remote repository into the current branch. It fetches changes and automatically merges them into the current branch.

Viewing History and Changes:

  • git log: Displays the commit history of the repository, showing the author, date, and commit message for each commit.
  • git log --oneline: Displays a condensed version of the commit history, showing only the first few characters of the commit hash and the commit message.
  • git diff: Shows the changes between the working directory and the staging area. It helps review modifications before staging them.
  • git diff --staged: Shows the changes between the staging area and the last commit. It helps review changes before committing them.

Undoing Changes:

  • git reset HEAD <file>: Unstages changes from the staging area. It removes the changes from the staging area without discarding them from the working directory.
  • git checkout -- <file>: Discards changes in the working directory for a specific file. It reverts the file to the state of the last commit.
  • git reset --hard HEAD: Discards all changes in the working directory and staging area. It reverts both tracked and untracked files to the state of the last commit.

Tagging and Releases:

  • git tag: Lists all tags in the repository. Tags are used to mark significant points in the commit history, such as releases or milestones.
  • git tag <tag-name>: Creates a lightweight tag at the current commit. Lightweight tags are just references to specific commits and do not contain extra information.
  • git tag -a <tag-name> -m "Tag message": Creates an annotated tag with a message. Annotated tags contain extra information, such as tagger name, email, and the tag message.
  • git push origin --tags: Pushes all tags to the remote repository. It sends both lightweight and annotated tags to the remote repository.

Stashing:

  • git stash: Temporarily saves changes that are not ready for commit. It allows you to switch branches or apply updates without committing unfinished work.
  • git stash apply: Applies the latest stashed changes back to the working directory. It restores the changes to the working directory.
  • git stash list: Lists all stashed changes. It shows a list of stashes with a reference name for each stash.

Aliases:

  • git config --global alias.<alias-name> "<git-command>": Creates a custom Git alias. Aliases are shortcuts for frequently used commands, making Git usage more efficient.

Git offers many more powerful commands and features. We can use git --help or git <command> --help for more information on specific commands. Refer to official Git documentation or additional resources for deep understanding.

You May Also Like