Skip to content

Instantly share code, notes, and snippets.

@Hoang-Minh
Last active April 15, 2024 21:59
Show Gist options
  • Save Hoang-Minh/9c14580c94d0469508960507b30f5528 to your computer and use it in GitHub Desktop.
Save Hoang-Minh/9c14580c94d0469508960507b30f5528 to your computer and use it in GitHub Desktop.
Create a new branch on local and push into remote repo
git checkout -b <branch>
//Edit files, add and commit. Then push with the -u (short for --set-upstream) option:
git push -u origin <branch>
### How to create a local branch and check it to remote repository:
1. git checkout -b <nameofYourBranch>
2. git add .
3. git commit -m "your message here"
4. git push -u <yourRemoteName> <nameofYourBranch>
Note: where yourRemoteName usually is origins. Sometimes, it is features, depends on your scenario that it has different name.
### How to undo last commit
Hard undo: delete the last commit, and also undo changes from the last commit from your staging
1. git reset --hard HEAD~1
Soft undo: delete last commit, but keep file changes in staging
1. git reset --soft HEAD~1
### How to remove file from remote repository but still keep in local
1. git rm --cached mylogfile.log
OR
1. git rm --cached -r myDiretory
### How to checkout remote branch and create a copy of that branch on your local
1. git fetch: This will update your local environment so it knows about all the remote branches that exist in the remote repository.
2. git checkout -t <name of remote>/<branch name>
### How to rename local branch name
1. git branch -m <newBranchName>
### How to cherry pick ONE commit from one branch to another
1. Go to the branch that has the commit that you want to commit
2. Record the commit #
3. Switch to the branch that you want to commit to
4. git cherry-pick <commitNumberFromStepTwo>
### How to compare two branches
1. git diff branch1..branch2
### How to temp. save your changes
1. git stash
2. you can temp. save as many time as you want.
3. If you want to view a list of stashes that you save, do git stash list
4. git stash apply: apply the most recent save
5. git stash apply@{2}: apply the most second save
### How to compare if your local branch is up or behind the remote branch
1. git remote update
2. git status -uno
### How to create folder branch in remote
1. git checkout origin/<master>: it could be whatever branch that you want to branch out
2. git checkout -b feature/new_feature
3. git fetch
### Remember when merging, do it with --no-ff !!!!
### How to copy a single file from a different branch
1. git checkout <BranchThatYouWantToCopyTo>
2. Git checkout <remote_name>/<branchThatYouWantToCopyFrom> filesOrDirectory
Remote_name usually is origin
git fetch
git diff ...origin
git merge --no-ff yourBranchThatYouWantToMerge
# How to revert/reset a commit
# Link ref: https://blog.experteer.engineering/what-are-parents-on-git-merge-commits.html
1. Figure out what is the parent commit
2. Run git revet/reset <commit_number> -m 1/2
# How to add remote repo to the existing repo
git remote add origin <remote_repo_url>
# If complain about origin exists, run this:
git remote remove origin
#How to change commit message
git commit --amend and press Enter
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment