# ======================= [ MERGE TWO GIT BRANCH ] ======================= I have a branch named BranchA from master. I have some changes in BranchA (I am not going to merge changes from BranchA to master). Now I have created another branch from master named BranchB. How can I copy the changes from BranchA to BranchB? Answer: git checkout BranchB git merge BranchA or if you didn't create BranchB yet you can do: git checkout BranchA git checkout -b BranchB This is all if you intend to not merge your changes back to master. Generally it is a good practice to merge all your changes back to master, and create new branches off of that. Also, after the merge command, you will have some conflicts, which you will have to edit manually and fix. Make sure you are in the branch where you want to copy all the changes to. git merge will take the branch you specify and merge it with the branch you are currently in. # ======================= [ RESET A BRANCH ] ======================= -> get back to a commit by their commit id or the order. >>> Step 1: 1st check on which commit you want to move to, using `git log`. Step 2: then move to that using `git reset`, say you want to move the branch head backward by two steps. so you do --> `git reset --hard HEAD~2` >>> Using the commit id/commit hash might also work. But I have not tried that. Command: https://stackoverflow.com/a/3639154 `$ git reset --hard <commit-before-merge>` OR, git reset --hard <commit-hash> git push -f origin master # ======================= [ GIT STASH ] ======================= when switching from one branch to another, if this error comes `error: The following untracked working tree files would be overwritten by checkout:`. Then you either need to stash or clean. Better is stash. -> `git stash --include-untracked` ❌ Never do stash, it deletes all the changes that have been added after the last commit. -> Use `git stash pop` to undeo the recent stash. // https://stackoverflow.com/questions/10827160/undo-a-git-stash # ======================= [DIFF FILES] ======================== $ git diff HEAD:<version 1 of file1> <version 2 of file1> git diff HEAD:Carla/carla_scripts/initialpose_recorder.py Carla/carla_scripts/initialpose_recorder.py # ========== [UNCOMMIT A FILE FROM CURRENT/LAST COMMIT ] ========== [https://stackoverflow.com/questions/12481639/remove-file-from-latest-commit] - git reset --soft HEAD~1 - git restore --staged path/to/unwanted_file - git commit -c ORIG_HEAD # ========== [GIT COMMIT W/O MESSAGE] ========= - git commit -am. # ============ [GIT BRANCHES] ===================== - `git branch -a` => shows all branches - `git branch -r` => shows only remote branches - `git branch` => shows only local branches - `git checkout <branch name>` => add a remote branch to your local - if you are upable to add branch using git checkout you might need to do `git fetch -all`. then do `git checkout <branch name>` again. # =============== [REGULAR GIT COMMANDS WHEN WORKING @ ALIVE] ======================= - git status - git add commonroad_dev/ [NEVER DO `git add .`, if needed add the whole folder] - git commit -m "message" - git push -u origin <branch-name> # ================== [CREATE A GIT BRANCH] =========================== - git branch my_branch [This create a branch named `my_branch`] - git checkout my_branch [after creatng you switch to the `my_branch`] #=================== [GIT REBASE] ============================== - https://github.com/soumya997/alive-test/blob/main/git_rebase.md
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter