Git Cheat Sheet: Difference between revisions

From Wayne's Dusty Box of Words
(Created page with "{| class="wikitable" |+ !Setup & Connections ! |- |<code>git remote add origin <YourRemoteURL></code> |Connect to a remote repository |- |<code>git remote -v</code> |List rem...")
 
No edit summary
 
Line 1: Line 1:
{| class="wikitable"
{| class="wikitable" style="width: 100%;"
|+
|+ Setup & Connections
!Setup & Connections
!
|-
|-
|<code>git remote add origin <YourRemoteURL></code>
| style="width: 50%"| <code>git remote add origin <YourRemoteURL></code>
|Connect to a remote repository
|Connect to a remote repository
|-
|-
Line 24: Line 22:
|<code><nowiki>git config --[local | global] user.email "Your Email"</nowiki></code>
|<code><nowiki>git config --[local | global] user.email "Your Email"</nowiki></code>
|Set the committer email in gitconfig
|Set the committer email in gitconfig
|}
{| class="wikitable" style="width: 100%;"
|+ Tracking & Staging Files
|-
| style="width: 50%"| <code>git status</code>
|Shows you your current branch and changed files
|-
|<code>git diff <ChangedFileName></code>
|Shows the changes in the file
|-
|<code>git add <ChangedFileName></code>
|Stages a particular file
|-
|<code>git reset HEAD <ChangedFileName></code>
|Unstages a particular file
|-
|<code>git reset --hard</code>
|Resets the entire branch to head
|-
|<code>git reset --hard <Commit_ID></code>
|Resets the entire branch to before that commit
|-
|<code>git add .</code>
|Stage all changed files
|}
{| class="wikitable" style="width: 100%;"
|+ Commit, Log & Push Files
|-
| style="width: 50%"| <code>git commit -m "Message"</code>
|Creates a commit with that message
|-
|<code>git push origin <YourBranchName></code>
|If the branch exists on remote omit YourBranchName
|-
|<code>git push -u origin <YourBranchName></code>
|set upstream branch using the <code>git push</code> command
|-
|<code>git log</code>
|Lists all logs step by step
|-
|<code>git log --oneline</code>
|Plain list of commits
|}
{| class="wikitable" style="width: 100%;"
|+ Branching
|-
| style="width: 50%"| <code>git branch <BranchName></code>
|Creates a branch
|-
|<code>git push origin <BranchName></code>
|push the branch to remote
|-
|<code>git push origin --delete <YourBranchName></code>
|deletes the branch from remote
|-
|<code>git branch -r</code>
|Lists all remote branches
|-
|<code>git branch</code>
|Lists all local branches
|-
|<code>git branch -a</code>
|Lists all local & remote branches
|-
|<code>git checkout -b <BranchName></code>
|Checkout to local branch
|-
|<code>git branch -d <BranchName></code>
|Deletes branch
|-
|<code>git branch -d <BranchName1> <BranchName2> <BranchName3></code>
|Deletes multiple branches
|-
|<code>git merge <YourLocalBranch></code>
|merges local branch
|-
|<code>git merge origin <YourRemoteBranch></code>
|merges remote branch
|-
|<code>git fetch --prune</code>
|align remote branch with local
|}
{| class="wikitable" style="width: 100%;"
|+ Staging
|-
| style="width: 50%"| <code>git stash save <MyStashName></code>
|Stashes all staged changes
|-
|<code>git stash list</code>
|List all stashes (w/ IDs)
|-
|<code>git stash drop stash@(ID)</code>
|Removes that stash
|-
|<code>git stash clear</code>
|Removes '''ALL''' stashes
|-
|<code>git stash apply stash@(ID)</code>
|Merges that shash to the current branch
|-
|<code>git stash merge stash@(ID)</code>
|Immediate merge, not recommended
|}
{| class="wikitable" style="width: 100%;"
|+ Reverts & Updates
|-
| style="width: 50%"| <code>git log --onetime</code>
|Lists all of your commits. Copy <commit hash> from list
|-
|<code>git stash revert <ID></code>
|reverts that commit
|-
|<code>git commit --amend --no-edit</code>
|Fix a commit by <code>git add ./</code> and them this to update the previous commit
|}
|}

Latest revision as of 19:28, 22 October 2020

Setup & Connections
git remote add origin <YourRemoteURL> Connect to a remote repository
git remote -v List remote link
git remote rm <YourRemoteURL> Remove remote link
git config --[local | global] user.name Display the committer name in gitconfig
git config --[local | global] user.email Display the committer email in gitconfig
git config --[local | global] user.name "Your Name" Set the committer name in gitconfig
git config --[local | global] user.email "Your Email" Set the committer email in gitconfig
Tracking & Staging Files
git status Shows you your current branch and changed files
git diff <ChangedFileName> Shows the changes in the file
git add <ChangedFileName> Stages a particular file
git reset HEAD <ChangedFileName> Unstages a particular file
git reset --hard Resets the entire branch to head
git reset --hard <Commit_ID> Resets the entire branch to before that commit
git add . Stage all changed files
Commit, Log & Push Files
git commit -m "Message" Creates a commit with that message
git push origin <YourBranchName> If the branch exists on remote omit YourBranchName
git push -u origin <YourBranchName> set upstream branch using the git push command
git log Lists all logs step by step
git log --oneline Plain list of commits
Branching
git branch <BranchName> Creates a branch
git push origin <BranchName> push the branch to remote
git push origin --delete <YourBranchName> deletes the branch from remote
git branch -r Lists all remote branches
git branch Lists all local branches
git branch -a Lists all local & remote branches
git checkout -b <BranchName> Checkout to local branch
git branch -d <BranchName> Deletes branch
git branch -d <BranchName1> <BranchName2> <BranchName3> Deletes multiple branches
git merge <YourLocalBranch> merges local branch
git merge origin <YourRemoteBranch> merges remote branch
git fetch --prune align remote branch with local
Staging
git stash save <MyStashName> Stashes all staged changes
git stash list List all stashes (w/ IDs)
git stash drop stash@(ID) Removes that stash
git stash clear Removes ALL stashes
git stash apply stash@(ID) Merges that shash to the current branch
git stash merge stash@(ID) Immediate merge, not recommended
Reverts & Updates
git log --onetime Lists all of your commits. Copy <commit hash> from list
git stash revert <ID> reverts that commit
git commit --amend --no-edit Fix a commit by git add ./ and them this to update the previous commit