6

A Cheatsheet For Git

 1 year ago
source link: https://www.codelivly.com/a-cheatsheet-for-git/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Git

A Cheatsheet For Git

By RockyDecember 30, 2022Updated:December 30, 2022No Comments5 Mins Read

Share

git-cheat-sheet-blog-1024x576.png
Share

Git is an incredibly powerful version control system that has become the industry standard for collaborating on and tracking changes to code. While it can seem overwhelming at first, with a little practice and a handy cheatsheet, you’ll be navigating Git like a pro in no time.

Here are some essential Git commands to get you started:

  1. Clone a repository: Use this command to download a copy of a repository from a remote server onto your local machine.
git clone <repository URL>
  1. Check the status of your repository: This command will show you which files in your repository have been modified and are ready to be committed.
git status
  1. Add changes to the staging area: Use this command to prepare your changes for a commit. You can either add specific files or use git add . to add all modified files.
git add <filename>
  1. Commit your changes: Use this command to save your changes to the repository’s history. Make sure to include a commit message explaining the changes you made.
git commit -m "Commit message"
  1. Push your changes to the remote repository: Use this command to send your committed changes to the remote repository.
git push
  1. Pull changes from the remote repository: Use this command to download any new commits that have been made to the remote repository.
git pull
  1. Create a branch: Branches allow you to work on multiple features or bug fixes simultaneously without affecting the main branch. Use this command to create a new branch.
git branch <branch name>
  1. Switch to a different branch: Use this command to switch between branches.
git checkout <branch name>
  1. Merge branches: Use this command to merge changes from one branch into another.
git merge <source branch> <target branch>
  1. Revert changes: Use this command to undo commits and return to a previous state in your repository’s history.
git revert <commit hash>
  1. View commit history: Use this command to see a list of all the commits that have been made in the repository, along with the commit messages and authors.
git log
  1. Undo changes to a file: If you’ve made changes to a file and want to revert them, you can use this command to discard the changes and return to the version in your last commit.
git checkout -- <filename>
  1. Compare changes between commits: Use this command to see the differences between two commits.
git diff <commit hash 1> <commit hash 2>
  1. Create a tag: Use this command to mark a specific commit in the repository with a label, which can be useful for identifying releases or important points in the project’s history.
git tag <tag name> <commit hash>
  1. Push tags to the remote repository: Use this command to send tags to the remote repository, so that other collaborators can access them.
git push --tags
  1. Create a branch and switch to it at the same time: If you want to create a new branch and switch to it in one command, you can use this handy shortcut.
git checkout -b <branch name>
  1. Delete a branch: When you’re finished with a branch and want to delete it, use this command.
git branch -d <branch name>
  1. Stash changes: If you need to switch branches but don’t want to commit your changes yet, you can use this command to temporarily save them and switch branches without committing.
git stash
  1. Create a new repository: If you’re starting a new project and want to set up a Git repository for it, use this command to create a new repository on your local machine.
git init
  1. Rename a branch: If you need to rename a branch, use this command.
git branch -m <new branch name>
  1. Merge conflicts: If you’re trying to merge two branches and Git detects a conflict, you’ll need to resolve the conflict manually. Use this command to open a text editor and resolve the conflict.
git mergetool
  1. Abort a merge: If you want to stop a merge that’s in progress, you can use this command to abort it.
git merge --abort
  1. Remove a file from the repository: If you need to delete a file from the repository, use this command.
git rm <filename>
  1. Ignore files: If you have certain files or directories that you don’t want Git to track, you can use this command to add them to the .gitignore file.
git ignore <filename or directory>
  1. Reset commits: If you want to undo commits and return to a previous point in your repository’s history, use this command.
git reset <commit hash>

By mastering these Git commands and adding them to your cheatsheet, you’ll be able to effectively collaborate with your team and manage changes to your code. Don’t be afraid to experiment and try out new commands to see how they can help you manage your code more efficiently.

Conclusion

In conclusion, Git is a powerful version control system that allows you to collaborate with your team, track changes to your code, and manage your repository effectively. By mastering a few essential commands and adding them to your cheatsheet, you’ll be able to navigate Git like a pro in no time. Don’t be afraid to experiment and try out new commands to see how they can help you manage your code more efficiently.


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK