100+ Git Kommandoer

Git Config

git config –global core.editor “code –wait” git config –global -eset vscode as default editor for git
git config –global color.ui true
git initNew: git is tracking
git config –global user.name “Your Name”

git config –global user.email “youremail@yourdomain.com”
Set your name and email
.git/configFile: Project file configuration
.git/headFile: this file shows the currect “Head” branch
.git/head : Points to the directory in .git/…heads / …branchFile: where the sha-1 file commit description is

Git Workflow

1. Make change, 2. add the change (to staging), 3. commit changes 4. Push to cloudWorking > Staging index > repository

Git Add and Commit

git add .add all files in the project (to the staging index)
git add file.txtadd file.txt to project (to the staging index)
git commit -m “Initial commit”do commit | from Staging > repository. the “-m” stands for message
git commit -m “Initial commit next line there can be a long description, use * to do bullets”do commit
commit naming: [css,js], bugfix: , #38405, Add feature.., Inital commit
git commit -am “Initial commit”add + commit Only for modified files. Does not work well for new or deleted files. Because Add does not track if files has been created or deleted
git commit –amend -m “Change last commit message”Change last commit message.
git commit –allow-emptyEmpty commit without any file changes, without commit message
git commit -a –allow-empty-message -m “”Empty commit without any file changes, with commit message

Git Log and Status

git statusMessages: Untracked files:   files created in Working, but not yet added to Stageing
git logLogs all commits
git log -p file.txtShows all changes to a file (shows a diff for all changes)
git log –stat –summaryShow an overview of changed files, in all commits
git show 9c5d34a08bShows the diff for this commit
git log –onelineNice overview of commit messages
git log –oneline -5Show the last 5 commits
git log –name-only –pretty=oneline –full-index HEAD^^..HEAD | grep -vE ‘^[0-9a-f]{40} ‘ | sort | uniqShow files that has changed since last commit
git log -n 2Show a commit number
git reflogShow all commits, including the id.. Head 1,2,3,4,5,6 information
git log –since=10.days –until=0.daysSee all commits between
git log –since=2018-05-25See all commits between since date
git log –until=2018-05-25See all commits between before date
git log –since=2018-02-20 –until=2018-05-25See all commits between two dates
git log –author=”Joe”See all commits from Joe
git log –grep=”Init”logs commits after a regex search term
git log Headthe first commit in this branch
git log –graph –oneline –all –decorateShow commit timeline short
git log –graph –abbrev-commit –decorate –allShow commit timeline detailed

Git Diff

git diffshows the diff in the working directory, before commit. use keyboar [f,b,space] to go [forward,backward, space=forward]
git diff file.txtShows the diff between HEAD and last commit
git diff –stagedcompare staged vs repository
git diff –color-wordsviser diff’en på samme line for små ændringer
git diff cf78008..cdae0ed file.txtShows the diff for a file between two commit-hashes. Use the .. between the two hashes
git diff 5f2d8e4..abf91c6Show all changes between two commits. Use the .. between the two hashes
git diff –stat –summary cf06d76..HEADShows a summery / overview of files that changed since this commit
git diff branchA..branchBShow diffs between two branches
git diff –color-words branchA..branchBviser diff’en på samme line for små ændringer
qquit diff mode

Misc

git add file.txt git rm file.txt git commit …..When you deleted a file in exporer: add the file before commit rm Remove a file from the directory rember to commit that you deletet a file
git rm file.txt git commit ..use this when you want to delete it from the commandline. Its a hard delete


git add newname.txt git rm oldname.txt git commit ..Rename: this is how you rename a file
git mv oldname.txt newname.txtRename or move a file with git (mv = move)

Git Checkout, Reset, Clean, rm

git checkout 9c5d34a08b — file.txtRestore a file from a commit, to the current active commit
git reset HEAD file.txtUn-stage a file, so it no longer is in the Staging Area
git revert 5f2d8e4fd0b9234b9aadac7aca32bee02f3Revert: Will go back to a earlier commit for the whole working direcory. VsCode: If you have multiple changes and want to apply all of them at once – open command palette (View -> Command Palette) and start typing merge – multiple options will appear including Merge Conflict: Accept Incoming / Accept All Incoming

Revert: Takes a previus command and makes it the HEAD. A good way to undo a merge.
git checkout -b new_branch
git checkout -b new_branch from_branchcreate a new Git branch from a branch name
git checkout -b justin a9c146a09505837ec03bcreate a new Git branch from an old commit
git reset –soft 4ff01e7Soft: Moves the HEAD
git reset –mixed 4ff01e7Mixed (default reset)
 Changes HEAD and Staging index
git reset –hard 4ff01e7Hard Dangerous Reverts completely back to commit
git reset –hardUndo like behavior. 1. you did a commit. 2 you made some changes without commiting, and you regret the changes and want to revert back to your last commit. UNDO local file changes but NOT REMOVE your last commit.
git reset –hard HEAD~1Undo a Git merge that hasn’t been pushed yet


