Saturday, January 23, 2021

Git cheat sheet


Clone git repo

git clone https://github.com/user/project.git


Add remote

git remote add [alias] [url]

git remote add origin https://github.com/user/project.git


Pull

git pull

# or:

# Update (from) remote tracking (upstream)

# Merge into current branch

git pull

# Update from origin

git pull origin

# Merge remote branch1 into current branch:

git pull origin branch1

# or:

git fetch origin

git merge origin/branch1

# normally HEAD of remote repository; defined by git-config:

# branch.branch1.remote

# branch.branch1.merge


Show local branches

git branch

git branch -l

Show all - including remote

git branch -a

git branch -a | grep remotes/*

git branch -r


Switch to another git branch

git checkout origin/branch1

# from git 2.23.0:

git switch branch1 origin/branch1


Switch to another git branch (reset local branch based on source branch)

git checkout -B branch1 origin/branch1


Create/Clone branch from another branch and switch into it

git fetch origin

git merge origin/master

git checkout -b branch1 origin/master^0 --

More at StackOverflow


Push new local branch to remote

git push origin refs/heads/branch1:branch1 --set-upstream

git push origin refs/heads/branch1:branch1 -u # maybe outdated, check it


Merge branch to master

git checkout origin/master

## git checkout master

git merge origin/branch1

resolve possible conflicts:

git add -A -f -- build/installer.sh

commit resolved changes:

git commit -F C:\git\project1\.git\MERGE_MSG --

push to remote:

git push origin refs/heads/master:master

# Merge remote branch1 into current branch:
git pull origin branch1

Rebase

Never rebase publicly used branches

git rebase

git rebase --interactive

git rebase --onto

See more about Rebase


Delete branch

delete local

git branch -d branch1

delete remote

git push origin --delete origin/branch1


Prune

Delete all local branches without their remote

git fetch -p


Revert file

git checkout HEAD -- file1.txt

revert (remove) uncommited file:

git rm --cached -f -- file1.txt

undo/revert local changes:

git reset --hard

git reset HEAD file1.txt # unstage to current commit

git checkout -- file1.txt

git stash

See more at Revert all local changes


Ref log

git reflog show

See more Many undo possibilities


Stash

stash create

git stash save "my stash name"

stash apply

git stash apply stash@{0}

git stash pop

stash list

git stash list


Related commands

git fetch upstream

git push -u origin branch1

See Push default

git remote add origin ssh://...

git config branch.master.remote origin

git config branch.master.merge refs/heads/master

git push origin master


Show current branch name

git symbolic-ref --short HEAD


Show Git remote URL

git config --get remote.origin.url


Check origin fetch and push URL

git remote -v

origin  https://github.com/username/project.git (fetch)

origin  https://github.com/username/project.git (push)


Set change remote origin URL to HTTPS or SSH

# set remote origin url to https:

git remote set-url origin https://github.com/username/project.git

# set remote origin url to ssh:

git remote set-url origin git@github.com/username/project.git


Different Git configs

git config --list

git config --global --list

git config --system --list


Push default

git config --global push.default matching

git config --global push.default simple

See default git push

See push default


Global proxy settings

git config --global http.proxy myproxy.com:8080


Global SSL verify

git config --global http.sslVerify "false"

git config --global --unset http.sslVerify


Project specific SSL verify

cd C:\git\my-project

git config http.sslVerify "false"

:: git config --unset http.sslVerify


Set branch upstream

# If you forgot to specify branch upstream during checkout:

git checkout -b branch1 origin/branch1

# you have to set upstream branch later:

git branch --set-upstream-to=origin/branch1

# shortcut for set upstream branch:

git branch -u origin/branch1

# set upstream for other branch:

git branch -u origin/branch1 branch1


Sources

git branch vs git checkout -b

track remote branch

IntelliJ Idea branches


No comments: