# Using checkout commandgit checkout <branch>git checkout -b <new_branch>git checkout <commit> # To get into detached head commit# Using switch commandgit switch <branch>git switch -c <new-branch> # -c or --creategit switch --detach <commit>
Get file from another branch
Use -- if file path and branch name are same or file path has -- as prefix
-- is used in Unix commands to treat everything after it as filename
# Using checkout commandgit checkout <branch> [--] <filepath># Using restore commandgit restore -s <branch> [--] <filepath> # -s or --source
Common commands
git reflog show
Get git url of remote repo
git remote get-url origingit remote show origingit config --get remote.origin.url
Search commit by commit message
git log --grep="Updated"
Clean/Undo changes
Undo local commit
# Revert commit but changes are not lost# it has --soft flag implicitlygit reset HEAD~1# Revert changes and also remove any working directory changesgit reset --hard
Clean working directory
git reset --hard # removes staged and working directory changesgit clean -f -d # remove untrackedgit clean -f -x -d # CAUTION: as above but removes ignored files like config.git clean -fxd :/ # CAUTION: as above, but cleans untracked and ignored files through the entire repo (without ":/", the operation affects only the current directory)
Diff
Diff branches
# show changes inside all filesgit diff branch1 branch2# show only filenamesgit diff branch1 branch2 --name-only# diff commitsgit diff <commit-id1> <commit-id2>
Diff Files
git diff --no-index <path1> <path2># ignore whitespace and make it more usefulgit diff --no-index --ignore-space-at-eol -b -w --ignore-blank-lines <path1> <path2>
If the diff is large spacebar can be used to go to next page faster
Cherry pick
applies changes introduced from a particular commit in the current branch
git cherry-pick <commit-id>
Check the current status
git status# include ignored changes in the outputgit status --ignored
Merge Conflicts
Check merge conflicts
# show unmerged pathsgit status# show files with conflict markergit diff --check