An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
GitSheet - my git cheatsheet
GitSheet
a cheatsheet that's lived in my notepad for too long
Add existing local project to GitHub
$ git init
$ git add .
$ git commit -m <message>
$ git remote add origin <github-repo-url>
$ git remote -v
//sanity check$ git push -u origin <master>
//or other branch
Add a file afterwards$ git add .
$ git status
//sanity check$ git commit -m <message>
$ git push origin branch-name
If you accidentally initialise a git repo in a folder that shouldn't be under version control
Revert the change by removing the hidden .git folder$ rm -rf .git
Updating file
Use $ git add <file>
to update what will be committed
If you don't want these changes and just want to go back to the version of this file at the time of the last commit
Use $ git checkout --<file>
to discard changes in working directory
Sanity checks: run$ git status
before you commit, to confirm that the repo is in the state you expect it to be.
On remotes
Create new branch with switch$ git switch -c branch name
(is the same as $ git checkout -b <branchname>
)
Push a new branch up to remote $ git push -u origin <branchname>
Set a new remote$ git remote add origin https://github.com/user/repo.git
Verify new remote$ git remote -v
Navigating The Log
To view all of the commits we've made, we run:$ git log
$ git log --oneline
//keep things simpler
(TLDR use or enter/return key to move further down, $ q
to quit)
If you're not used to a pager on the command line, navigating inLesscan be a bit odd.
- to scrolldown, press
- jorto movedownone line at a time
- dto move by half the page screen
- fto move by a whole page screen
- to scrollup, press
- korto move up one line at a time
- uto move by half the page screen
- bto move by a whole page screen
- pressqtoquitout of the log (returns to the regular command prompt)
More Sanity checks
Stats$ git log --stat
- displays the file(s) that have been modified
- displays the number of lines that have been added/removed
- displays a summary line with the total number of modified files and lines that have been added/removed
$ git log -p
(or patch)
Shows the diff between files changed
This command adds the following to the default output:
- displays the files that have been modified
- displays the location of the lines that have been added/removed
- displays the actual changes that have been made
You can "log" information with:$ git log
$ git log --oneline
$ git log --stat
$ git log -p
You can supply the SHA of a commit as the final argument
ie. $ git log -p fdf5493
git log -p
command willstart at that commit & show all commits madepriorto the supplied SHA
Thegit showcommand will showonly one commit.
To unstage: $ git rm cashed <file>
$ git diff
displays:
- the files that have been modified
- the location of the lines that have been added/removed
- the actual changes that have been made
On gitignore
Add the file inside the.gitignorefile: ie. info.md
In the.gitignorefile, you can use the following:
- blank lines can be used for spacing
- #- marks line as a comment
- *- matches 0 or more characters
- ?- matches 1 character
- [abc]- matches a, b, or c
- *- matches nested directories -a/*/zmatches
- a/z
- a/b/z
- a/b/c/zSo if all of the 50 images are JPEG images in the "samples" folder, we could add
samples/*.jpg
to.gitignoreto have Git ignore all images.
On git tag
Git tag
$ git tag -a beta
This command will:
- add a tag to the most recent commit
- add a tag to a specific commitif a SHA is passed$ git tag -a v1.0git log --decorateA Git tag can be deleted with the-dflag (fordelete!) and the name of the tag:$ git tag -d v1.0But what if you wanted to tag a commit that occurred farther back in the repo's history?All you have to do is provide the SHA of the commit you want to tag!$ git tag -a v1.0 a87984
On branches
git branch can be used to:
- list all branch names in the repository
- create new branches
- delete branchesif we type out just
$ git branch
it will list out the branches in a repositoryif you want a branch called "something", you'd run this command:$ git branch something
to switch between branches:$ git checkout something
this command will: - remove all files and directories from the Working Directory that Git is tracking (files that Git tracks are stored in the repository, so nothing is lost)
- go into the repository and pull out all of the files and directories of the commit that the branch points to
DELETE a branch$ git branch -d something
to list all branches:$ git branch
to create a new "footer-fix" branch:$ git branch footer-fix
to delete the "footer-fix" branch:$ git branch -d footer-fix
See All Branches At Once$ git log --oneline --decorate --graph --all
On merging
$ git merge <name-of-branch-to-merge-in>
- look at the branches that it's going to merge
- look back along the branch's history to find a single commit thatbothbranches have in their commit history
- combine the lines of code that were changed on the separate branches together
- makes a commit to record the merge
There are two types of merges:
- fast-forward merge the branch being merged in must beaheadof the checked out branch. The checked out branch's pointer will just be moved forward to point to the same commit as the other branch.
- regular merge
- two divergent branches are combined
- a merge commit is created
On merge fails / merge conflics:
A merge conflict will happen whenthe exact same line(s)are changed in separate branches
Thegit statusoutput tells us to that the merge conflict is insideindex.html. So check out that file in your code editor!
Merge Conflict Indicators Explanation
The editor has the following merge conflict indicators:
- <<<<<<< HEADeverything below this line (until the next indicator) shows you what's on the current branch
- ||||||| merged common ancestorseverything below this line (until the next indicator) shows you what the original lines were
- =======is the end of the original lines, everything that follows (until the next indicator) is what's on the branch that's being merged in
- >>>>>>> heading-updateis the ending indicator of what's on the branch that's being merged in (in this case, theheading-updatebranch)
- choose which line(s) to keep
- remove all lines with indicatorsTo resolve the conflict in a file:
- locate and remove all lines with merge conflict indicators
- determine what to keep
- save the file(s)
- stage the file(s)
- make a commit
commit amend
Now with the--amendflag, you can alter themost-recentcommit.$ git commit --amend -m 'your text'
If your Working Directory is clean (meaning there aren't any uncommitted changes in the repository) running$ git commit --amend
will let you provide a new commit message.
Add Forgotten Files To Commit
- edit the file(s)
- save the file(s)
- stage the file(s)
- and rungit commit --amend
for the 'OH NO' moments
Resetting Is Dangerous
Resetting erasescommits!
Ancestry References
that we can use to tell Git about relative references
- ^ indicates the parent commit
- ~ indicates thefirstparent commitHere's how we can refer to previous commits:
- the parent commit the following indicate the parent commit of the current commit
- HEAD^
- HEAD~
- HEAD~1
- the grandparent commit the following indicate the grandparent commit of the current commit
- HEAD^^
- HEAD~2
- the great-grandparent commit the following indicate the great-grandparent commit of the current commit
- HEAD^^^
- HEAD~3
commit revert
When you tell Git toreverta specific commit, Git takes the changes that were made in commit and does the exact opposite of them
Git will make a new commit$ git revert <SHA-of-commit-to-revert>
This command:
- will undo the changes that were made by the provided commit
- creates a new commit to record the change
$ git reset <reference-to-commit>
OR use the ancestry refs
For example if you made a merge on the wrong branch, use this command to undo the merge:$ git reset --hard HEAD^
Make sure to include the^character, the "Relative Commit Reference" indicates "the parent commit".
It can be used to:
- move the HEAD and current branch pointer to the referenced commit
- erase commits
- move committed changes to the staging index
- unstage committed changesThe way that Git determines if it erases, stages previously committed changes, or unstages previously committed changes is by the flag that's used. The flags are:
- --mixed (the default, places changes back in the working dir)
- --soft (will put them into the staging are)
- --hard (will throw out all changes)*** Good idea: a backup branch, in case u mess up
$ git branch backup
Reset Recap
To recap, thegit resetcommand is used erase commits:
$ git reset
It can be used to:
- move the HEAD and current branch pointer to the referenced commit
- erase commits with the--hardflag
- moves committed changes to the staging index with the--softflag
- unstages committed changes--mixedflagTypically, ancestry references are used to indicate previous commits. The ancestry references are:
- ^ indicates the parent commit
- ~ indicates the first parent commit
This cheatsheet has lived in my notepad for some time.
Of course all this info is out there on the wild web, certainly better written & explained. And there's a lot more to git and life-saving commands.
I'd love to know them & list them all but I prefer to know what I need to know at every moment, so this is where I'm at right now. I like my quick ref cheatsheets & so I am sharing it.
Regrettably I do not have refs of where all these snippets came from. Some are c/p, some are mine, many are from the official Atlassian guides.
Read up, then make your own cheatsheet :)
- Atlassian git tutorials
- Oh Shit, Git! I love this one
next:
rebasing & squashing cheatsheet in the making
Original Link: https://dev.to/myrtle/gitsheet-my-git-cheatsheet-4o18
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To