git clean -nRemove untracked files: with -n flag, you can preview the files you want to remove
git clean -fWith -f flag they will be removed completly. Removes untracked files that has not been”.add’ed”


git rm –cached file.txtFjerner filen fra staging, men beholder filen i working directory. Godt hvis du også vil tiføje filen til gitignore. Removes the file from index alone and keeps it in your working copy. This is the exact opposite of git add file

Git Ignore

.gitignore :
tempfile.txt
.DS_Store
*.zip
*.gz
log/.log log/.log.[0-9]
assets/photoshop/
assets/videos/
assets/videos/tour_*.mp4

https://github.com/github/gitignore

https://help.github.com/articles/ignoring-files/

.gitkeepPut a gitkeep file in a directory you want to keep. Often used if there is no other file in the direcory. Git does only track files, not folders. So you will use the gitkeep file in empty directorys

Branch

git branchShow Local branches
git branch -rShow remote branches
git branch -aShow local + remote branches
git branch new_featureCreate a new branch called “new_feature”. The name can be: Letters, numbers, underscore [az_09]
git checkout -b funny_featureCreate a new branch, and Swith HEAD to “funny_feature” branch
git checkout new_featureSwith HEAD to “new_feature” branch
git branch –mergedShow all branches (HEAD commits) that is merged into current branch
git branch –no-mergedShow all branches (HEAD commits) that is NOT merged into current branch
git branch -r –mergedShow all remote branches (HEAD commits) that is merged or not into current branch
git branch -m old_branch_name new_branch_namerenames / move (hince the -m or –move) the branch
git branch -d branch_to_deletedeletes (hince the -d or –delete) the branch. Remember to switch to another branch before deleting: git checkout some_other_branchname
git branch -D branch_to_deleteallways use the -d option, unless you really want to -D delete the branch, without merges etc.
git push -d origin branch_to_deleteDeletes remote branch
git fetch –pruneCarefull – Localy i can have many snapshots of remote branches, but if another person deletes a remote branch, then I need to run Prune command, to update my locale index pf what branches is availible on remote. Im not shure, but maybe it also deletes the local branches..?
git remote prune origin –dry-runShows a preview of deleted remote branches
git remote prune originUpdates my local index of all remote branches

Merge

Merge:
git merge branch_nameMerge a branch by name
git merge –abortIf you are in the middle of a merge, you can cancel it this way.
git commitIf there are merge conflicts, then fix the the conflict, and then run ”git commit” to complete the merge

Stach

Stash:Git stach starts with 0. Git stash is used to save temperary files, that is not ready to be commited, or you want to move to another branch. Stach is availible in all branches.
git stash save “my stuff to stach”Save files temporary. + Git reset hard head
git stash listShow a list of my staches
git stash show stach@{0}Shows the diff
git stash showShows filepath to files, that differ
git stash show -pShows file path and diff
git stash show -p stach@{0}Shows detailed diff
git stash pop stash@{0}Moves files from stash to working directory and then delete the stash
git stash apply stash@{0}Moves files from stash to working directory but keeps the stash
git stash drop stash@{0}Delete filers in stash by id
git stash clearRemoves all stash’es

Remote Branch and Push

Remote:Every remote uses a alias, often called ”origin”, in the following i will write <alias> to indicate the remote name
git remoteList all remotes the computer knows
git remote -vList all remotes the computer knows, and push/fetch information
git branch -r
git remote show originlist all local brances and there push/pull branches
git remote add <remoteAlias> <url>Add a remote adress with alias ex: git remote add origin https://github.com/myname/myproject.git
git remote rm <remoteAlias>Removes a remote, ex: “git remote rm origin”
git push -u origin masterLocal -> Remote | Use this the first time you add a remote. It will create the remote and push the code. It will also enable tracking.
git push –forcelokale branch overwriite remote branch
git clone <url>
git clone <url> <toFoldernameOnDisk>foldernameondisk: clone url to folder on disk
git clone https://www.github.com/martin/project.git from_jamesClones to a folder called ”from_james”
git branch non_tracking && git push origin non_trackingCreates a new branch, where tracking is disabled
push -u / cloneWill allways enable tracking of changes on remote
git log –oneline origin/masterShow commit history on remote branch
git diff origin/master..masterCompare remote branch with local version
git push origin masterWhen a branch allready have tracking enabled, you can push like this, without extra parameters

Note: git pull = git fetch + git merge

On this website we use first or third-party tools that store small files (<i>cookie</i>) on your device. Cookies are normally used to allow the site to run properly (<i>technical cookies</i>), to generate navigation usage reports (<i>statistics cookies</i>) and to suitable advertise our services/products (<i>profiling cookies</i>). We can directly use technical cookies, but <u>you have the right to choose whether or not to enable statistical and profiling cookies</u>. <b>Enabling these cookies, you help us to offer you a better experience</b>.