Git

Git

Git

Tips & Solutions

Remember http authorization password

git config --global credential.helper store

Set file permissions on Windows

git update-index --chmod=+x file.sh

Move resent commits to another branch

Moving to an existing branch

git checkout existingbranch
git merge master         # Bring the commits here
git checkout master
git reset --keep HEAD~3  # Move master back by 3 commits.
git checkout existingbranch

Moving to a new branch

git branch newbranch      # Create a new branch, containing all current commits
git reset --keep HEAD~3   # Move master back by 3 commits (Make sure you know how many commits you need to go back)
git checkout newbranch    # Go to the new branch that still has the desired commits
# Warning: after this it's not safe to do a rebase in newbranch without extra care.

Config

git config --global http.proxy http://127.0.0.1:1080

# 提交时, \r\n => \n。
git config --global core.autocrlf input

# store http credentials:
git config --global credential.helper store
# for Windows:
git config --global credential.helper wincred

Proxy

git config --global http.proxy 'socks5://127.0.0.1:1080'
git config --global https.proxy 'socks5://127.0.0.1:1080'

# cancel proxy config
git config --global --unset http.proxy
git config --global --unset https.proxy

禁用 git push 代理:

git config --global --add remote.origin.proxy ""

强制清除本地所有未提交更改

git reset --hard HEAD

Show branches "Tree"

git config --global alias.lgb "log --graph --pretty=format:'%Cred%h%Creset -%C(yellow)%d%Creset %s %Cgreen(%cr) %C(bold blue)<%an>%Creset%n' --abbrev-commit --date=relative --branches"

git lgb

"git pull" vs "git pull --rebase"

Suppose you have two commits in local branch:

      D---E master
     /
A---B---C---F origin/master

After "git pull", will be:

      D--------E  
     /          \
A---B---C---F----G   master, origin/master

After "git pull --rebase", there will be no merge point G. Note that D and E become different commits:

A---B---C---F---D'---E'   master, origin/master

Remote rejected (shallow update not allowed) after changing Git remote URL

Problem:

After changing remote by git remote set-url origin bitbucket_address, But now when I try to push my project I get the error

! [remote rejected] master -> master (shallow update not allowed)

Why: As it seems you have used git clone --depth to clone your local version. This results in a shallow clone. One limitation of such a clone is that you can't push from it into a new repository.

git remote add old <path-to-old-remote>
git fetch --unshallow old

Shallow repository

# 只获取最新版本
git clone git_url --depth 1

# 更新到最新版
# git fetch origin remoteBranch:localBranch --depth 1
git fetch origin master:master --depth 1

Check the first (oldest) commit in a repository

git checkout $(git log --reverse --pretty=format:"%h" | head -n 1)

撤销最近提交

# Careful: git reset --hard WILL DELETE YOUR WORKING DIRECTORY CHANGES. Be sure to stash any local changes you want to keep before running this command.

# The HEAD~1 means the commit before head.
git reset --hard HEAD~1
# check the output of git log, find the commit id of the commit you want to back up to, and then do this
git reset --hard <sha1-commit-id>

git push origin HEAD --force

HEAD 用来代指本地“当前分支”


Last update: 2020-11-20 02:19:28 UTC