Git useful command list

Basic Commands

To init a repository within a folder:

git init

To set your global username/email configuration:

git config --global user.name 'contact@jtrainer.education'

git config --global user.email 'contact@jtrainer.education'

To clone a remote repository:

git clone https://mygitrepository.com/user/repository.git

To check the status of the working repository (current branch, changed files…)

git status

See recent commit history

git log

Revert to the moment before one commit

# reset the index to the desired tree
git reset 56b07fcad

# move the branch pointer back to the previous HEAD
git reset --soft HEAD@{1}

git commit -m "Revert to 56b07fcad"

# Update working copy to reflect the new commit
git reset --hard

To checkout a remote branch

git checkout -b [branch1] origin/[branch1]

Advanced Commands

This command make a list of the remote branches (origin) and print an ordered list of the last commits

for branch in `git branch -r | grep -v HEAD`;do echo -e `git show --format="%ci %cr" $branch | head -n 1` \\t$branch; done | sort -r

The following command make a list of the local branches (refs) and print them in a ordered list based on last commit date:

 git for-each-ref --sort='-committerdate:iso8601' --format=' %(committerdate:iso8601)%09%(refname)' refs/heads

To show the list of files that differentiate two branches:

git diff  --name-only branch1 branch2

To see differences between two commits

git diff COMMIT1_SHA COMMIT2_SHA

To rename branches remotely (without renaming any local branches) you can do in this way:

 git push origin origin/branch1_OLD:refs/heads/branch1_OLD :branch1_NEW

Leave a Reply

Your email address will not be published. Required fields are marked *