Git is a free and open-source distributed version control system designed to manage software development projects. It was created by Linus Torvalds in 2005 to help manage the development of the Linux kernel. We will see Git Cheat Sheet in this article.
Git allows multiple developers to work on the same codebase simultaneously without overwriting each other’s changes. It tracks changes made to the codebase over time, allowing developers to roll back to previous versions if necessary.
Some key concepts in Git include repositories, branches, commits, and merges. A repository is a collection of files and directories, along with the version history for those files. Branches are parallel versions of the codebase that allow developers to work on separate features or bug fixes without interfering with each other. Commits are snapshots of the codebase at a specific point in time, and merges combine changes from one branch into another.
Git is widely used in the software development industry and is considered an essential tool for managing codebases. There are many resources available for learning Git, including online tutorials, documentation, and books.
So let’s see all Git Cheat Sheet codes.
Table of Contents
git– Check if the Git is installed or not.
git config --global user.name "Your Name"– Setup your name in config.
git config --global user.email "firstname.lastname@example.org"– Setup your email in config.
git config --list– To check the configuration like name, email etc.
Creating a Repository
git init– Initialize the directory for git.
git clone [repository url]– For cloning the repo
git status– To check the changes/modifications on files. Whether it is added, updated, or deleted.
git add [filename]– To stage the modified file.
git add .– To stage every files.
git commit -m "commit message"– Do a commit with a message.
git push origin [BRANCH_NAME],
git push -u origin [BRANCH_NAME],
git push origin [BRANCH_NAME] -f– Push the changes to repo. 4th option is for force push.
git pull origin [BRANCH_NAME]/
git pull upstream [BRANCH_NAME]– To pull the latest changes from the GitHub repo, 3rd option will pull all the changes from the upstream branch, if you have forked the repo.
git branch– To check all available branches for the particular repo.
git branch [BRANCH_NAME]– To create a new branch (If you want to create a branch from a specific branch like master or staging, then first check out in that particular branch and then create a new branch).
git checkout [BRANCH_NAME]– Checkout in specific branch / Move to branch.
git checkout -b [BRANCH_NAME]– Create & Checkout in the branch with this single command.
git checkout [COMMIT_ID]– This will give you that particular commit changes in Detached Head. This is just for changes. We can’t push it.
git branch -d [BRANCH_NAME]– To delete the branch.
git merge [BRANCH_NAME]– To merge the specific branch into current branch. (First checkout in the branch where you want to merge the other branch, then run the command).
You can easily learn git branching with this site: https://learngitbranching.js.org/
git stash– A changes which we don’t want to delete and commit. We can push that changes to the stash(back area).
git stash pop– Get the all stash changes back.
git stash clear– Clean out all the stash changes. We will not be able to get those changes again.
git log– Shows the log.
git log --graph– Shows the log in graph format.
git show [commit hash]
Git Cheat Sheet Notes:
1) If you create Branch B from Branch A, then if you do changes in Branch B you can simply merge it to the Branch A with two ways.
- You can make a pull request from Github and merge it from there. And you have to take a pull in a code from Branch A.
- You can merge with command “git merge BRANCH_NAME” (Make sure you are in that branch where you want to merge another branch).
Then you have to push the Branch A code to the Github via command.
2) When you take the wrong branch pull in your branch
git reset --hard ORIG_HEAD
3) When you want to contribute to any other project on GitHub. You can simply fork the repo.
- Fork means it will create the origin repo in your account, but the origin repo will be connected with upstream
4) If we have created a PR for any Branch, then after we can’t create another PR for the same branch until PR is merged.
- If you make any changes on that branch, then it will show the commits on that same PR.
5) If you want to get your previous changes with commit then you can do it with
git checkout -b [BRANCH_NAME] <COMMIT_ID>.
- This will create a new Branch with the given name and pull that mentioned commit ID code into that local branch.
Hope this Git Cheat Sheet will help you.