Git is a distributed version control system that helps developers collaborate on projects of any size. Linus Torvalds, a Linux kernel developer, created Git in 2005 to help 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 you make to files in your project.

This change history is kept on the local machine and you can easily revert to a previous version of the project in the event of a problem.

Git makes collaboration easy. Everyone on the team can keep a full backup of the repository they are developing on their local machine. Then, thanks to external servers like BitBucket, GitHub, or GitLab, they can safely store their repositories in one place.

This way, different members of the team can copy it locally, and everyone can clearly see all the changes made by the entire team.

Git has a number of different commands you can use. I find these 50 techniques to be the ones I use most often (and therefore the ones that help me remember best).

So I wrote them down and thought it would be nice to share them with the community. I hope you find them useful.

Check your Git configuration:

The following command returns a list of information about git configuration, including username and email:

git config -l
Copy the code

How to set Git username:

You can configure your username using the following command:

git config --global user.name "your username"
Copy the code

How to set up your Git user email:

This command allows you to set the user email address to be used in the submission.

git config --global user.email "[email protected]"
Copy the code

How to cache your login credentials in Git:

You can store login credentials in the cache so you don’t have to enter them every time. Just use this command:

git config --global credential.helper cache
Copy the code

How to initialize a Git repo

It all starts here. The first step is to initialize a new Git repo locally in the project root directory. You can use the following command:

git init
Copy the code

How to add a file to staging in Git:

The following command adds a file to the staging area. Simply replace filename_here with the name of the file you want to add to the staging area.

git add filename_here
Copy the code

How to add all files in staging in Git:

If you want to add all the files in your project to the staging area, you can use the wildcard character ‘. ‘. Each file will be added for you.

git add .
Copy the code

How do I add only certain files to the staging area in Git

Using the asterisk in the following command, you can add all files starting with ‘fil’ to the staging area.

git add fil*
Copy the code

How to check the state of a repository in Git:

This command displays the status of the current repository, including pending, unpending, and untracked files.

git statu
Copy the code

How to commit changes in Git’s editor:

This command opens a text editor in the terminal where you can write the complete commit message.

The commit message consists of a short summary of the changes, blank lines, and a full description of subsequent changes.

git commit
Copy the code

How to commit changes via message in Git:

You can add commit messages without opening the editor. This command only allows you to specify a short summary of the commit message.

git commit -m "your commit message here
Copy the code

How to commit changes in Git (and skip the staging area):

By using the -a and -m options, you can add and commit trace files using a single command.

git commit -a -m"your commit message here"
Copy the code

How to view commit history in Git:

This command displays the commit history of the current repository:

git log
Copy the code

How to view your commit history, including changes in Git:

This command displays the committed history, including all files and their changes:

git log -p
Copy the code

How to see a specific commit in Git:

This command displays a specific commit.

Replace the commit id with the commit ID you found after the word commit in the commit log.

git show commit-id
Copy the code

Git log statistics

This command will cause the Git log to show some statistics about each committed change, including the line and filename of the change.

git log --stat
Copy the code

How to use diff in Git to see changes made before committing:

You can pass a file as a parameter so that you only see changes on a particular file.

By default, git diff shows only unstaged changes.

We can use the –staged flag to call diff to view any staged changes.

git diff
git diff all_checks.py
git diff --staged
Copy the code

How to use “git add -p” to view changes:

This command opens a prompt asking you if you want to make changes and includes additional options.

git add -p
Copy the code

How to remove trace files from Git’s current working tree:

This command requires a commit message to explain why the file was deleted.

git rm filename
Copy the code

How to rename files in Git:

This command grades the changes and then waits for a commit message.

git mv oldfile newfile
Copy the code

How to ignore files in Git:

Create a.gitignore file and commit itCopy the code

How to restore unstaged changes in Git:

git checkout filename
Copy the code

How to restore phased changes in Git:

You can specify the changes you want to reset using the -p option flag.

git reset HEAD filename
git reset HEAD -p
Copy the code

How to modify a recent commit in Git:

Git commit-amend allows you to modify and add the latest committed changes.

git commit -amend
Copy the code

! ! Attention! ! Fixing local commits with Amend is great, and you can push them to a shared repository after fixing them. However, you should avoid modifying commits that are already public.

How to rollback the last commit in Git:

Git Revert creates a new commit that is the opposite of everything in a given commit.

We can restore the latest commit using the head alias like this:

git revert HEAD
Copy the code

