Skip to content

Latest commit

 

History

History
401 lines (329 loc) · 9.13 KB

README.md

File metadata and controls

401 lines (329 loc) · 9.13 KB

My Git cheatsheet

Other git cheatsheets and training courses

Delete all branches locally except for the master

git branch | grep -v "master" | xargs git branch -D

View the commit history

git log --pretty=oneline --abbrev-commit --graph --decorate
git log --graph --oneline --decorate --all

Cherry-pick

To cherry-pick all the commits from commit A to commit B (where A is older than B), run:

git cherry-pick A^..B

If you want to ignore "A" itself, run:

git cherry-pick A..B

Use '-n' flag with the cherry-picking which is "no commit"

git cherry-pick -n <HASH>

SSH

Add key to ssh-agent, check its availability with:

eval "$(ssh-agent -s)"
ssh-add ~/.ssh/github_rsa
ssh -T [email protected]

Routine work

Clone a remote repository to a local directory

git clone url folder_name

Fetch all changes from the remote server

git fetch

Pull changes from the remote branch that the local branch is tracking with

git pull

With git pull --ff-only, Git will update your branch only if it can be “fast-forwarded” without creating new commits.

git pull --ff-only

Checkout and track a remote branch

git checkout -b <branch-name> origin/<branch-name>

Diff

View changes between current uncommitted files and the last commit

git diff

View changes between current added files and the last commit

git diff --cached

Show differences between two branches

git diff <branch1> <branch2>

Show only the names of files that differ between two branches

git diff --name-only <branch1> <branch2>

Show a list of files that differ between two branches with their status (added/deleted/modified)

git diff --name-status <branch1> <branch2>

Save the diff to a file

git diff > 20150203_someChanges.diff
git diff > some-changes.patch

Apply changes from a diff file

git apply /path/to/some-changes.patch

If there are errors related to whitespaces, apply the diff file using:

git apply --reject --whitespace=fix mychanges.patch

The --reject option will instruct git to not fail if it cannot determine how to apply a patch, but instead to apply individual hunks it can apply and create reject files (.rej) for hunks it cannot apply. Additionally, --whitespace=fix will warn about whitespace errors and try to fix them, rather than refusing to apply an otherwise applicable hunk.

log

Show the last 2 commits with messages

git log -2

Show all changes to files

git log -f

Show only file names

git log --name-only

Switch HEAD to commit 332d568f3249db83baa0784f6f4738cf2842490c

git checkout 332d568f3249db83baa0784f6f4738cf2842490c

remote

Show a list of remote repositories

git remote -v

Add the remote repository remote_repo_url with the name remote_repo

git remote add remote_repo remote_repo_url:

push

Publish changes from the local master branch to the remote_branch branch of the remote repository remote_repo. If the remote_branch branch does not exist, it will be created

git push remote_repo remote_branch

Send the local serverfix branch to the awesomebranch branch of the remote project remote_repo

git push remote_repo serverfix:awesomebranch

Push the dist sub-folder to the gh-pages branch

git subtree push --prefix dist origin gh-pages

git branch

Rename the current local branch local_branch

[local_branch] git branch -m <new_name>

Rename the local branch old_name

[local_branch] git branch -m <old_name> <new_name>

Delete the local branch testing

git branch -d testing

Delete the remote branch testing in the remote repository origin

git push origin :testing
git push origin --delete testing

List remote branches

git branch -r
git remote show origin

###List local and remote branches

git branch -a

Undo changes to not added file

git checkout
git checkout [file]

Create a new local branch new_loc_branch and switch to it

git checkout -b [new_loc_branch]

Create a new local branch new_loc_branch, download the contents of the remote branch origin/rem_branch, and switch to it

git checkout origin/rem_branch -b new_loc_branch

git merge

Merge changes from local_branch_2 into local_branch_1

[local_branch_1] git merge local_branch_2

Create a new remote branch, push changes to it, and link it to the current branch.

git push --set-upstream origin module3-task1

git stash

Hide changes and return to the last commit state

git stash

Show a list of all hidden states

git stash list

To see the files in a specific stash (0 is the latest, 1 is the penultimate, etc.)

git stash show stash@{2}

To see the edits in these files, add -r

git stash show -p stash@{2}

Retrieve changes from the latest hidden state and apply them to the current version

git stash apply

Delete the latest changes in the list

git stash drop

Equivalent to apply + drop

git stash pop

Clear the list of changes

git stash clear

git config

set the Sublime as a default text editor

git config --global core.editor "'c:/program files/sublime text 3/subl.exe' -w"

Set a global name of a git user

git config --global user.name "John Doe"

Set a global email of a git user

git config --global user.email "[email protected]"

git alias (doc)

Change long checkout to co

 git config --global alias.co checkout

Change long status to st

 git config --global alias.st status

Show list of all configs

git config --list

git amend

Change the text of last commit to "Right text"

git commit --amend -m 'Right text'
git commit -a --amend

Add some changes to the last commit without editing its comment

git commit --amend --no-edit

The resulting commit will replace the incomplete commit. In this case, everything will be as if the changes in the file occurred in one commit.

git reset

Cancel adding README to the staged files for commit

$ git reset -- README

This command will undo the last commit (but not the changes you made, they will still be saved).

git reset --soft HEAD^

If the last commit is terrible, you can just remove it altogether:

git reset --hard HEAD^

If you like to reset merging conflicts during aplying the stash:

git reset --merge

Remove commit from remote repo

All of this works if you haven't published your changes yet. If you have, then the only thing left to do is to make a commit that cancels some other commit:

git revert commit-sha1
git push

git blame

Check who and when changed file file-name

git blame file-name

For example, suppose you look at git blame's output. Here -L 150,+11 means "only look at the lines 150 to 150+11"

git blame -L 150,+11 -- git-web--browse.sh

And you want to know the history of what is now line 155. Then, use git log. Here, -L 155,155:git-web--browse.sh means "trace the evolution of lines 155 to 155 in the file named git-web--browse.sh".

git log --pretty=short -u -L 155,155:git-web--browse.sh

git rm

Remove application.yml from repo, but keep it on disk

git rm --cached --ignore-unmatch application.yml
git mv oldname newname

is just shorthand for:

mv oldname newname
git add newname
git rm oldname

i.e. it updates the index for both old and new paths automatically.

push the branch to master with squashing all your commits to 1

To push my work to master on one of my previous places of work I had to do something like this

git stash
git fetch
git checkout master
git pull --ff-only
git checkout username/is15126
git checkout -b username/is15126b
git rebase master
git checkout master
git merge --squash username/is15126b
git commit -m "IS-15126: XXX"git push origin master