Discover the essentials of Git version control with our beginner's guide. Learn key commands and workflows to efficiently manage your codebase and enhance your development skills.
At The Educative, we understand that mastering Git is vital for efficient version control and collaboration in software development. This cheat sheet provides a comprehensive overview of essential Git commands, helping you manage repositories, branches, and commits effectively.
Repository Management
`git init`: Initializes a new Git repository in the current directory.
git init
`git clone <repository>`: Clones an existing repository from a remote URL.
git clone https://github.com/CyberSaikat/CyberSaikat.git
`git remote add <name> <url>: Adds a new remote repository with the given name and URL.
git remote add origin https://github.com/CyberSaikat/CyberSaikat.git
`git remote -v`: Lists the current remote repositories associated with the local repository.
git remote -v
`git fetch`: Fetches changes from the remote repository without merging them into the local branch.
git fetch
`git pull`: Fetches and integrates changes from the remote repository into the current branch.
git pull
`git push`: Pushes local changes to the remote repository.
git push origin main
Branch Management
`git branch`: Lists all branches in the repository. The current branch is highlighted.
git branch
`git branch <branch-name>`: Creates a new branch with the specified name.
git branch feature/new-feature
`git checkout <branch-name>`: Switches to the specified branch.
git checkout feature/new-feature
`git checkout -b <branch-name>`: Creates a new branch and switches to it.
git checkout -b feature/new-feature
`git merge <branch-name>`: Merges changes from the specified branch into the current branch.
git merge feature/new-feature
`git branch -d <branch-name>`: Deletes the specified branch. Use -D to force delete.
git branch -d feature/old-feature
Commit History
`git log`: Displays the commit history for the current branch.
git log
`git log --oneline`: Shows the commit history with one line per commit.
git log --oneline
`git diff`: Shows the changes between commits, working directory, and index.
git diff
`git status`: Displays the status of changes in the working directory and staging area.
git status
`git show <commit>`: Displays detailed information about a specific commit.
git show a1b2c3d
`git revert <commit>`: Reverts changes introduced by the specified commit and creates a new commit.
git revert a1b2c3d
`git reset <commit>`: Resets the current branch to the specified commit. Use --hard to discard changes.
git reset --hard a1b2c3d
Create a separate branch for each feature or bug fix.
After completing the feature, merge the branch into the main branch and delete it.
Use this workflow to keep the main branch stable and free of incomplete code.
Often used in open-source projects, where developers fork the main repository and work on a copy.
After making changes, submit a pull request to the original repository.
This workflow is common for contributing to projects on GitHub.
A structured approach with branches for features, releases, and hotfixes.
Typically involves using the develop branch for development and the main branch for stable releases.
A popular choice for larger teams and projects with clear versioning requirements.
Integrates continuous integration and continuous deployment.
Every push triggers automated tests and deployments, streamlining code validation and release.
Suitable for projects with frequent updates or deployments.
git revert [commit-hash] – Reverts the changes introduced by a specific commit.
git reset --soft [commit-hash] – Moves the HEAD to a previous commit but keeps changes in the staging area.
git reset --hard [commit-hash] – Completely resets the working directory and staging area to a previous commit.
git log – Shows a list of all commits, often with dates and messages.
git diff – Displays differences between commits, branches, or working files.
git blame [file] – Shows who last edited each line of a file, helping trace changes.
git stash – Temporarily saves uncommitted changes without committing them.
git stash pop – Restores the stashed changes and removes them from the stash.
git stash list – Lists all stashes.
Commit Often: Make small, frequent commits with clear messages. It’s easier to track changes and resolve conflicts.
Use Branches: Keep the main branch stable and use branches for features and fixes.
Sync Regularly: Regularly pull changes from the remote repository to stay updated and avoid conflicts.
Write Clear Commit Messages: Aim for concise, informative messages that clarify the purpose of each commit.
By mastering these essential Git commands and workflows, you’ll be well-equipped to manage projects efficiently, work seamlessly with your team, and maintain a clean project history. Whether you're working solo or as part of a team, understanding Git fundamentals is key to smooth version control and collaboration.
No comments yet. Be the first to share your thoughts!
Learn about the architecture, design decisions, and implementation strategies behind successful open-source projects. A valuable resource for developers.
Discover how to set up a continuous integration (CI) pipeline to automate testing and deployment. This tutorial is perfect for those looking to streamline their development process.
A comprehensive overview of distributed systems, explaining core principles such as consistency, scalability, and fault tolerance.