How to roll back old commits in Git:

You can use its commit ID to restore the old commit. This opens the editor so that you can add a commit message.

git revert comit_id_here
Copy the code

How to create a branch in Git

By default, you have only one branch, the main branch. Using this command, you can create a new branch. Git won’t switch to it automatically — you’ll need to do it manually using the next command.

git branch branch_name
Copy the code

How to switch to a newly created branch in Git:

You can use this command when you want to use a different branch or a newly created branch:

git checkout branch_name
Copy the code

How to list branches in Git:

You can use the git branch command to view all branches created. It displays a list of all branches, with the current branch marked with an asterisk and highlighted in green.

git branch
Copy the code

How to create a branch in Git and switch to it immediately:

In a single command, you can immediately create and switch to a new branch.

git checkout -b branch_name
Copy the code

How to delete branches in Git:

When you have finished working on a branch and merged it, you can remove it using the following command:

git branch -d branch_name
Copy the code

How to merge two branches in Git:

To merge the history of your current branch with branch_name, use the following command:

git merge branch_name
Copy the code

How to display commit logs graphically in Git:

We can use –graph to get a graphical display of the commit log. At the same time,

Oneline will limit the commit message to oneline.

git log --graph --oneline
Copy the code

How to display all branches of the commit log in Git:

Do the same as the command above, but apply to all branches.

git log --graph --online --all
Copy the code

How to abort merge conflicts in Git:

If you want to throw out a merge and start over, you can run the following command:

git merge --abort
Copy the code

How do I add a remote repository in Git

This command adds the remote repository to the local repository (simply replace https://repo_here with the remote repo URL).

git add remote https://repo_here
Copy the code

How to view remote urls in Git:

Use this command to view all remote repositories of the local repository:

git remote -v
Copy the code

Learn more about how to get a remote repO in Git:

Simply replace Origin with the name of the remote server you obtained

Run the git remote -v command.

git remote show origin
Copy the code

How to push changes to a remote repo in Git:

When all your work is ready to be saved to a remote repository, you can push all changes with the following command:

git push
Copy the code

How to get changes from Git’s remote repo:

If other team members are working with your repository, you can retrieve the latest changes made to the remote repository using the following command:

git pull
Copy the code

How to check remote branches tracked by Git:

This command displays the names of all remote branches of the current repository that Git is tracking:

git branch -r
Copy the code

How to get remote repO changes in Git:

This command downloads the changes from the remote repo, but does not perform the merge on the local branch (as Git pull does).

git fetch
Copy the code

How do I check the remote REPO for currently committed logs in Git

Git creates a log after each commit. Use this command to find the remote repository log:

git log origin/main
Copy the code

How to merge a remote and local repO in Git:

If the remote repository has changes that you want to merge with the local repository, this command will do it for you:

git merge origin/main
Copy the code

How to get the contents of a remote branch in Git without automatically merging:

This allows you to update the remote without merging anything into the local branch. You can do this by calling Git Merge or Git Checkout.

git remote update
Copy the code

How to push a new branch to a remote repo in Git:

If you want to push the branch into a remote repository, use the following command. Just remember to add -u to create the upstream branch:

git push -u origin branch_name
Copy the code

How to delete a remote branch in Git:

If you no longer need the remote branch, you can remove it using the following command:

git push --delete origin branch_name_here
Copy the code

Git rebase

Git rebase can be used to move completed work from one branch to another.

git rebase branch_name_here
Copy the code

Git Rebase can become very messy if not done properly. Before using this command, I recommend you re-read the official documentation (git-scm.com/book/it/v2/…).

How to run Rebase interactively in Git:

You can interactively run Git Rebase using the -i flag.

It opens the editor and displays a set of commands that you can use.

git rebase -i master
# p, pick = use commit
# r, reword = use commit, but edit the commit message
# e, edit = use commit, but stop for amending
# s, squash = use commit, but meld into previous commit
# f, fixup = like "squash", but discard this commit's log message
# x, exec = run command (the rest of the line) using shell
# d, drop = remove commit
Copy the code

How to force a push request in Git:

This command will force a push request. This is generally good for pull request branches, because no one else should clone them. But that’s not what you want to do with a public warehouse.

git push -f
Copy the code

At the end

These commands can significantly increase your productivity in Git. You don’t have to remember them all — that’s why I wrote this cheat sheet. Bookmark this page for future reference or print it out if you wish.

Thanks for reading!