![]() ![]() You can demonstrate how to view a list of available branches by executing the git branch command and switch to a specified branch. The git branch and git checkout commands can be integrated. We can perform many operations by git checkout command like the switch to a specific branch, create a new branch, checkout a remote branch, and more. It checks the branches and updates the files in the working directory to match the version already available in that branch, and it forwards the updates to Git to save all new commit in that branch. Sometimes this command can be dangerous because there is no undo option available on this command. The git checkout command operates upon three different entities which are files, commits, and branches. Be careful with your staged files and commits when switching between branches. The git checkout command is used to switch between branches in a repository. However, itâs important to note the difference between how commits and branches behave. Iâll be using git switch to change branches in future.In Git, the term checkout is used for the act of switching between different versions of a target entity. git branch You can use the checkout command to switch to any active branches: git checkout Checkout a Commit (Detached HEAD) Just as you can switch to different branches with the checkout command, you can also switch to commits.And thatâs a good thing in my eyes: one command should do one thing (which is why I also use git restore). You canât do either of those things with git switch. You can also discard untracked changes to a file for example, if Iâve made some changes to my homepage that I donât want to keep, I might restore the file to its pre-edited state with: git checkout index.html I mentioned that, as well as switching branches, git checkout allows you to jump back to a previous commit using a commit hash: git checkout abc1234 And -c has a longhand equivalent that confirms this meaning: git switch -create my-new-feature Single purpose Happily, the switch equivalent of the -b flag makes much more sense: git switch -c my-new-featureĬ for âcreateâ: weâre switching to and creating a branch. So what does b stand for? Maybe âbuildâ? Annoyingly, there isnât a longhand for it, like -f has -force, so thereâs no way of knowing for sure. Both commands were proposed in 2019, and as of now, they are experimental. ![]() They provide more explicit semantics, in line with the UNIX philosophy. b for âbranchâ I guess, but that doesnât quite make sense as weâre already doing something with a branch when we run the checkout command without the -b. The features of git checkout are clearly separated: git switch is used to switch branches, while git restore is used to restore working tree files. Using the -b flag means âcreate a new branch that doesnât already exist, then checkout on itâ. I often like to create a new branch and âcheckout on itâ in one move, so the following command gets a lot of use: git checkout -b my-new-feature The semantics of âchecking outâ could also be interpreted as leaving, not moving to itâs a bit of an odd ball.Īnd if Iâm teaching someone how to use Git, checkout has always required some explanation where switch wonât. But when when we plan to do work on a branch, weâre doing a lot more than just checking it out. âChecking outâ another branch might make sense when weâre going back in time to have a quick look at (âcheck outâ), how things looked on an old commit before coming back to the present-day commit. The main reason I like git switch is that it says what it means: switch to another branch. The checkout command tells Git which branch or commit you want your changes applied. We can now replace that with switch: git switch my-featureÄ«ut why is git switch a good thing? More plain English A checkout command would look like this: git checkout my-feature Git switch was introduced in Git version 2.23 to separate jumping from branch to branch into its own command. ![]() Running the command git checkout 21f25fe0 will bring the state of the changes made in commit with the SHA hash 21f25fe0. The other thing that this command can do is switch to a certain commit in a history log. Git switch vs checkout code#Git checkout has always been a funny one for me it switches branches, allows you to view code at older commits, discards changes to a file, and probably some other things I donât know about. Running a command git checkout main will switch from the current to the branch main. Since upgrading to macOS Big Sur and its version of Xcode Command Line Tools, Iâve noticed that git switch now works. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |