Frequently used git commands
1. Initialize git
git init
2. See what you have changed since the last commit
git diff
3. Stage files for committing
git add .
4. Commit changes
git commit -m "Made some changes"
5. Create a branch and switch to it
git checkout -b issue87
6. List branches
git branch
7. Switch to a different branch
git checkout issue13
8. Change remote
git remote remove origin
git remote add origin https:#github.com/dancancro/ng2-redux-form
9. Save changes without committing them so you can go to a different branch and come back later
git stash
10. Restore stashed changes
git stash apply
11. Push the current branch to the same name on the remote
git push origin HEAD
12. Compare two branches
git diff branch1..branch2
13. Delete a local branch
git branch -d branchname
14. Delete a remote branch
git push origin --delete branchname
15. Merge a branch into another branch
git checkout dest_branch
git merge source_branch
16.Discard all unstaged changes in the current branch
git checkout -- .
17. Remove untracked files from the working tree
git clean -xfd
18. Create a new branch with changes
git branch mynewbranch git git reset --soft HEAD~3 # only if you want to undo last 3 commits git checkout mynewbranch
19. Combine commits from a branch and merge it into another branch
git checkout master git merge --squash bugfix git commit
20. Undo last commit without undoing the changes.
Do this so you can see the differences between your files and a previous commit. Repeat the command until you reach the point against which you want to compare your current files
git reset --soft HEAD~
21. Rename a branch
-
Rename your local branch. If you are on the branch you want to rename:
git branch -m new-name
If you are on a different branch:
git branch -m old-name new-name
-
Delete the old-name remote branch and push the new-name local branch.
git push origin :old-name new-name
Rename your local branch. If you are on the branch you want to rename:
Delete the old-name remote branch and push the new-name local branch.