A work-in-progress list of my favorite Git commands
I was planning to write an article as a reference for the Git commands I use the most. In the following list you see these commands and an explanation for each one of them:
- Initialize an empty Git repository in an existing folder:
git init
- Discards all local changes to all files:
git reset --hard
- Clones an online repository:
git clone URLOfTheRepository
- Merge master into a feature branch. A practical way to refresh the feature branch with changes that were made in other feature branches and merged to master:
git rebase master
- Create a new feature branch:
git checkout -b BranchName
- A list of all local branches inside a repository:
git branch
- Delete a branch locally:
git branch -D BranchName
- Connect a remote branch to an existing local branch:
git branch --set-upstream-to=origin/TheNameOfTheRemoteBranch TheNameOfTheLocalBranch
- Make a new remote branch, based on the local branch
git push --set-upstream origin TheNameOfTheLocalBranch
- Revert the last commit you made:
git revert head
- Push the local branch to remote:
git push origin TheNameOfTheLocalBranch
- Delete a remote branch:
git push origin --delete TheNameOfTheLocalBranch
- Update the local branches from the remote ones:
git pull
- Get & update all (not only the mapped ones) the remote branches locally:
git fetch
- List the commit history of a specific file in your repository:
git log absolutePathToFile
- Revert changes of a single file in your repository. You can find the commit hash in the git history of your file:
git checkout <commit-hash> absolutePathToFile