Skip to content

Latest commit

 

History

History
300 lines (193 loc) · 3.84 KB

git.md

File metadata and controls

300 lines (193 loc) · 3.84 KB

Git basics

Commit routine:

git status
git add ...
git status
git commit -m "${commit message}" -m "${commit description}"

 

Show changes on staged files only:

git diff --staged

 

Show file differences at a smaller scope than a line:

git diff --color-words   # word-wise
git diff --color-words=. # character-wise

 

Fix last commit message (safe for local commits):

git commit --amend -m "${new commit message}"

 

Undo last commit (safe for local commits):

git reset HEAD~1

 

List branches by date:

git branch --sort=committerdate

 

List remote branches:

git branch -r

 

Switch branches:

git checkout ${existing branch}

 

Create a new branch and switch to it:

git checkout -b ${new branch}

 

Rename current local branch:

git branch -m ${new branch name}

 

Delete a local branch:

git branch -D ${existing local branch}

 

Push new branch to remote repository:

git push -u origin ${local branch}

 

Remove branches that no longer exist on the remote, then update:

git fetch -p # or --prune

 

List files modified between two commits:

git diff --name-only ${commit A} ${commit B}

 

Create a patch, check it, apply it:

git diff ${commit A} ${commit B} > diff_fromA_toB.patch
git apply --check diff_fromA_toB.patch
git apply diff_fromA_toB.patch

 

Export one commit as a detailed patch file:

git format-patch -1 ${commit}

 

Apply version of a file from another branch:

git checkout ${reference branch} -- ${file}

 

Apply existing commit without commiting:

git cherry-pick -n ${commit}

 

Reset the state of a file:

git checkout -- ${file}

 

Reset the state of a branch:

git checkout ${branch}
git reset --hard origin/${branch}

 

Put aside current changes for later use (called stash):

git stash push -m "${description}"

 

List stash entries:

git stash list

 

Show a stash as a patch:

git stash show -p ${stash index}

 

Apply a stash:

git stash apply ${stash index}

 

Remove a stash:

git stash drop ${stash index}

 

Apply and remove a stash at the same time:

git stash pop ${stash index}

 

Put aside changes for unstaged files only:

git stash --keep-index

 

Put aside changes for some files only:

git stash push -m "${description}" -p ${file 1} ${file 2} ... ${file N}

 

Apply the latest stash on some files only:

git checkout stash@{0} -- ${file 1} ${file 2} ... ${file N}

 

Revert up to some specific commit and loose intermediate history (does not work on a protected branch):

git reset --hard ${commit}
git push origin HEAD --force

 

Add up-stream project and rebase on it:

# from the fork repository:
git remote add upstream ${Git project URL, either SSH or HTTP}
git fetch upstream
git rebase upstream/${branch}

 

Display all commit messages (subject and body) separated by a line containing -- written in blue:

git log --format=%B%Cblue--%Creset

 

Display the first commit of some user in current branch:

git log --author="${user}" --reverse | head -4

 

Display the list of all users:

git shortlog --summary --numbered --email

 

Display ignored files:

git status --ignored

 

Use libsecret as a credential manager for git on Linux:

sudo apt-get install make gcc libsecret-1-0 libsecret-1-dev libsecret-tools seahorse
cd /usr/share/doc/git/contrib/credential/libsecret && sudo make
git config --global credential.helper /usr/share/doc/git/contrib/credential/libsecret/git-credential-libsecret