DEV Community

Cover image for Git Cheat Sheet
Buddhadeb Chhetri
Buddhadeb Chhetri

Posted on

Git Cheat Sheet

Important Commands of Git.
1)What is Git?
2)Why should we use git and GitHub?
3)How to Download?
4)Git Operations?

  • How to create a git Directory?
  • How to Stage?
  • How to do your first Commit?

5)Important Git commands?

1. Git- Git is a version control system (VCS) that lets you manage and keep track of your changes that you make to files.

2. Why we use:- GitHub is a website for hosting projects that use git.
Git tracks the changes you make to files, so you have a record of what has been done, and you can revert to specific versions should you ever need to. Git also makes collaboration easier, allowing changes by multiple people to all be merged into one source.

3. Download- Click the Blue mark download this will take you to the download page of git.
Alt Text
4. Git Operations -There are main three operations that every programmer have to do in Git.

Alt Text
6. Git commands - The most used commands are

i) CREATE

Create a new local repository

git init
Enter fullscreen mode Exit fullscreen mode

Clone an existing Repository

git clone ssh://user@domain.com/repo.git
Enter fullscreen mode Exit fullscreen mode

ii)LOCAL CHANGES

Change files in your working directory

git status
Enter fullscreen mode Exit fullscreen mode

Changes to tracked files

git diff
Enter fullscreen mode Exit fullscreen mode

Add all current changes to the next commit

git add .
Enter fullscreen mode Exit fullscreen mode

Add some changes in <file> to the next commit

git add -p <file>
Enter fullscreen mode Exit fullscreen mode

Commit all local changes in tracked files

git commit -a
Enter fullscreen mode Exit fullscreen mode

Commit previously staged changes

git  commit
Enter fullscreen mode Exit fullscreen mode

Change the last commit
Don't amend published comments

git commit --amend
Enter fullscreen mode Exit fullscreen mode

iii)COMMIT HISTORY

Show all commits , starting with newest

git log
Enter fullscreen mode Exit fullscreen mode

Show changes over time for a specific file

git log -p <file>
Enter fullscreen mode Exit fullscreen mode

Who changed what and when in <file>

git blame <file>
Enter fullscreen mode Exit fullscreen mode

iv) BRANCHES & TAGS

List of all existing branches

git branch -av
Enter fullscreen mode Exit fullscreen mode

Switch HEAD branch

git checkout <branch name>
Enter fullscreen mode Exit fullscreen mode

Create a new branch based on your current HEAD

git branch <new-branch>
Enter fullscreen mode Exit fullscreen mode

Create a new tracking branch based on a remote branch

git checkout --track <remote/branch>
Enter fullscreen mode Exit fullscreen mode

Delete a local branch

git branch -d <branch name>
Enter fullscreen mode Exit fullscreen mode

Mark the current commit with a tag

git tag <tag-name>
Enter fullscreen mode Exit fullscreen mode

v) Update & Publish

List of all Currently configured remotes

git remote -v
Enter fullscreen mode Exit fullscreen mode

Show information about a remote

git remote show <remote>
Enter fullscreen mode Exit fullscreen mode

Add new remote repository ,named <remote>

git remote add <shortname> <url>
Enter fullscreen mode Exit fullscreen mode

Download all changes from <remote>, but don't integrate into HEAD

git fetch <remote>
Enter fullscreen mode Exit fullscreen mode

Pull all changes from remote, merge/intregrate into HEAD

git pull <remote> <branch name>
Enter fullscreen mode Exit fullscreen mode

Publish local changes on a remote

git push <remote> <branch name>
Enter fullscreen mode Exit fullscreen mode

Delete a branch on the remote

git branch -dr <remote/branch name>
Enter fullscreen mode Exit fullscreen mode

Publish your tags

git push --tags
Enter fullscreen mode Exit fullscreen mode

vi) MERGE & REBASE

MERGE <BRANCH> INTO YOUR CURRENT HEAD

git merge <branch name> 
Enter fullscreen mode Exit fullscreen mode

Rebase your current HEAD onto <branch>
_Don't rebase publish commits _

git rebase <branch>
Enter fullscreen mode Exit fullscreen mode

Abort a rebase

git rebase --abort
Enter fullscreen mode Exit fullscreen mode

Continue a rebase after resolving conflicts

git rebase --continue
Enter fullscreen mode Exit fullscreen mode

Use your configured mege tool to solve conflicts

git mergetool
Enter fullscreen mode Exit fullscreen mode

vi)UNDO

Discard all local changes in your working directory

git reset --hard HEAD
Enter fullscreen mode Exit fullscreen mode

Discard local changes in a specific file

git checkout HEAD <file>
Enter fullscreen mode Exit fullscreen mode

Revert a commit

git revert <commit>
Enter fullscreen mode Exit fullscreen mode

Reset your HEAD pointer to a previous commit

git reset --hard <commit>
Enter fullscreen mode Exit fullscreen mode

Preserve all changes as unstaged changes

git reset <commit>
Enter fullscreen mode Exit fullscreen mode

Preserve uncommitted local changes

git reset --keep <commit>
Enter fullscreen mode Exit fullscreen mode

Top comments (0)