Most used Git commands

The most used commands in console which are used by me in projects. The reference is often overwhelmed with information which hinders me to use it in practice. This text is the place to keep working examples for quick reference instead of looking them up from somewhere in the internet.

All examples presume a local and a remote repositories which might be maintained simultaneously in most of cases. Also you should take a note that all commands are prefixed with ‘git' so the whole command should look like the following ‘git

Task Commands
Reset current branch to specific commit (details)

Discard changes in current branch

Create a new branch
The first command creates a local branch. The second command creates a remote one.

Delete a branch
The first command deletes a remote branch, The second command deletes a local one.

List local and remote branches & update the list of remote branches
It shows branches in local and remote repositories.
branch -a
remote update origin --prune
Switch to another local branch

Merge another branch to the current one

Compare local with remote

Delete local and remote branches

Get all merge commits from branches
log BRANCH -NUMBER --merges --oneline --since='YYYY-MM-DD' --until='YYYY-MM-DD'
Revert one file to a specific commit
checkout c5f567 -- file1/to/restore file2/to/restore
Untrack a file git rm –cached

Dealing with Issues

Shallow clone

After you make a shallow clone with the following command you may not be able to checkout to other remote branch.

You have to solve this with the following commands: 

Taken from here.

Some issues with in

It may say that NuGet configuration is invalid or some path is invalid. In such a case delete this file %AppData%/Nuget/Nuget.config. Taken from here.

Git rebase –onto

Transferring commits from one to another branches without changing already pushed commits. Schema is as follows:

: create from the target branch - merge_to_3.6

git rebase --onto merge_to_3.6 b3.5 b3.5-c
git checkout merge_to_3.6
git merge b3.5-c
git checkout b3.5-c
git reset --hard origin/b3.5-c

After those operations you will be able to make a pull request to b3.6 branch with changes only from b3.5-c branch. But you're not protected from conflicts you may have during the rebasing.

Conflict resolution

It's possible to split the rebasing and conflict resolution into two different steps. The following command solves all conflicts using the version from the target branch.

git rebase -Xours --onto merge_to_3.6 b3.5 b3.5-c

After you restore the branch b3.5-c you can merge it to the target. During this merge you will need to solve conflicts.

See Also


Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Related Post

%d bloggers like this: