Skip to content

Latest commit

 

History

History
346 lines (257 loc) · 5.66 KB

Git.md

File metadata and controls

346 lines (257 loc) · 5.66 KB

Git

Configuration

File ~/.gitconfig

[color]
    diff = auto
    status = auto
    branch = auto
[core]
    editor = vim
    pager = less -r

[credential]
    helper = cache --timeout=3600

Clone

git clone <url>

Branch

Create new branch

git checkout master
git pull
git checkout -b <branch>

From an existing distant branch

git checkout master
git pull
git checkout <branch>

Based on another local branch

git checkout master
git pull
git checkout -b <branch> <existing-branch>

Rebase branch onto master (if necessary)

git checkout master
git pull
git checkout <branch>
git rebase master
git status
vim <conflict_file>
git add <conflict_file>
git rebase --continue
git push -f origin <branch>

If error fatal: 'upstream' does not appear to be a git repository

git remote -v
git remote add upstream <upstream-url>

Merge branch

git checkout master
git merge <branch>
git push origin master
git branch -d <branch>
git push origin :<branch>

Into an existing branch

git checkout <existing-branch>
git merge --no-ff <branch>
git push origin <existing-branch>
git branch -d <branch>
git push origin :<branch>

From a fork

git checkout master
git merge <branch>
git push upstream master
git branch -d <branch>
git push origin :<branch>
git push origin master

Rename branch

git checkout master
git branch -m <old-name> <new-name>

Checkout a branch from another fork

git remote add <username> https://github.com/<username>/<repository>
git fetch <username>
git checkout -b <username>/<branch> <username>/<branch>

Reset

Remove all local changes (not committed)

git reset --hard

Undo the last commit (not pushed)

git reset HEAD^

Edit last commit message (not pushed)

git commit --amend -m '<new-message>'

Come back at previous state (commits not pushed)

git log
# Search commit and copy its hash
git reset --hard <hash>

Revert a commit

git log
# Search commit and copy its hash
git revert --no-edit <hash>

Update

Syncing a fork

git checkout master
git fetch upstream
git merge upstream/master
git push origin master

If error fatal: 'upstream' does not appear to be a git repository

git remote -v
git remote add upstream <upstream-url>

Update submodules

git submodule sync
git submodule update --init
git submodule foreach git submodule sync
git submodule foreach git submodule update --init

Rebase

Squash commits

Rebase

git log
git rebase -i HEAD~<n>

With <n> last commits

Commands

  • p, pick = use commit
  • r, reword = use commit, but edit the commit message
  • f, fixup = use commit, but meld into previous commit and discard this commit's log message

Push rebase

git push -f origin <branch>

Edit commit

git rebase -i <hash>^

Modify pick to edit

Make changes

git commit --all --amend --no-edit
git rebase --continue
git push -f origin <branch>

Rebase onto other branch

git fetch upstream
git rebase -i upstream/<other-branch>

Remove all pick expect the ones to commit

git push -f origin <branch>

Cherry pick

Cherry pick commit

git cherry-pick <SHA>

Stash

Move uncommitted changes to a new branch

git stash
git stash branch <branch> stash@{0}
git commit -am '<message>'
git push -u origin <branch>

Save temporarily changes

git stash

List stashes

git stash list

Load specific stash

git stash pop stash@{<stash-index>}

Search

Search in repository files

git grep -n '<value>'

Search files in repository

git ls-files | grep <filename>

Tag

List all tags

git tag -l

Checkout a tagged version

git checkout tags/<tag>

Misc

Change remote source

git remote -v
git remote set-url origin https://<domain>/<org>/<repo>.git
git remote -v
git remote update