Git is a distributed version control system that helps developers collaborate on projects of any size.
Linus Torvalds, the developer of the Linux kernel, created Git in 2005 to control Linux kernel development.
What is a distributed version control system?
A distributed version control system is a system that helps you keep track of changes made to your project files.
This change history resides on your local computer and allows you to easily revert to a previous version of your project if something goes wrong.
Git makes collaboration easy. All team members can keep a full backup of the repositories they are working on on their local computer. They can securely store the repository in one place thanks to an external server such as BitBucket, GitHub or GitLab.
This way, different team members can copy it locally and everyone has a clear view of all changes made by the entire team.
Git has many different commands you can use. And I've found that these fifty are the ones I use the most (and therefore the most useful to remember).
So I wrote them and thought it would be nice to share them with the community. I hope they are useful - Enjoy.
To verify the Git configuration:
The following command returns a list of information about your Git configuration, including username and email:
git config -l
To set your Git username:
You can set your username with the following command:
git config --global user.name "Fabio"
To configure your Git user email:
With this command you can set the user's email address that you will use in your commits.
git config --global user.email "firstname.lastname@example.org"
To cache your Git credentials:
You can cache your credentials so you don't have to enter them every time. Just use this command:
git config --credencial global.helper-Cache
To initialize a Git repository:
It all starts here. The first step is to initialize a new Git repository locally at the root of your project. You can do this with the following command:
To add a file to the staging area in Git:
The following command adds a file to the staging area. just replace
File name_herewith the name of the file you want to add to the staging area.
git add filename_here
How to add all files in staging area in Git
If you want to add all your project files to the staging area, you can use a wildcard
.and each file will be added for you.
How to only add specific files to the staging area in Git
You can use the asterisk in the following command to place all files starting with "fil" in the staging area.
git add file *
To check the status of a repository in Git:
This command displays the status of the current repository, including staged, unstaged, and untracked files.
Committing editor changes to Git:
This command opens a text editor in the terminal where you can type a complete confirmation message.
A confirmation message consists of a brief summary of changes, a blank line, and a full description of subsequent changes.
To commit changes with a message in Git:
You can add a confirmation message without opening Notepad. With this command, you can only provide a brief summary of your confirmation message.
git commit -m "your commit message here"
To make changes (and bypass the staging area) in Git:
You can add and commit tracked files with a single command using the -a and -m options.
git commit -a -m "Commit message here"
To view your commit history in Git:
This command displays the commit history of the current repository:
To view your commit history, including changes in Git:
This command shows the commit history, including all files and their changes:
git log -p
To view a specific commit in Git:
This command displays a specific confirmation.
Replace commit-id with the commit ID found in the commit record after the word commit.
git displays the commit ID
To view non-Git log statistics:
This command causes the git log to show some statistics about the changes on each commit, including changed lines and filenames.
git log --stat
Here's how to see the changes made before committing them to Git with "diff":
You can pass a file as a parameter to only show changes to a specific file.
git differenceshows only unscaled changes by default.
We can call diff with the
- stagedCheck to see the changes in stages.
git diffgit diff all_checks.pygit diff --staged
To see the changes with "git add -p":
This command will open a command prompt and ask if you want to make changes or not and will have more options.
git add -p
To remove tracked files from the current working tree in Git:
This command waits for a confirmation message explaining why the file was deleted.
git rm file name
To rename files in Git:
This command deploys the changes and waits for a confirmation message.
git mv old file new file
To ignore files in Git:
.ignore.gitfile and confirm.
To undo changes not provided in Git:
git checkout file name
To undo escalated changes in Git:
You can use the -p option flag to specify the changes you want to revert.
git reset HEAD Archivierungsname reset HEAD -p
To change the most recent commit in Git:
git commit --editallows you to modify and add changes to the last commit.
git commit --edit
!!Note!!: Patching a patched local commit is great, and you can move it to a shared repository after patching it. However, you should avoid modifying commits that have already been published.
To revert the last commit in Git:
go to redefinecreates a new commit that is the opposite of everything in the given commit.
We can undo the last commit with the parent alias like this:
git reverter HEAD
Prepare to roll back a previous commit no Git:
You can revert a previous commit with your commit ID. This will open Notepad so you can add a confirmation message.
git retorna commit_id_here
To create a new branch in Git:
By default it has one branch, the main branch. With this command you can create a new branch. Git will not change it automatically; You have to do it manually with the following command.
git rama branch_name
To switch to a newly created branch in Git:
If you want to use a different or newly created branch, you can use this command:
git checkout branch_name
To list branches in Git:
You can see all branches created with
rama gitDomain. It displays a list of all branches and marks the current branch with an asterisk and highlights it in green.
With a single command, you can quickly create and switch to a new branch.
git checkout -b branch_name
To delete a branch in Git:
When you're done working on a branch and merged it, you can delete it with the following command:
git branch -d branch_name
To merge two branches in Git:
To merge the history of the industry you are currently in with the
branch name, you need to use the following command:
git merge branch_name
To view the commit log as a graph in Git:
we can use
--Graphicfor the commit log to be displayed as a graph. Also,
--on-linewill limit confirmation messages to a single line.
registrar git --graph --oneline
To view the commit log as a graph of all branches in Git:
It does the same as the previous command but for all branches.
git log --graph --oneline --all
To cancel a conflicting merge in Git:
If you want to discard a combination and start over, run the following command:
git merge --abort
How to add a remote repository in Git
This command adds a remote repository to your local repository (just replace
https://repo_herewith the URL of your remote repository).
Adicionar git remote https://repo_here
To view non-Git remote URLs:
You can view all remote repositories from your local repository with this command:
git remote -v
For more information about a remote repository in Git:
Originwith the name of the command obtained by
Running the command git remote -v.
To push changes to a remote repository in Git:
When all your work is ready to be saved to a remote repository, you can commit all your changes with the following command:
To pull changes from a remote repository to Git:
When other team members are working on your repository, you can get the latest changes made to the remote repository with the following command:
To check the remote branches that Git is tracking:
This command displays the names of all remote branches that Git is tracking for the current repository:
git rama -r
To get changes from remote repositories in Git:
This command pulls changes from a remote repository, but doesn't do a merge to your local branch (because git pull does that for you).
To search for
How to check the current commit log of a remote repository in Git
Commit after commit, Git creates a record. You can find the remote repository log with this command:
git log source/main
To merge a remote repository into your local Git repository:
If the remote repository contains changes that you want to merge into your local repository, this command will do that for you:
git merge fonte/principal
To get content from remote branches into Git without auto-merging:
This allows you to update the remote without merging the content on the
local branches. You can call git merge or git checkout to perform the merge.
To push a new branch to a remote repository in Git:
If you want to push a branch to a remote repository, you can use the following command. Just remember to add -u to create the upstream branch:
git push -u origin branch_name
To delete a remote branch in Git:
If you no longer need a remote branch, you can remove it with the following command:
git push --delete origem branch_name_here
To use the Git rebase:
You can use to transfer completed work from one branch to another
git rebase branch_name_here
Git rebase can get really tricky if you don't do it right. Before using this command, I recommend that you read the official documentation again.Here
To run the rebase interactively in Git:
You can run git rebase interactively with the -i flag.
It opens Notepad and presents a set of commands you can use.
git rebase -i master# p, pick = use commit# r, reword = use commit but edit commit# e message, edit = use commit but stop changing #s, squash = use commit but with previous merge commit # f, fixup = like "squash" but delete log message for this commit # x, exec = run command (rest of line) with shell # d, drop = delete commit
To force a push request in Git:
This command forces a push request. This is usually fine for pull request branches as no one else should have cloned them.
But this is not something you want to do with public repositories.
git push -f
These commands can dramatically improve your productivity in Git. You don't have to remember all of them, which is why I wrote this cheat sheet. Bookmark this page for future reference or print it if you prefer.
Thanks for reading! By the way, I'm Fabio, a full-stack web developer and educator, and a certified expert in IT automation using Python. If you find this cheat sheet helpful, you're sure to find something interesting on my YouTube channel as well. you can applyHere.