Git for Beginners - A Quick Reference Guide

Published Apr 18, 2024

This is a basic guide to some common Git commands.

Setup

  • Configure your username and email:
    git config --global user.name "Your Name"
    git config --global user.email "[email protected]"
    
  • Set up color coding for better readability (optional):
    git config --global color.ui auto
    

Starting a Project

  • Initialize a new Git repository in the current directory:
    git init
    
  • Clone an existing repository from a remote URL:
    git clone https://docs.github.com/articles/cloning-a-repository
    

Working with Changes

  • Check the status of your working directory (uncommitted changes, staged changes):
    git status
    
  • Add a file to the staging area for the next commit:
    git add [file]
    
  • Unstage a file (remove from staging area):
    git reset [file]
    
  • See what changes you have made (uncommitted vs working directory, staged vs unstaged):
    git diff
    git diff --staged
    

Committing Changes

  • Create a commit with a descriptive message:
    git commit -m "Your commit message here"
    

Changing the author for last commit

  • Set email and name with git config command as shown above in setup section.
  • Run reset author command as below.
    git commit --amend --reset-author --no-edit
    
  • Run below command to push your changes.
    git push --force-with-lease
    

Branches

  • List all local branches:
    git branch
    
  • Create a new branch:
    git branch [branch-name]
    
  • Switch to a different branch:
    git checkout [branch-name]
    
  • Merge another branch into the current branch:
    git merge [branch-name]
    
  • Delete a local branch (after merging its changes):
    git branch -d [branch-name]
    

Remote Repositories

  • Fetch updates from the remote repository (doesn't merge):
    git fetch
    
  • Push your local commits to the remote repository:
    git push
    
  • Pull down changes from the remote repository and merge them into your local branch:
    git pull
    

Viewing History

  • See the commit history:
    git log
    

For More Information

#Git #VersionControl #GitTutorial #GitCheatSheet #DevOps #Programmer #Coding #CommandLine #OpenSource #FOSS

Tags: tech cheat-sheet git version control development command line tutorial devops