Top Git commands for beginners II
This article is to continue discussing the super helpful Git commands. It is still for beginners but will be relatively more advanced. Check out my previous article for basics.
- To check the staged files,
git status
2. To check the file changes not yet added to the Git staging field,
git diff
3. To stop tracking a file
git rm --cache <file>
4. To apply a commit from another branch directly,
git cherry-pick <commit>
5. To create a new branch based on a old branch locally,
git checkout -b <new branch> <old branch>
6. To push the current branch and set the remote as upstream,
git push - set-upstream origin <local-branch>
7. To set the remote tracking information for a local branch,
git branch --set-upstream-to=origin/<remote-branch> <local-branch>
8. To delete a local branch and repull from remote,
# 1. Delete local branch
git branch -d <local-branch>
# 2. Fetch the latest remote branch
git fetch origin <remote-branch>
# 3. Rebuild the local branch based on the remote one
git checkout <remote-branch>
9. To delete a remote branch and repush the local branch,
# 1. delete the remote branch
git push origin --delete <remote-branch>
# 2. push the local branch
git push -u origin <local-branch>
10. To merge new changes to your last commit, but keep everything else, including the commit message, the same.
git add <file> # add changed files to the staging area
git commit --amend --no-edit # commit the changes, but keep the commit message the same
11. Same as 10, but to change the commit message as well,
git commit --amend # commit the changes and update the commit message
12. To restore the file to the last committed version, removing any new changes
git restore
13. To remove files from the staging area, so that they won’t be committed until you git add them back, but the changes to the file are kept,
git restore --staged
14. To revoke the last commit, but keep all modified files in the staging area
git reset --soft HEAD~1
# Or
git reset --soft HEAD^
15. To list commits within the repository.
git log