Git cheatsheet 2023
Tags: git, career, javascript, opensource
Published: 2023-01-23 10:35:24

Here is a list of some important Git commands along with one example for each:

git init - Initialize a new Git repository

git add - Add files to the repository's staging area
Example: git add . (to add all files in the current directory)

git commit - Create a new snapshot of the repository
Example: git commit -m "Initial commit" (to commit with the message "Initial commit")

git log - View the commit history of the repository
Example: git log (to view the commit history)

git diff - View the differences between the current state of the files and the last committed version
Example: git diff (to view the differences)

git revert - Create a new commit that undoes the changes made in a previous commit
Example: git revert <commit-id> (to revert to a specific commit)

git reset - Discard commits and move the branch pointer to a previous commit
Example: git reset --hard <commit-id> (to reset the branch to a specific commit)

git push - Upload the repository to a remote server
Example: git push origin master (to push the master branch to the "origin" remote)

git clone - Create a copy of a remote repository
Example: git clone https://github.com/username/repository.git

git merge - Combine changes made in different branches
Example: git merge feature (to merge the changes made in the "feature" branch into the current branch)

git branch - View and manage branches
Example: git branch -a (to view all branches, including remote branches)

git remote - View and manage remote repositories
Example: git remote -v (to view the list of remotes associated with the repository)

git tag - Create and manage tags
Example: git tag -a v1.0 -m "Initial release" (to create a new tag "v1.0" with the message "Initial release")

git stash - Save changes temporarily without committing them
Example: git stash save "Work in progress" (to save the changes with the message "Work in progress")

git cherry-pick - Select specific commits from one branch and apply them to another branch
Example: git cherry-pick <commit-id> (to apply a specific commit to the current branch)

git config - Configure Git settings
Example: git config --global user.name "John Doe" (to set the user name as "John Doe" globally)

git fetch - Download changes from a remote repository
Example: git fetch origin (to download changes from the "origin" remote)

git pull - Download changes from a remote repository and merge them with the local branches
Example: git pull origin master (to download changes from the "origin" remote's "master" branch and merge them with the local "master" branch)

git status - View the status of the repository
Example: git status (to view the status of the current repository)

git blame - Show who last modified each line of a file
Example: git blame <file> (to view the last modification of each line of a specific file)

git ls-files - Show information about files in the index and the working tree
Example: git ls-files --stage (to list all the files in the staging area and the status of each file)

git clean - Remove untracked files from the working tree
Example: git clean -df (to remove all untracked files and directories)

git mv - Move or rename a file, a directory, or a symlink
Example: git mv <file> <new-location> (to move a file to a new location)

git submodule - Initialize, update or inspect submodules
Example: git submodule add <repository> <directory> (to add a submodule to the current repository)

git lfs - Large file support extension for Git
Example: git lfs install (to install Git LFS extension)

git rebase - Reapply commits on top of another base tip
Example: git rebase -i <branch> (to rebase the current branch onto the specified branch interactively)

git archive - Create a tar or zip archive of the contents of the repository
Example: git archive -o <file>.zip HEAD (to create a zip archive of the contents of the repository)

git gc - Cleanup unnecessary files and optimize the repository
Example: git gc (to perform a garbage collection on the current repository)

git filter-branch - Rewrite the entire history of the repository
Example: git filter-branch --tree-filter 'rm -rf <directory>' HEAD (to remove a directory from the entire history of the repository)

git bisect - Find the commit that introduced a bug
Example: git bisect start (to start the bisect process)

git switch - Switch branch or create a new branch
Example: git switch -c <branch> (to create a new branch and switch to it)

git restore - Restore working tree files
Example: git restore <file> (to restore a specific file to its previous state)

The git switch command allows you to switch to an existing branch or create a new branch. This command is similar to git checkout but it also allows you to create a new branch with the -c option.

The git restore command allows you to restore files in your working tree to their previous state. This command can be used to restore a specific file, a directory or even the entire repository. It's similar to git checkout but it's more powerful and flexible.

It's important to note that, git switch is available since git version 2.23 and git restore is available since git version 2.25, if you are using an older version of git, these commands might not be available.

These commands are more advanced, but they can be very useful in specific scenarios. Git LFS can help to manage large files, git rebase and filter-branch can be used to rewrite the repository history and git bisect can help to find the commit that introduced a bug. As with the previous commands, it is important to understand the underlying concepts and the implications of each command before using them

Wishing you guys a lucky 2023! Please give a like!!