Hello. I'm 🐈 Yokoyama from development. Do you use git? I think it is rare not to use git in development now. In this article, I will explain
When I first started working with git, it was very difficult and I used to crash a lot 😭.
Even now, I can't say that I understand it, but I almost never get stuck with git operations during development anymore. With this paper as a guide, I urge you to get familiar with the git commands!
Click here for table of contents
- 1. what is git?
- 2. features and mechanism of git
- 3. git commands recommended for developers
- 4. summary
To begin with, what is git?
git is a distributed version control system that keeps track of changes to program source code. git was created by Linus Torvalds, the inventor of Linux, to manage the source code of the Linux kernel.
With git, you can store (commit) your code changes in your local environment as well, so you don't need to be constantly connected to a remote server. This allows you to work without being connected to a network.
What is git and how does it work?
Features of ◆git
I could easily revert to an older version!
I can centrally manage my old and new files!
You can share your edited history with multiple people!
I can merge multiple people's revisions into one!
Recommended git commands for developers
Here are some of the commands I use during development.
This function is used to check the current status. Specifically, you can add or modify a file, or register it in the index with add, to see what the current status is.
Branching is a feature of version control that allows you to branch off of your master history. git branch lets you see what branch you're on and what other branches you have, and you can use it before you checkout.
This is used when you want to check your commit history. You can use this command to see your own history and the history of others, and it's very useful when you're developing with multiple people. If you're working with multiple people, this is a very useful command. log shows you the commits you've made so far in the form of a list of commit messages.
Use this to switch branches or revert changes to a file back to the way it was before you changed it.
It's too versatile, so they added git switch and git restore, but I still use checkout out of habit.
|git checkout .||Restore all files to the state of the latest commit.|
||Restore the specified file to the state of the latest commit.
More than one is allowed. Directory is also acceptable.
||Switch to the specified branch.
If you don't have a specified branch locally, but have one remotely, you can create a branch and switch between them.
|git checkout -b ||Cut a branch from the current branch and switch straight to it.
||Switches to the state of the specified commit.
It is used for bug verification and so on.
Used to add changes to the workspace to staging.
|git add .||All files with changes in the workspace are added to staging.|
||Add the specified file to staging.
More than one is allowed. Directory is also acceptable.
|git add -p||You can choose to subdivide your changes and add them to staging.
Use this if you've made some changes and want to split the commit into multiple commits.
Use this to commit files that have been added to staging.
|git commit -m "commit message"||You can commit by specifying a commit message.
If you don't have this, you may be confused at first when vim opens.
Use this to undo commits or to return things that have been added to staging to the workspace.
|git reset||Returns the files added to the staging to the workspace.|
|git reset HEAD^||Undo the previous commit.|
Reflects the status of the remote repository to the local repository.
|git pull||Basically this is the only thing I use.|
|git pull -prune||Updates the information in the local repository for the remote repository branch.
It's too much work to specify this every time.
git config --global fetch.prune true
you can just do a git pull.
Adds your local commits to the remote repository.
|git push||If the remote repository doesn't have a branch with the same name, you can use
fatal: The current branch master has no upstream branch. To push the current branch and set the remote as upstream, use git push --set-upstream origin master
error. It's too much work to add this option every time.
git config --global push.default current
This can be avoided by specifying
|git push -f||If your local branch and the remote branch have different commit histories, git push alone is rejected.
This often happens when you push a local branch once, rebase the master branch to a development branch, and then re-push.
In this case, you can use -f to force the remote branch to overwrite the local branch's content.
Please use with caution in branches where multiple people are developing.
I'm using it instead of merge. I know there are many opinions, but I use rebase because I don't want to keep a commit log of the merge.
||Joins the current branch to the HEAD of the specified branch.|
|git rebase -abort||Use this to undo a rebase and revert to before the rebase.
If you have a conflict during rebase, you need to resolve the conflict, but it is difficult to resolve the conflict.
|git rebase -continue||After resolving conflicts, continue rebase.|
This is used to save the changes in the workspace without commit.
|git stash||Saves the contents of the workspace without commit.|
|git stash -u||The untracked file is also included in the evacuation.|
|git stash pop||Return the contents of the last stash to the workspace.|
Displays the HEAD movement history.
If you undo too many commits with git reset -hard or something similar, you can use
You can use reflog to see where you want to go back, and then git reset -hard
In this article, I've introduced some git commands that I personally use frequently. If you're not familiar with git commands, it's hard to understand, but it's very useful. If you've never used it before, I recommend you learn it once!
For programmers, reverting back to the original version of the code is an everyday occurrence when something goes wrong after editing a lot of code. However, it takes a lot of time and needless to say, human error will increase if you save the old version with the date and time and start a new work again every time you edit the code. Git is a tool to do such work efficiently and without waste.