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 <command> <options> <parameters>’.

Task Commands
Reset current branch to specific commit (details)
reset --hard <commit-hash>
push -f origin <your_branch>
Discard changes in current branch
1) to remote (everything not pushed)
reset --hard origin/<your_branch>

2) since last commit
reset HEAD --hard
clean -fd
Create a new branch
The first command creates a local branch. The second command creates a remote one.
checkout -b <your_branch>
push -u origin <your_branch>
Delete a branch
The first command deletes a remote branch, The second command deletes a local one.
push --delete origin <your_branch>
branch -d <your_branch>
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
checkout <your_branch>
Merge another branch to the current one
merge <another_branch>
Compare local with remote
diff your_branch origin/<your_branch>
Delete local and remote branches
branch -d <your_branch>
push origin :<your_branch>
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
rm --cached <file>

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.

git clone --depth 1 -- <branch>

You have to solve this with the following commands: 

git remote set-branches origin '*'
git fetch -v
git checkout <other-branch>

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

Tags:

Leave a Reply

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

Related Post

%d bloggers like this: