2

Git Cheat Sheet: Essential Commands for Effective Code Management

 1 year ago
source link: https://dev.to/kanani_nirav/git-cheat-sheet-essential-commands-for-effective-code-management-2a32
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client
Kanani Nirav

Posted on May 16

Git Cheat Sheet: Essential Commands for Effective Code Management

In this blog post, I have tried to cover some basic Git commands that I used frequently in my work with Git. Git is a powerful tool that helps you manage your code or files over time. With Git, you can create snapshots of your project, compare and merge different versions, collaborate with other developers, and revert mistakes.

What is Git?

Git is a system that stores your project history on your own machine and lets you work without needing an internet connection. Git creates snapshots of your project called commits, which have unique code and messages. You can also create branches, which are like copies of your project that you can work on separately. You can also connect to online copies of your project called remote repositories, which you can share with others or back up your work.

Git Cheat Sheet

To use Git, you need to install it on your machine and set up some basic information. You also need to learn some commands that you can type in your terminal or command prompt to talk to Git. Here are some of the most common and useful commands:

Git configuration

Before you start using Git, you need to tell it who you are and how you want it to work.

  • $ git config: Get and set configuration variables that control all facets of how Git looks and operates.
  • $ git config --global user.name "User name": Set the name
  • $ git config --global user.email "[email protected]": Set the email
  • $ git config --global core.editor "your-editor": Set the default editor
  • $ git config --global color.ui auto: Turns on color output for git commands
  • $ git config --list: see all your settings

Create Project

To start a new project with Git, you need to make a folder for your project and turn it into a Git repository.

  • $ git init: Create a local repository
  • $ git clone: Clone remote repository

Local Changes

To make changes to your project files locally, you need to use some commands to track, stage, and commit them.

  • $ git add file-path: Adds a file to the staging area, which means that it is ready to be committed
  • $ git add .: Add all files to the staging area
  • $ git commit -m " Commit Message": Commit the file in the version history with a message

Track Changes

To keep track of your project history and see what changes were made by whom and when, you need to use some commands to view and navigate through your commits.

  • $ git diff: Shows the differences between your repository and the stage
  • $ git diff --staged: Shows the differences between the stage and the last commit
  • $ git diff HEAD: Track the changes after committing a file
  • $ git diff < branch 2>: Track the changes between two commits
  • $ git status: Display the state of the working directory and the staging area
  • $ git show: Show the newest commit on a branch
  • $ git diff -- file-path: Show Particular File changes
  • $ git diff branch-1 branch-2 --name-only: Show difference between two branch
  • $ git diff branch-1 branch-2 -- file-path: Show the differences between two branches for a specific file

Commit History

To modify or manipulate your commit history, you need to use some commands that allow you to rewrite or rearrange your commits.

  • $ git log: Display the most recent commits and the status of the head
  • $ git log -oneline: Display the output as one commit per line
  • $ git log -stat: Displays the files that have been modified
  • $ git log -p: Display the modified files with location

Ignoring files

Specify intentionally untracked files that Git should ignore. Create .gitignore:

$ touch .gitignore

List the ignored files:

# Ignore all .txt files in the current directory
*.txt

# Ignore all .log files in any subdirectory
**/*.log

# Ignore all files in the data directory except data.csv
data/*
!data/data.csv

# Ignore all files that start with temp or backup
temp*
backup*

A collection of useful .gitignore templates for different languages or frameworks: gitignore templates

Branching

To create parallel versions of your project that you can work on separately, you need to use some commands to create, switch, and delete branches.

Git branch

  • $ git branch: Show a list of all branches in your local repository
  • $ git branch -a: Show all branches in both local and remote repositories
  • $ git branch -r: Show only remote branches
  • $ git branch name: Creates a new branch called name based on the current commit
  • $ git branch name hash: Create a new branch based on a specific commit identified by its hash

Git checkout

  • $ git checkout branch-name: Switch between branches in a repository
  • $ git checkout -b branch-name: Create a new branch and switch to it

Git stash

Switch branches without committing the current branch.

  • $ git stash: Stash current work
  • $ git stash save "your message": Saving stashes with a message
  • $ git stash list: Check the stored stashes
  • $ git stash apply: Re-apply the changes that you just stashed
  • $ git stash show: Track the stashes and their changes
  • $ git stash pop: Re-apply the previous commits
  • $ git stash drop: Delete a most recent stash from the queue
  • $ git stash clear: Delete all the available stashes at once
  • $ git stash branch: Stash work on a separate branch

Merging

To combine the changes from different branches into one, you need to use some commands to merge them.

Git merge

  • $ git merge name: Merges branch called name into the current branch, by creating a new commit that incorporates both changes
  • $ git merge --abort: Aborts the merge process and restores the original state of your project, if there are any conflicts or errors during the merge

Git rebase

  • $ git rebase: command used in Git to apply a sequence of commits from one branch to another
  • $ git rebase -continue: to continue the rebasing process after you have resolved conflicts manually
  • $ git rebase --skip: to skip a commit when rebasing

Git Rebase vs Git Merge, Which One is the Best Option?

Remote

To connect your local repository with an online copy of your project, you need to use some commands to manage your remote repositories.

  • $ git remote: This shows a list of all remote repositories that are associated with your local repository, and their names and URLs
  • $ git remote -v: Check the configuration of the remote server
  • $ git remote show name: Show information about a specific remote repository called name
  • $ git remote add origin repo-url: Add a remote for the repository
  • $ git remote rm: Remove a remote connection from the repository
  • $ git remote rename: Rename remote server
  • $ git remote show: Show additional information about a particular remote
  • $ git remote set-url name url: Changes the URL of a remote repository called name to url

Pushing Updates

To send your local changes to a remote repository, you need to use some commands to push them.

  • $ git push origin master: Push data to the remote repository
  • $ git push --all: Push all branches to the default remote repository
  • $ git push --all origin: Specify the remote repository explicitly
  • $ git push -f: Force push data to the remote repository

Pulling updates

Git pull

To receive changes from a remote repository to your local repository, you need to use some commands to pull them.

  • $ git pull: Pull the changes from the default remote repository and branch, usually origin and master respectively
  • $ git pull origin master: Specify the remote repository and branch explicitly

Git fetch

Download branches and tags from one or more repositories.

  • $ git fetch <repository Url>: Fetch the remote repository
  • $ git fetch: Fetch a specific branch
  • $ git fetch -all: Fetch all the branches simultaneously
  • $ git fetch origin: Synchronize the local repository

Undo changes

To undo or revert some changes in your project, you need to use some commands that allow you to reset, revert, or restore your files or commits.

Git revert

  • $ git revert HEAD: Undo the latest commit
  • $ git revert hash: Creates a new commit that undoes the changes made by a specific commit identified by its hash
  • $ git revert branch: Undo all commits on a branch

Git reset

  • $ git reset --soft hash: Resets your current branch to a specific commit identified by its hash, but keeps your working directory and staging area unchanged, which means that you can still commit or modify your changes later
  • $ git reset --mixed hash: Resets your current branch to a specific commit identified by its hash, and resets your staging area to match it, but keeps your working directory unchanged, which means that you can still modify or add your changes later
  • $ git reset --hard hash: Resets your current branch to a specific commit identified by its hash, and resets your working directory and staging area to match it, which means that you lose any changes you made since then.

Removing Files

To remove some files from your project, you need to use some commands that allow you to delete them from your working directory, staging area, or history.

  • $ git rm file: Delete a file from both your working directory and staging area, and stages the deletion for the next commit.
  • $ git rm --cached file: Delete a file only from the staging area but not from the working directory.

For a table of all commands, please see below. You can also start or fork this GitHub gist if you like.

Git Cheat Sheet

Git Configuration

Command Description
$ git config πŸ› οΈ Get and set configuration variables for Git.
$ git config --global user.name "User name" ✏️ Set the name.
$ git config --global user.email "[email protected]" βœ‰οΈ Set the email.
$ git config --global core.editor "your-editor" πŸ“ Set the default editor.
$ git config --global color.ui auto 🌈 Turns on color output for Git commands.
$ git config --list ℹ️ See all your settings.

Create Project

Command Description
$ git init 🏁 Create a local repository.
$ git clone πŸ”„ Clone a remote repository.

Local Changes

Command Description
$ git add file-path βž• Adds a file to the staging area.
$ git add . βž• Add all files to the staging area.
$ git commit -m " Commit Message" πŸ’Ύ Commit file permanently in the version history.

Track Changes

Command Description
$ git diff πŸ”„ Shows the differences between your folder and the stage.
$ git diff --staged πŸ”„ Shows the differences between the stage and the last commit.
$ git diff HEAD πŸ”„ Track the changes after committing a file.
$ git diff <branch-2> πŸ”„ Track the changes between two commits.
$ git status ℹ️ Display the state of the working directory and the staging area.
$ git show ℹ️ Show the newest commit on a branch.
$ git diff -- file-path πŸ”„ Show changes for a particular file.
$ git diff branch-1 branch-2 --name-only πŸ”„ Show difference between two branches (file names only).
$ git diff branch-1 branch-2 -- file-path πŸ”„ Show differences between two branches for a specific file.

Commit History

Command Description
$ git log πŸ“œ Display the most recent commits and the status of the head.
$ git log -oneline πŸ“œ Display the output as one commit per line.
$ git log -stat πŸ“œ Displays the files that have been modified.
$ git log -p πŸ“œ Display the modified files with location.

Ignoring files

Command Description
$ touch .gitignore πŸ“ Create a file named .gitignore to list ignored files.

A collection of useful .gitignore templates for different languages or frameworks: gitignore

Branching

Git branch

Command Description
$ git branch 🌿 Show a list of all branches in your local repository.
$ git branch -a 🌿 Show all branches in both local and remote repositories.
$ git branch -r 🌿 Show only remote branches.
$ git branch name 🌿 Creates a new branch called name based on the current commit.
$ git branch name hash 🌿 Create a new branch based on a specific commit identified by its hash.

Git checkout

Command Description
$ git checkout branch-name πŸ”€ Switch between branches in a repository.
$ git checkout -b branch-name πŸ”€ Create a new branch and switch to it.

Git stash

Command Description
$ git stash πŸ“¦ Stash current work.
$ git stash save "your message" πŸ“¦ Saving stashes with a message.
$ git stash list πŸ“¦ Check the stored stashes.
$ git stash apply πŸ“¦ Re-apply the changes that you just stashed.
$ git stash show πŸ“¦ Track the stashes and their changes.
$ git stash pop πŸ“¦ Re-apply the previous commits.
$ git stash drop πŸ“¦ Delete the most recent stash from the queue.
$ git stash clear πŸ“¦ Delete all the available stashes at once.
$ git stash branch πŸ“¦ Stash work on a separate branch.

Merging

Git merge

Command Description
$ git merge name 🀝 Merges branch called name into the current branch, creating a new commit that incorporates both changes.
$ git merge --abort 🀝 Aborts the merge process and restores the original state of your project, if there are any conflicts or errors during the merge.

Git rebase

Command Description
$ git rebase πŸ”€ Apply a sequence of commits from one branch to another.
$ git rebase -continue πŸ”€ Continue the rebasing process after resolving conflicts manually.
$ git rebase --skip πŸ”€ Skip a commit when rebasing.

Remote

Command Description
$ git remote 🌐 Show a list of all remote repositories associated with your local repository.
$ git remote -v 🌐 Check the configuration of the remote server.
$ git remote show name 🌐 Show information about a specific remote repository called name.
$ git remote add origin repo-url 🌐 Add a remote for the repository.
$ git remote rm 🌐 Remove a remote connection from the repository.
$ git remote rename 🌐 Rename a remote server.
$ git remote show 🌐 Show additional information about a particular remote.
$ git remote set-url name url 🌐 Change the URL of a remote repository called name to url.

Pushing Updates

Command Description
$ git push origin master πŸ“€ Push data to the remote repository.
$ git push --all πŸ“€ Push all branches to the default remote repository.
$ git push --all origin πŸ“€ Specify the remote repository explicitly.
$ git push -f πŸ“€ Force push data to the remote repository.

Pulling Updates

Git pull

Command Description
$ git pull πŸ“₯ Pull changes from the default remote repository and branch.
$ git pull origin master πŸ“₯ Specify the remote repository and branch explicitly.

Git fetch

Command Description
$ git fetch <repository URL> πŸ“₯ Fetch the remote repository.
$ git fetch πŸ“₯ Fetch a specific branch.
$ git fetch --all πŸ“₯ Fetch all branches simultaneously.
$ git fetch origin πŸ“₯ Synchronize the local repository.

Undo Changes

Git revert

Command Description
$ git revert HEAD βͺ Undo the latest commit.
$ git revert hash βͺ Create a new commit that undoes changes made by a specific commit identified by its hash.
$ git revert branch βͺ Undo all commits on a branch.

Git reset

Command Description
$ git reset --soft hash βͺ Reset your current branch to a specific commit identified by its hash, keeping your working directory and staging area unchanged.
$ git reset --mixed hash βͺ Reset your current branch to a specific commit identified by its hash, resetting your staging area to match it, but keeping your working directory unchanged.
$ git reset --hard hash βͺ Reset your current branch to a specific commit identified by its hash, resetting your working directory and staging area to match it, discarding any changes made since then.

Removing Files

Command Description
$ git rm file πŸ—‘οΈ Delete a file from both your working directory and staging area, staging the deletion for the next commit.
$ git rm --cached file πŸ—‘οΈ Delete a file only from the staging area, but not from the working directory.

Reference: Git Docs

Reference: Git Docs

If You are using Medium Please support and follow me for interesting articles. Medium Profile

If this guide has been helpful to you and your team please share it with others!


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK