====== git command cheatsheet ======
===== unstage files (if you have "git add" the wrong file) =====
git restore --staged some.file
===== revert 1 or more local commits (not yet pushed) =====
$ git reset --soft HEAD~1
Unstaged changes after reset:
D somefolder/somefile
M somefolder/someother.file
===== revert to a previous commit (already pushed) =====
git reset --hard
===== git reset details =====
git reset [] []
* [] is the commit identifier (hash or reference) to reset to.
* [] are optional flags that modify the behavior of the reset command.
Some possible options:
--hard: Reset the repository to the specified commit and discard all changes in the working tree.
--soft: Reset the repository to the specified commit, but keep changes in the working tree.
--mixed (default): Reset the repository to the specified commit, but keep changes in the index (staging area).
--merge: Reset the repository to the specified commit and keep changes that have been made in the working tree.
--keep: Keep changes in both the working tree and the index (staging area).
===== Use git stash to move code between branches =====
A common use case that I use git stash for is when I make changes in the wrong branch that are bigger than just a few lines and thus not so easy to copy/paste into the next branch.
Here is how I do it:
git stash // stash your changes
git checkout feature_branch // Navigate to your feature branch (or create and checkout a new branch: git checkout -b feature_branch
git stash apply // apply the stashed changes
git add .
git commit -m "Your commit message here"
git stash drop // delete the stash in case yuo no longer need it
===== other common commands =====
* ''git init'': Initialize a new Git repository.
* ''git clone '': Clone an existing Git repository.
* ''git add '': Stage changes to a file or directory for the next commit.
* ''git reset '': Unstage changes to a file or directory.
* ''git commit -m ""'': Commit staged changes with a message.
* ''git log'': Display the commit history of the repository.
* ''git status'': Display the current status of the repository, including staged and unstaged changes.
* ''git diff'': Display the difference between the current state and the last commit.
* ''git checkout '': Check out a different branch or commit.
* ''git branch'': Display a list of local branches.
* ''git branch '': Create a new branch.
* ''git checkout -b '': Create and checkout a new branch.
* ''git merge '': Merge changes from another branch into the current branch.
* ''git rebase '': Rebase the current branch on top of another branch.
* ''git fetch'': Download new changes from the remote repository.
* ''git pull'': Fetch and merge changes from the remote repository.
* ''git push'': Upload local changes to the remote repository.
===== .git/config =====
Random stuff one can use in their git config file: ~/somerepo/.git/config
==== User details ====
[user]
email = username@example.com
name = username
You can add this manually, or use the cli commands to populate the file with this information, like so:
git config user.email "username@example.com"
git config user.name "username"
==== Aliases ====
Setting up some short-cut commands for frequently used commands
[alias]
st = status
ci = commit
br = branch
co = checkout
df = diff
lg = log -p
lol = log --graph --decorate --pretty=oneline --abbrev-commit
kama = log --graph --pretty=format:\"%h - %cr - %cN - %s - %b\" --all
ls = ls-files
==== Formatting ====
Funky colors
[color]
ui = auto
[color "branch"]
current = yellow reverse
local = yellow
remote = green
[color "diff"]
meta = yellow bold
frag = magenta bold
old = red bold
new = green bold
whitespace = red reverse
[color "status"]
added = yellow
changed = green
untracked = cyan