Git Cheat Sheet

Photo by RetroSupply on Unsplash

Git Cheat Sheet

A few basic terms which help to understand git commands .

repository It keeps all your project’s files, including commits and branches.

branch It is a copy of the repository holding the specific version. The main branch in git is master.

commit It may be imagined as a single save of changes to the specific branch.

checkout It is the operation of switching between the current branch and the one specified in the command.

master It is the main branch of the repository.

merge It is an action that adds changes from one branch to another.

fork It is a copy of the repository.

head It is the most recent commit of the repository you work with.

Basic git commands everyone should know

git init | git init [folder]

It is used to initialize an empty repository . Often used while starting a new project .

git clone [repo URL] [folder]

It is used to copy the existing repository to the specified folder on your computer.

git add [directory | file]

Git add stage all changes in the directory or in the file, and it depends on what you add as a parameter. In most cases, it’s followed by git commit and git push commands.

git commit -m "[message]"

It is used to commit all staged changes with the custom message passed as a string. By changing -m parameter to -am it’s possible to add and commit changes at once.

git push

This is the command, which pushes changes to the origin branch.

git status

It is used to check the status of the modified files and it shows which files are staged, unstaged, and untracked.

git log

Git log is used to display the history of the commit in the default format.

git diff

Git diff shows all unstaged differences between the index and the current directory. It display differences between staging files and the most recent versions.

git pull

Git pull is used to get changes from the original branch, and it merges the changes into the local branch.

git fetch

This command retrieves the most recent changes from the origin branch but doesn’t merge.

Git branch commands

git branch

It displays the list of all branches in the repository. Also create a non-existing branch if you add a branch name as a parameter.

git branch -d [branchname]

Using -d flag will delete the branch with the specified branch name.

git checkout [branchname]

This command switch to the branch named [branchnamed]. If you add -b flag before branch name, it will checkout to a new branch, which will be created automatically.

git merge [branchname]

It merges the branch with the specified branch name to the current branch.

Git undoing changes commands

git revert [commit]

This command creates a new commit that undoes changes made in the specified commit and applies it to the current branch.

git reset [filename]

It remotes specifies a file from the staging and leaves the working directory unchanged.

Git config commands

git config -global user.email [user_email]
git config -global user.name [user_name]

The commands above are used to set current user email and name configuration.

git config --global --edit

And this command is very useful, as it allows for editing user configuration in a text editor.

For more depth please refer the official documentation of git which is free and available to everyone .

Thanks for reading .