😊 git command tips

1️⃣ rename git branch name local and remote

1,rename the local branch to the new name

git branch -m old_name new_name

2,delete the old branch on remote - where is eg. origin

git push <remote> --delete old_name

3,push the new branch to remote

git push <remote> new_name

2️⃣ checkout a remote git branch


git checkout -b test origin/test

3️⃣ push new branch and set upstream

git push --set-upstream haier feature/operaiton-reconstruct

4️⃣ remove .gitignore


2.2 Git 基础 - 记录每次更新到仓库

git rm -r --cached out/*`
git add -A
git commit -m…
git push …

5️⃣ git merge —no-ff


git checkout master
git merge --no-ff develop

默认情况下,Git执行”快进式合并”(fast-farward merge),会直接将Master分支指向Develop分支

6️⃣ Github Keeping a fork update to date



1,List the current configured remote repository for your fork.

➜  mybatis-3 git:(master) git remote -v
origin  [email protected]:LaudOak/mybatis-3.git (fetch)
origin  [email protected]:LaudOak/mybatis-3.git (push)

2,Specify a new remote upstream repository that will be synced with the fork.

➜  mybatis-3 git:(master) git remote add upstream [email protected]:mybatis/mybatis-3.git

3,Verify the new upstream repository you’ve specified for your fork.

➜  mybatis-3 git:(master) git remote -v
origin  [email protected]:LaudOak/mybatis-3.git (fetch)
origin  [email protected]:LaudOak/mybatis-3.git (push)
upstream    [email protected]:mybatis/mybatis-3.git (fetch)
upstream    [email protected]:mybatis/mybatis-3.git (push)

4,Fetch the branches and their respective commits from the upstream repository. Commits to master will be stored in a local branch, upstream/master.

➜  mybatis-3 git:(master) git fetch upstream
From github.com:mybatis/mybatis-3
 * [new branch]          3.2.x      -> upstream/3.2.x
 * [new branch]          3.3.x      -> upstream/3.3.x
 * [new branch]          gh-pages   -> upstream/gh-pages
 * [new branch]          master     -> upstream/master

5,Check out your fork’s local master branch.

$ git checkout master

6,Merge the changes from upstream/master into your local master branch. This brings your fork’s master branch into sync with the upstream repository, without losing your local changes.

➜  mybatis-3 git:(master) git merge upstream/master
Already up-to-date.

7️⃣ undo merge

Undo a Git merge that hasn’t been pushed yet

If during the merge you get a conflict, the best way to undo the merge is:

git merge --abort

8️⃣ find deleted file in commit history

Git: How to find a deleted file in the project commit history?

 git log --diff-filter=D --summary | grep delete


git log --all -- file


9️⃣ replace git branch code with another branch’s code

How to completely replace git branch code with another branch’s code

# Rename master to old-master, staging to master
$ (staging) git checkout master
$ (master) git branch -m old-master
$ (old-master) git checkout staging
$ (staging) git branch -m master

# Force push staging (now master) into remote master
$ (master) git push origin master -f

# Change branch names back to original
$ (master) git branch -m staging
$ (staging) git checkout old-master
$ (old-master) git branch -m master

# Sync local master with remote master
$ (master) git fetch --all
$ (master) git reset --hard origin/master

Some explanation and notes:

  • git branch -m renames the current branch to ‘new-name’
  • git fetch –all downloads all objects and refs from the entire repository without merging
  • git reset –hard origin/master forces your local master’s latest commit to be aligned with remote’s
  • git branch names are just pointers, so renaming staging to master and doing a git push origin master will update remote’s master
  • force pushing a branch to a remote will force the remote branch to take on the branch’s code and git commit history

🔟 How do I delete a Git branch locally and remotely?

Executive Summary

$ git push --delete <remote_name> <branch_name>
$ git branch -d <branch_name>

Note that in most cases the remote name is origin.

Delete Local Branch

To delete the local branch use one of the following:

$ git branch -d branch_name
$ git branch -D branch_name

Note: The -d option is an alias for –delete, which only deletes the branch if it has already been fully merged in its upstream branch. You could also use -D, which is an alias for –delete –force, which deletes the branch “irrespective of its merged status.” [Source: man git-branch]