How to move a few last commits into new branch in Git?

Create new branch from current state of original branch

$ git checkout -b new_branch_name

Switch to original branch and reset a few last commits

$ git checkout original_branch_name
$ git reset --hard HEAD~number_of_commits
$ git push --force

Read more

How to set up git repository to use external sub-module repository


Go to sub-folder of your git-folder where you want to put new sub-module and execute

$ git submodule add git_repository_url directory_name

Make sure that directory name does not contain dashes.


Updating sub-modules from command line

$ git submodule foreach git pull

or by TortoiseGit

Updating sub-modules by TortoiseGit
Updating sub-modules by TortoiseGit.

Often used GIT commands

Pushing new branch

$ git push -u origin new_branch

Fetch all remote branches

$ git fetch origin

View branches list

$ git branch -v -a

Fetch remote branch

$ git checkout -b local_branch origin/remote_branch

Create new branch

$ git checkout -b branch_name

Delete local branch

$ git branch -d branch_name

Delete remote tracking branch (local)

$ git branch -rd origin/branch_name

Delete remote branch

$ git push origin --delete branch_name

Revert all

$ git checkout .

Revert single file

$ git checkout file_name

Set up user attributes

$ git config --global "John Doe"
$ git config --global

Get origin name

$ git remote show origin

Merge branches locally

  • Commit changes to original branch
  • Switch to new branch and execute
$ git merge original_branch

Select local version

$ git checkout HEAD file_name

Select branch version

$ git checkout branch_name file_name

Copy commit from another branch

$ git cherry-pick commit_sha

Reset changes to commit

$ git reset --hard commit_sha

Create patch from last commit

$ git format-patch -1 HEAD

Create patch from the difference with original branch

$ git format-patch origin

Apply patch

$ git am --ignore-whitespace patch_name

Create patch from uncommitted changes

$ git diff > file_name

Remove file from repository and save local copy

$ git rm --cached file_name

Create branch from commit

$ git checkout -b branch_name commit_hash

How to revert merge request in Git?

Create a git patch from the changes in the current working directory

How do I create a new Git branch from an old commit?

How to selectively merge or pick changes from another branch in Git?

How to add file to GIT and ignore subsequent changes

It can be useful if you don’t want to store some sensitive information like logins in repository.

Step 1

Add clean version of the file to GIT as usual. Commit it.

Step 2

Change file.

Step 3

Start commit by TortoiseGit, right click the file and select “Skip worktree”.

Skipping local changes of file under GIT
Skipping local changes of file under GIT.

Step 4

Repeat this for every branch which contains the file (over fresh checkout of branch).