Restore a file from a commit, to the current active commit
git reset HEAD file.txt
Un-stage a file, so it no longer is in the Staging Area
git revert 5f2d8e4fd0b9234b9aadac7aca32bee02f3
Revert: 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_branch
create a new Git branch from a branch name
git checkout -b justin a9c146a09505837ec03b
create a new Git branch from an old commit
git reset –soft 4ff01e7
Soft: Moves the HEAD
git reset –mixed 4ff01e7
Mixed (default reset)
Changes HEAD and Staging index
git reset –hard 4ff01e7
Hard Dangerous Reverts completely back to commit
git reset –hard
Undo 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~1
Undo a Git merge that hasn’t been pushed yet
git clean -n
Remove untracked files: with -n flag, you can preview the files you want to remove
git clean -f
With -f flag they will be removed completly. Removes untracked files that has not been”.add’ed”
git rm –cached file.txt
Fjerner 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
Put 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 branch
Show Local branches
git branch -r
Show remote branches
git branch -a
Show local + remote branches
git branch new_feature
Create a new branch called “new_feature”. The name can be: Letters, numbers, underscore [az_09]
git checkout -b funny_feature
Create a new branch, and Swith HEAD to “funny_feature” branch
git checkout new_feature
Swith HEAD to “new_feature” branch
git branch –merged
Show all branches (HEAD commits) that is merged into current branch
git branch –no-merged
Show all branches (HEAD commits) that is NOT merged into current branch
git branch -r –merged
Show all remote branches (HEAD commits) that is merged or not into current branch
git branch -m old_branch_name new_branch_name
renames / move (hince the -m or –move) the branch
git branch -d branch_to_delete
deletes (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_delete
allways use the -d option, unless you really want to -D delete the branch, without merges etc.
git push -d origin branch_to_delete
Deletes remote branch
git fetch –prune
Carefull – 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-run
Shows a preview of deleted remote branches
git remote prune origin
Updates my local index of all remote branches
Merge
Merge:
git merge branch_name
Merge a branch by name
git merge –abort
If you are in the middle of a merge, you can cancel it this way.
git commit
If 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 list
Show a list of my staches
git stash show stach@{0}
Shows the diff
git stash show
Shows filepath to files, that differ
git stash show -p
Shows 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 clear
Removes 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 remote
List all remotes the computer knows
git remote -v
List all remotes the computer knows, and push/fetch information
git branch -r
git remote show origin
list 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 master
Local -> Remote | Use this the first time you add a remote. It will create the remote and push the code. It will also enable tracking.