Git Cheat Sheet: Essential Git Commands

Git Cheat Sheet: Essential Git Commands

Git is a powerful version control tool used by developers worldwide to manage code and track changes in projects. This cheat sheet covers the essential Git commands that will help you work effectively with repositories.

1. Getting Started with Git

Creating a new project:

1echo "# project name" >> README.md  # Create a README.md file
2git init  # Initialize a new Git repository
3git add README.md  # Add the file to the staging area
4git commit -m "first commit"  # Commit the changes with a message

Connecting to a remote repository:

1git remote add origin https://github.com/username/project.git  # Add a remote repository
2git push -u origin master  # Push changes to the master branch on the remote server

2. Working with Commits and Logs

Viewing commit history:

1git log --oneline  # Display a condensed list of commits

Reverting to a previous state:

1git checkout .  # Revert all changes
2git checkout "commit_hash"  # Revert to a specific commit
3git checkout master  # Switch back to the master branch

3. Branching and Working with Branches

Creating and switching between branches:

1git branch new_branch  # Create a new branch
2git checkout new_branch  # Switch to the new branch
3git checkout -b new_branch origin/new_branch  # Create and switch to a branch from a remote repository

Merging branches:

1git merge some_branch  # Merge branch some_branch into the current branch
2git branch -d some_branch  # Delete the branch after merging

4. Deleting and Recovering Data

Recovering data from a remote repository:

1git fetch --all  # Fetch all branches from the remote repository
2git reset --hard origin/master  # Reset to the state of the master branch on the remote server

Deleting files and branches:

1git rm filename.txt  # Remove a file from the repository
2git push origin :branch-name  # Delete a branch from the remote repository

5. Useful Commands

Viewing changes in a commit:

1git show d8578edf8458ce06fbc5bb76a58c5ca4a58c5ca4  # Show changes made in a specific commit

Cleaning up untracked files:

1git clean -f  # Remove untracked files from the local repository

This cheat sheet covers the most commonly used Git commands to help you efficiently manage repositories and project versions. I hope it proves useful in your daily workflow!

comments powered by Disqus

Translations: