GIT Cheatsheet: Difference between revisions

From UNIX Systems Administration
Jump to navigation Jump to search
m (Michael Kohler moved page GIT to GIT Cheatsheet without leaving a redirect)
 
(14 intermediate revisions by the same user not shown)
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>
#:<tt>'''$ git config --global user.email "<email>"'''</tt>
:<tt>'''git config --global user.email "<email>"'''</tt>
#:<tt>'''$ git config --list'''</tt>
:<tt>'''git config --list'''</tt>


Initialize a new git Repository
==== Initialize a new git Repository ====
        cd into directory to be added to the repository
:<tt>'''cd into directory to be added to the repository'''</tt>
        git init
:<tt>'''git init'''</tt>
        git add -A
:<tt>'''git add -A'''</tt>
        git commit -m "Repository Initialization"
:<tt>'''git commit -m "Repository Initialization"'''</tt>
        git remote add origin ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/outlands
:<tt>'''git remote add origin ssh:<url>'''</tt>
        git push origin master
:<tt>'''git push origin master'''</tt>


Cloning a Remote Repository
==== Cloning a Remote Repository ====
        git clone ssh://git-codecommit.us-east-2.amazonaws.com/v1/repos/outlands-lib src/.
:<tt>'''git clone ssh:<url> <src/.>'''</tt>
        git remote -v
:<tt>'''git remote -v'''</tt>
        git branch -a
:<tt>'''git branch -a'''</tt>


Creating Branches
==== Creating Branches ====
        git branch <branch name>
:<tt>'''git branch <branch name>'''</tt>
        git checkout <branch name>
:<tt>'''git checkout <branch name>'''</tt>
        git branch
:<tt>'''git branch'''</tt>


Merging Branches
==== Merging Branches ====
        git checkout master
:<tt>'''git checkout master'''</tt>
        git pull origin master
:<tt>'''git pull origin master'''</tt>
        git branch --merged
:<tt>'''git branch --merged'''</tt>
        git merge <branch>
:<tt>'''git merge <branch>'''</tt>
        git push origin master
:<tt>'''git push origin master'''</tt>


Deleting a Branch
==== Deleting a Branch ====
        git branch --merged
:<tt>'''git branch --merged'''</tt>
        git branch -d <branch to delete>
:<tt>'''git branch -d <branch to delete>'''</tt>
        git branch -a
:<tt>'''git branch -a'''</tt>
        git push origin --delete <branch to delete>
:<tt>'''git push origin --delete <branch to delete>'''</tt>


Update a file
==== Update a file ====
        git add <file>
:<tt>'''git add <file>'''</tt>
        git commit -m "Description of changes"
:<tt>'''git commit -m "Description of changes"'''</tt>
        git push
:<tt>'''git push'''</tt>


Commiting & Pushing Changes
==== Commiting & Pushing Changes ====
        git diff
:<tt>'''git diff'''</tt>
        git status
:<tt>'''git status'''</tt>
        git add -A
:<tt>'''git add -A'''</tt>
        git commit -m "<message>"
:<tt>'''git commit -m "<message>"'''</tt>
        git pull origin <branch>
:<tt>'''git pull origin <branch>'''</tt>
        git push origin <branch>
:<tt>'''git push origin <branch>'''</tt>


Remove files from repository keep local
==== Remove files from repository keep local ====
        git rm --cached <file>
:<tt>'''git rm --cached <file>'''</tt>
        git commit -m "Description of changes"
:<tt>'''git commit -m "Description of changes"'''</tt>
        git push
:<tt>'''git push'''</tt>


List files in repository
==== List files in repository ====
        git ls-files
:<tt>'''git ls-files'''</tt>


Revert to previous version (pre commit)
==== Revert to previous version (pre commit) ====
        git checkout <filename>
:<tt>'''git checkout <filename>'''</tt>
        git status
:<tt>'''git status'''</tt>
        git diff
:<tt>'''git diff'''</tt>


Fix commit message
==== Fix commit message ====
        git commit --ammend -m "<message>"
:<tt>'''git commit --ammend -m "<message>"'''</tt>


Add a file as a part of previous commit
==== Add a file as a part of previous commit ====
        git commit --ammend
:<tt>'''git commit --ammend'''</tt>


Move commits to another branch
==== Remove the commit from the original branch (soft reset), removing commits but keeping staging directory in branch ====
        Get Hash
:<tt>'''git checkout <original branch>'''</tt>
                git log
:<tt>'''git log'''</tt>
:<tt>'''git reset --soft <hash of correct commit>'''</tt>
:<tt>'''git status'''</tt>


        git checkout <branch>
==== Remove the commit from the original branch (mixed reset), removing commits and files in staging directory in branch ====
        git cherry-pick <hash>
:<tt>'''git checkout <original branch>'''</tt>
:<tt>'''git reset <hash>'''</tt>
:<tt>'''git status'''</tt>


        Remove the commit from the original branch (soft reset), removing commits but keeping staging directory in branch
==== Remove the commit from the original branch (hard reset), removing commits and tracked files entirely in branch ====
                git checkout <original branch>
:<tt>'''git checkout <original branch>'''</tt>
                git log
:<tt>'''git reset --hard <hash>'''</tt>
                git reset --soft <hash of correct commit>
:<tt>'''git status'''</tt>
                git status


        Remove the commit from the original branch (mixed reset), removing commits and files in staging directory in branch
==== Remove Untracked Directories and Files ====
                git checkout <original branch>
:<tt>'''git clean -df'''</tt>
                git reset <hash>
:<tt>'''git status'''</tt>
                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

Latest revision as of 18:50, 2 April 2026

GIT Cheatsheet

Initial setup git Global Configuration Variables

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

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

git clone ssh:<url> <src/.>
git remote -v
git branch -a

Creating Branches

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

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