When I started to use Git, I believed that it was all a matter of clone, add, commit, push and pull.

Know really well git

Soon enough it occured to me that those commands are just the tip of the iceberg. Therefore I decided to collect some of the commands that I found useful and/or difficult to remember.

Since it would be a shame to hide on my Desktop this great resource I decided to share it here.

More than half of these stuff can be done with an IDE, but I like to know how to use them also at the command line. Even if you go out for dinner every night it does not mean you should know how to cook pasta! 🧑‍🍳😙🤌

N.B. Do not expect to find anything about rebase!

Start tracking remote branch

You want to work on a branch that your team mate has just pushed

git checkout <remote_branch_name>

Checkout file from another branch

Have you ever needed a file from another branch, for istance a configuration file? For sure you can checkout the other branch, copy the file content, go back to the original branch and paste the content. I got tired just at describing it, luckily there is an easier way

git checkout <branch_name> <file_name>

it works also indicating a commit

git checkout <sha_commit> <file_name>

Stop tracking deleted remote branches

Your git history is a mess of branches that were deleted from remote? You can clean it

git fetch --prune

Delete remote branch

Do you wish to make some repo cleaning deleting unused branches?

git push --delete <remote_name> <branch_name>

Delete local branch

You worked on a small change locally on a new branch. You decide to merge it directly to its parent branch. After it you can delete the small-change-branch with

git branch --delete <branch_name>

If you did not merge the branch but you want to delete it anyway

git branch --delete --force <branch_name>

Find which branch contains a commit

Do you want to have the list of branches that contain a given commit?

git branch -a --contains <sha_commit>

Find commit that are on a branch and not on another

What about having the list of commits that belong to a branch but not to another?

git log --oneline <first_branch> --not <second_branch> --no-merges

See names of files involved in commit

If you want to know the names of the files affected by commits

git log --name-only

Edit hunks

Making atomic commits is a good practice, but it can (often?) happen to work on two different little changes at the same time. How can you make one commit for each change?

git add -p <name-file>

For a detailed explanation on how to work with this command take a look at this post.

Discard files tracked by git lfs that look modified (but are not)

Using git lfs for large files can cause tracking issues. One of the most common for me it’s when git lfs tells that a file has changed even it is untouched.

For solving this issue I found on Stack Overflow this solution. Take it as it is. It’s a good idea to make it a Git alias out of it. Be sure to have just this files as changes then run

git rm --cached -r .
git reset --hard
git rm .gitattributes
git reset .
git checkout .

Solve git lfs 413 error

When pushing a very large file with git lfs to remote it may happen to get a 413 error. Thanks to an answer on Visual Studio Forum, I found a solution that works (on my computer!)

git config http.version HTTP/1.1