GIT Cheatsheet: Difference between revisions

From UNIX Systems Administration
Jump to navigation Jump to search
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
== GIT Cheatsheet ==
== GIT Cheatsheet ==
# Initial setup git Global Configuration Variables
* Initial setup git Global Configuration Variables
#:<tt>'''These configuration variables are stored in ~/.gitconfig'''</tt>
#:<tt>'''These configuration variables are stored in ~/.gitconfig'''</tt>
#:<tt>'''$ git config --global user.name "<user>"'''</tt>
#:<tt>'''$ git config --global user.name "<user>"'''</tt>
Line 7: Line 7:
#:<tt>'''$ git config --list'''</tt>
#:<tt>'''$ git config --list'''</tt>


# Initialize a new git Repository
* Initialize a new git Repository
#:<tt>'''$ cd into directory to be added to the repository'''</tt>
#:<tt>'''$ cd into directory to be added to the repository'''</tt>
#:<tt>'''$ git init'''</tt>
#:<tt>'''$ git init'''</tt>
Line 15: Line 15:
#:<tt>'''$ git push origin master'''</tt>
#:<tt>'''$ git push origin master'''</tt>


# Cloning a Remote Repository
* Cloning a Remote Repository
#:<tt>'''$ git clone ssh:<url> <src/.>'''</tt>
#:<tt>'''$ git clone ssh:<url> <src/.>'''</tt>
#:<tt>'''$ git remote -v'''</tt>
#:<tt>'''$ git remote -v'''</tt>
#:<tt>'''$ git branch -a'''</tt>
#:<tt>'''$ git branch -a'''</tt>


# Creating Branches
* Creating Branches
#:<tt>'''$ git branch <branch name>'''</tt>
#:<tt>'''$ git branch <branch name>'''</tt>
#:<tt>'''$ git checkout <branch name>'''</tt>
#:<tt>'''$ git checkout <branch name>'''</tt>

Revision as of 18:34, 2 April 2026

GIT Cheatsheet

  • Initial setup git Global Configuration Variables
  1. These configuration variables are stored in ~/.gitconfig
    $ git config --global user.name "<user>"
    $ git config --global user.email "<email>"
    $ git config --list
  • Initialize a new git Repository
  1. $ cd into directory to be added to the repository
    $ git init
    $ git add -A
    $ git commit -m "Repository Initialization"
    $ git remote add origin ssh:<url>
    $ git push origin master
  • Cloning a Remote Repository
  1. $ git clone ssh:<url> <src/.>
    $ git remote -v
    $ git branch -a
  • Creating Branches
  1. $ git branch <branch name>
    $ git checkout <branch name>
    $ git branch

Merging Branches

       git checkout master
       git pull origin master
       git branch --merged
       git merge <branch>
       git push origin master

Deleting a Branch

       git branch --merged
       git branch -d <branch to delete>
       git branch -a
       git push origin --delete <branch to delete>

Update a file

       git add <file>
       git commit -m "Description of changes"
       git push

Commiting & Pushing Changes

       git diff
       git status
       git add -A
       git commit -m "<message>"
       git pull origin <branch>
       git push origin <branch>

Remove files from repository keep local

       git rm --cached <file>
       git commit -m "Description of changes"
       git push

List files in repository

       git ls-files

Revert to previous version (pre commit)

       git checkout <filename>
       git status
       git diff

Fix commit message

       git commit --ammend -m "<message>"

Add a file as a part of previous commit

       git commit --ammend

Move commits to another branch

       Get Hash
               git log
       git checkout <branch>
       git cherry-pick <hash>
       Remove the commit from the original branch (soft reset), removing commits but keeping staging directory in branch
               git checkout <original branch>
               git log
               git reset --soft <hash of correct commit>
               git status
       Remove the commit from the original branch (mixed reset), removing commits and files in staging directory in branch
               git checkout <original branch>
               git reset <hash>
               git status
       Remove the commit from the original branch (hard reset), removing commits and tracked files entirely in branch
               git checkout <original branch>
               git reset --hard <hash>
               git status

Remove Untracked Directories and Files

       git clean -df
       git status