Git commands

Most common git commands i use on a daily basis

Β·

2 min read

Introduction.

In this article, I will be talking about common Git commands I use on a daily basis I hope you find this helpful.

Starter git templates

```jsx
echo "# next-template" >> README.md
git init
git add README.md
git commit -m "first commit"
git branch -M main
git remote add origin git@github.com:addegbenga/next-template.git
git push -u origin main
```

To Create a Repository

  • Initialize a project folder as a git repository

      git init
    
  • Download the repository into your local storage

      git clone <HTTPS link or SSH key>
    
  • To Delete already initialized repository

      rm -rf .git
    

Remote Repositories

  • Display the connected remote repositories

      git remote
    
  • To connect to the remote repository

      git remote add origin <URL>
    
  • Push all the commits to the remote repository

      git push <remote> <branch name>
    

Branches

  • List all the local branches of the repo

      git branch
    
  • Create a new branch locally

      git branch <branch name>
    
  • Delete the branch named branch_name

      git branch -d <branch name>
    
  • Create and switch to the new branch

      git checkout -b <branch name>
    
  • Switch to the branch which already exists

      git checkout <branch name>
    
  • Merge the provided branch with the current working branch

      git merge <branch name>
    
  • Abort the actions if there are any conflicts

      git merge abort
    

To Observe Your Repository

  • Display the state of the working directory and the staging area

      git status
    
  • To get the status in short form

      git status -s
    
  • Display changes between commits of between commit and saved files

      git diff
    
  • Display commit logs and diff output each commit introduces

      git whatchanged
    

To Make Changes

  • Stage the un-staged file

      git add <file>
    
  • Stage all files together

      git add .
    
  • To un-staged files

      git reset
    
  • Commit staged files.

      git commit -m "<message>"
    
  • Revert back to the previous commit

      git reset -hard
    
  • Take uncommitted changes (staged and un-staged) and save them away for later use. Later then reverts them from working copy

      git stash
    
Β