Git cheatsheet

Basic Commands

Command Description
git init Initialize a new Git repository
git clone <repository-url> Clone an existing Git repository
git add <file> Add a file to the staging area
git add . Add all files in the current directory to the staging area
git commit -m "commit message" Commit the staged changes with a message
git status Check the current status of the repository
git log View the commit history

Branching and Merging

Command Description
git branch List all the branches in the repository
git branch <branch-name> Create a new branch
git checkout <branch-name> Switch to a different branch
git merge <branch-name> Merge the specified branch into the current branch
git branch -d <branch-name> Delete a branch
git rebase <branch-name> Rebase the current branch onto the specified branch

Remote Repositories

Command Description
git remote add origin <repository-url> Add a remote repository
git push origin <branch-name> Push the specified branch to the remote repository
git pull origin <branch-name> Pull the latest changes from the remote repository
git fetch Fetch the latest changes from the remote repository without merging them

Undoing Changes

Command Description
git reset <file> Unstage a file from the staging area
git reset HEAD~1 Undo the last commit, but keep the changes in the working directory
git revert HEAD Undo the last commit and create a new commit with the reverted changes
git checkout -- <file> Discard all local changes in a file

Useful Tips

  • Use git diff to see the changes between the working directory and the staging area.
  • Use git blame <file> to see who last modified each line of a file.
  • Use git log --oneline --graph --all to get a nice visual representation of the commit history.
  • Use git stash to temporarily save your local changes and reapply them later.
  • Use git config --global alias.<alias-name> <command> to create custom Git aliases.
Learn more corner

Understanding the Basics of Git: A Non-Technical Guide

Git is a powerful version control system that has become the industry standard for managing code repositories. While it may seem daunting at first, especially for those with limited technical expertise, understanding the basics of Git can greatly enhance your workflow and collaboration with others.

What is Git?

Git is a distributed version control system that allows multiple people to work on the same project simultaneously. It keeps track of changes made to files over time, enabling you to revert to previous versions if needed. This makes it an invaluable tool for software development, as it allows teams to collaborate effectively and maintain a clear history of their project's evolution.

The Git Lifecycle

The Git lifecycle consists of several key steps:

  1. Initialize a Repository: The first step in using Git is to create a new repository (or "repo") for your project. This can be done locally on your own computer or on a remote server, such as GitHub or GitLab.

  2. Make Changes: As you work on your project, you'll make changes to various files. Git allows you to track these changes, so you can see what has been modified, added, or removed.

  3. Stage Changes: Before committing your changes, you'll need to "stage" them, which means adding them to the staging area. This is like a holding area where you can review and organize your changes before finalizing them.

  4. Commit Changes: Once you're satisfied with the changes in the staging area, you can commit them to the repository. A commit is like a snapshot of your project at a specific point in time, and it includes a message that describes the changes you've made.

  5. Collaborate with Others: One of the most powerful features of Git is its ability to facilitate collaboration. You can "push" your commits to a remote repository, allowing others to access and work on the same project. Similarly, you can "pull" changes made by others into your local repository.

The Branching Workflow

Git's branching functionality is a powerful tool that enables non-linear development. Branching allows you to create separate lines of development, which can be used for experimenting with new features, fixing bugs, or maintaining different versions of your project. When you're ready to integrate your changes, you can "merge" your branch back into the main codebase.

Undoing Changes

Git provides several ways to undo changes, whether you've made a mistake in your local repository or need to revert a commit that has already been pushed to a remote repository. Understanding these undo mechanisms can be invaluable when things go wrong.

Conclusion

While Git may seem intimidating at first, taking the time to understand its basic concepts and workflow can greatly improve your productivity and collaboration with others. By mastering the fundamentals of Git, you'll be well on your way to becoming a more efficient and effective project manager, developer, or team member.