Skip to the content.

Git Commands Tutorial

This repository provides a comprehensive guide to common Git commands, covering essential tasks for version control and collaboration.

Basic Commands:

Init

Creating a new Git repository

git init

Clone

Cloning an existing repository

git clone <repository-url-address>

Add

Adding files to the staging area

git add <file-name> or ('.' that means all files)

Commit

Committing changes to the local repository

Status

Checking the current status of the repository

git status

Log

Viewing the commit history

Diff & Show

Comparing changes between commits

Branching and Merging

Branch

Creating and listing branches

Checkout

Switching between branches

git checkout <branch-name>

Merge

Merging branches

git merge <branch-name>

Rebase

Reordering commits on a branch

Cherry pick

git cherry-pick is a powerful command that enables arbitrary Git commits to be picked by reference and appended to the current working HEAD.
Cherry picking is the act of picking a commit from a branch and applying it to another.

git cherry-pick <commit-hash>
# Steps
1. Find hash-key from `git log`
2. Checkout to the main branch
3. Use cherry-pick command to pick and commit it to main branch

Remote Repositories

Remote

Adding and listing remote repositories

Push

Pushing commits to a remote repository

Pull

Pulling changes from a remote repository

Fetch

Fetching changes from a remote repository without merging

Other Useful Commands

Reset

Resetting the current branch to a specific commit

# basic
git reset HEAD
# soft mode: 
git reset --soft <commit-id>
# hard mode
git reset --hard <commit-id>

Revert

Reversing a commit

we should only use git revert if we want to apply the inverse of a particular commit. It doesn’t revert to the previous state of a project by removing all subsequent commits, it simply undoes a single commit. git revert doesn’t move ref pointers to the commit that we’re reverting, which is in contrast to other ‘undo’ commands, such as git checkout and git reset. Instead, these commands move the HEAD ref pointer to the specified commit.

git revert <commit-id>

Stash

Temporarily saving changes

The git stash command takes your uncommitted changes (both staged and unstaged), saves them away for later use, and then reverts them from your working copy - if you create new file and this file have not been staged in git, then when use stash command, this file is ignored by git

Tag

Creating tags for specific commits

Bisect

The Git Bisect command performs a binary search to detect the commit that introduced a bug or regression in the project’s history
1. start the bisect -> git bisect start
2. define bad commit -> git bisect bad (current branch)
3. define good commit -> git bisect good <commit_hash>
4. then bisect starts to move between commits in the range of bad and good commits to find the commit which is a bug introduced there.
5. after finding the bug, stop the bisect -> git bisect reset
6. then you can check that commit and revert or fix it with a new commit.
7. [Tutorial for Bisect] (https://www.youtube.com/watch?v=D7JJnLFOn4A&t=367s)

Global Config

git config --global user.name "pouya pouryaie"
git config --global user.email "pouyapouryaie@gmail.com"

# change default branch name while creating new git repository
git config --global init.defaultBranch main

Reset Index

reset index : git rm -r --cached .

Some Common Process

back file to latest station

  1. add file to stage
  2. git reset HEAD
  3. git checkout – file name

alias

add command for use with alias name
ex: git config --global alias.fulllog "log --oneline --graph --decorate --all"
then in use: git fulllog

github

How to push or pull on a repository at github

  1. first you need create ssh-key : ssh-keygen
  2. copy content in id_rsa-public in github account ssh-key

push scenario oh Github

  1. if you are not repo in local:
    • echo “# gitdemo” » README.md
    • git init
    • git add README.md
    • git commit -m “first commit”
    • git remote add origin git@github.com:PouyaPouryaie/gitdemo.git
    • git push -u origin master
  2. if you are have repo in local
    • git remote add origin git@github.com:PouyaPouryaie/gitdemo.git
    • if your repo is another server : $ git remote add origin ssh://[serverUser]@[serverIp]:/home/pouya/myrepo
    • git push -u origin master
  3. if you created branch in local and you want push that as new branch in remote repo
    • git push -u origin branchName
  4. if you are push from local in repo anytime
    • git push origin master
  5. if you want use simple push command
    • git config –global push.default simple
    • git push
  6. if you want send tag to remote repo
    • git push origin tagName
    • git push –tags (send all tags)
  7. if you want Edit tag in remote repo
    • git push –force origin tagName
  8. if you want Remove tag from remote repo
    • git push –delete origin tagname

fetch & pull

rename remote repo in local

git remote set-url origin newAddress(newName)

track remote branch on local branch

git branch --set-upstream-to=origin/<remote-branch> <local-branch>

How to create a branch in remote and use it

  1. first create a branch in remote repository
  2. fetch in local repo and see the list of branch in remote repository: git fetch
  3. checkout the branch on your local: git checkout <branch-name>
  4. work on the branch and then commit in local: git commit -am "" (this commit done on the branch-name)
  5. push branch on the remote repository: git push
  6. push branch on the new branch in remote: git push <remote> <local-branch-name>:<remote-branch-name>

some remove scenario

change origin and push all-things in new origin

  1. Setup new origin url for local repo
    git remote set-url origin <new-url>
    
  2. push all your branch and tags into new repo with one of below command:
    git push origin '*:*'
    git push origin --all