Skip to content

xenioushk/useful-git-commands

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

27 Commits
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Useful & Frequently Used git Commands

This documentation provides a quick setup process of Git and a few useful commands.

βœ… Git application setup

  • Download git application from https://git-scm.com
  • Install it on your computer.
  • Open the terminal (Mac) or Poweshell (Windows).
  • Once done, run the following command to check the git version.
git --version

or

git -v

πŸ‘‰ Output

git version check output

βœ… Setup git user name and email

The following commands allow you to set username and email address globally. πŸ€—

git config --global user.name "Your Name"
git config --global user.email "Your Email"

You can also set specific username and email address for your current project. πŸ’ͺ

git config user.name "Your Name"
git config user.email "Your Email"

πŸ‘‰ Validate/Check user information

git config --global user.name
git config --global user.email

πŸ‘‰ Initalize a git repository

The following command allows you to initialize a fresh local git repository to your project.

git init

πŸ‘‰ Check git status

The following command will display the current status of the added/updated/deleted files.

git status

πŸ‘‰ git push & pull commands

git add -A

git commit -m "your_custom_commit_message_in_here"

git commit -am "your custom_commit_message_in_here"

git remote add origin "git_remote_repository_url"

//set upstream. (rename the remote from origin to upstream)

git remote rename origin upstream

// Using -u, you can set the upstream branch for the current branch. e.g main
git push -u origin main

// Pull any changes from remote main branch
git pull origin main

git clone "git_remote_repository_url"

// Clone a specific branch from a remote repository
// -b means, Gitll will check out the staging branch right after cloning.
git clone -b "staging" "git_remote_repository_url"

git reset "index.html"

// Check current origin
git remote -v

//Set a new origin.
git remote set-url origin "git_remote_repository_url"

βœ… Git branch commands

  1. List of all branches.
git branch -a
  1. Show current branch.
git branch --show-current
  1. Create a new branch and switch to that branch
git checkout -b "new_branch_name"
  1. Rename current branch.
git branch -m "new_renamed_branch_name"
  1. Delete a branch. (locally)
git branch -d "deleted_branch_name"
  1. Delete a branch. (remote)
git push origin -d "<BRANCH_NAME>"
  1. Switch to a branch.
git checkout "branch_name"
  1. Merge a branch.
git merge "branch_name"

βœ… Git tag commands.

Using git tag commands you can easiliy mark stable version of a project. It creates an image of your git repository. To tag a branch, you need to checkout a stable branch. e.g main

git checkout branch "main"
git tag v1.0

Add message to the tag

git tag -a v1.0 -m "this is a stable 1.0 version of my project"

Show all the tags

git tag

Push a tag

git push origin v1.0

Push multiple tag

git push origin v1.0 v1.1

Push all tags

git push --tags

Delete a tag (local)

git tag -d v1.0

Delete multiple tag (local)

git tag -d v1.0 v1.1

Delete a tag (remote)

git push origin -d v1.0

βœ… Git commit log

Using the following commands, you can check all previous commits hash, commits date, author.

Check all logs

git log

Last 3 git logs

git log -3

Output

git version check output

git logs in graphical mode

git log --graph

Output

git version check output

git logs in oneline mode

git log --oneline

Output

git version check output

Fix fatal: refusing to merge unrelated histories issue

  • Run the following command. (Check origin name, for mine it's the main)
git pull origin main --allow-unrelated-histories
  • After running this code a popup window will appear. Press 'wq' from keyboard.
  • It's done. Now you can push your code to the remote repository.

Remove a file/folder from git tracking

git rm -r --cached 'FOLDER_NAME'

Update last git commit message.

If you accidentally added a Git commit message or would like to update the last Git commit message, then use the following command:

git commit --amend -m "Updated commit message"

Bonus Commands For Mac

  1. Create a new directory
mkdir YOUR_DIR_NAME
  1. Create an empty file.
touch YOUR_FILE_NAME
  1. Read A File.
cat YOUR_FILE_NAME

Bonus Commands For Windows

  1. Create a directory
mkdir YOUR_DIR_NAME
  1. List of folder and files in a directory.
ls YOUR_DIR_NAME
  1. Create an empty file.
echo YOUR_FILE_NAME

Acknowledgement

About

This documentation provides a quick setup process of Git and a few useful commands.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 2

  •  
  •  
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy