Git Commands Cheat Sheet

Madhav Ganesan - Jun 18 - - Dev Community

Git is a distributed version control system (DVCS) that allows multiple developers to collaborate on a project, tracking changes to files and coordinating work seamlessly.

Initializing a Repository:

git init
Enter fullscreen mode Exit fullscreen mode

Cloning a Repository:

git clone -b <branch-name> <url> <folder-name>
Enter fullscreen mode Exit fullscreen mode

Working with Branches:

Create a new branch:

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

Switch branches:

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

Create and Switch branches:

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

List all branches:

git branch
Enter fullscreen mode Exit fullscreen mode

Staging and Committing Changes:

Add files to staging area:

git add .
Enter fullscreen mode Exit fullscreen mode

Commit changes:

git commit -m "message"
Enter fullscreen mode Exit fullscreen mode

View changes before staging:

git diff
Enter fullscreen mode Exit fullscreen mode

Remote Repositories:

Add a remote repository:

git remote add origin <remote-repository-URL>
Enter fullscreen mode Exit fullscreen mode

List all remotes:

git remote -v
Enter fullscreen mode Exit fullscreen mode

Push changes to a remote repository:

git push -u origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Pull changes from a remote repository:

git pull origin <branch-name>
Enter fullscreen mode Exit fullscreen mode

Stashing Changes:

Stashing in Git allows you to temporarily save changes that are not yet ready to be committed, so we can switch to another branch without losing your progress. It is used when you need to quickly switch contexts or update your working directory without committing incomplete work.

Stash changes:

git stash
Enter fullscreen mode Exit fullscreen mode

Apply stashed changes:

git stash apply
Enter fullscreen mode Exit fullscreen mode

Configuration:

Change user name:

git config --global user.name "name"
Enter fullscreen mode Exit fullscreen mode

Change user email:

git config --global user.email "email"
Enter fullscreen mode Exit fullscreen mode

Reset:

Soft reset:

git reset --soft HEAD^
Enter fullscreen mode Exit fullscreen mode

Moves to the previous commit and discards all changes.

Hard reset:

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

Moves to the previous commit but keeps all changes.

( HEAD is a reference to the current commit &
notation HEAD^ refers to the parent commit of the current HEAD )

. . . .