编程知识 cdmana.com

Don't despise git any more -- how do I use git in my work

up to now , I brought three interns , Every one of them has a unique skill , But every one of them is git I've had a fall in my car 、 I have suffered losses :

  • Some deal with git The conflict has been dealt with to the end
  • yes , we have git commit message A mess , In the end, I couldn't tell which one commit It's the code you want
  • Some even lose the code


4565523a17d1d18b6f79fb8e4335eec8.jpeg


without doubt ,Git It's a very important tool , But also by most students 、 A skill that new employees despise .

Today, I would like to share with you how I work with Safest Most robust The most simple The way to use git


Set up git alias

Use git alias Can greatly improve efficiency , I often use

git config --global alias.co checkout
git config --global alias.br branch
git config --global alias.ci commit
git config --global alias.st status
git config --global alias.unstage 'reset HEAD --'
git config --global alias.last 'log -1 HEAD'
git config --global alias.logl 'log --oneline'

Finish the instructions above , see ~/.gitconfig Whether the following contents are included

d534b6b2db13341bf5f74995346fa8d1.png

The effect is

I type... On the command line git st Is equivalent to git status , Input git ci -m "xxx" Equate to git commit -m "xxx"


Most commonly used git Instructions TOP 3

The first is  git st ( namely git status)

I usually   After writing the code 、 Do anything git Before operation 、 Do complex git On the way to operation ( such as rebase、merge In a conflict ) and Do anything git After the operation , Will use git st To check the current status —— Which files are still in the workspace ( Not yet git add)、 Which files are still in the staging area ( Not yet git commit) perhaps rebase、merge And so on .

to the full 、 Keep abreast of the current git State can avoid some misoperation


The second is  git logl ( namely git log --oneline )

This command prints out the latest commit messages, Every one of them message Just one line , Make the interface Cleaner 、 beautiful .

I usually go to   Before and after switching branches , Pull the front and back of the line branch   Use git logl, It can help me to base on commit message Confirm my current branch and whether it is up to date ( The premise is that every time commit Be sure to fill in carefully commit message)


The third is  git ci ( namely git commit)

git The reason why it is widely used , Because it has excellent design and performance . And one of ,commit yes git The core of design .

In daily development work , I usually go to   After writing part of the code , Prepare for a break or confirm a function /bug After processing   Use  git add  and  git ci  To save the code to the local repository .

remember : Just be right commit 了 , Code is almost never lost .( Here are more about git commit The discussion of the )




Work scene one —— new task ( Develop new features / repair bug)

  • git st  View the status of the current project , If there are unsaved changes , Just git add .  and  git ci -m "xyz"  preserved
  • Switch to the development main branch , Let's call it the main project develop Just use  git co develop
  • Make sure the main branch is up to date  git pull origin develop
  • Create a new branch  git co -b feat-1 ( Create a feat-1 New branch )
  • Start writing code
  • If the current task is complex , A lot of code , I usually use it many times  git add .  and  git ci -m "xxx"  Save current code ( Remember to xxx Change to more meaningful words , Such as git ci -m "init feature-1" )
  • End of code development , Arrangement commit message 
    First of all, it's still using  git st  Make sure that all code is commit 了 ( There is no git add and git ci )
    The second step  git logl  View the most recent commit message, Such as

1d23813 (HEAD -> feat-1) develop feat-1 phase 3
a7e5705 develop feat-1 phase 2
2685240 init feat-1

Suppose I develop feat-1 in commit 了 3 Time , Three times commit message As shown above .


As I said before commit To prevent accidental loss of code , But before pushing to the far end , It's better to make a development of commit Merge into one , Avoid contaminating the far end of git commit message

Because in this case there is 3 individual commit, So execute  git rebase -i HEAD~3

And then you'll see a vim Interface ( Not familiar with vim Please refer to the basic operation of self search )

03baba68845f83eb217c030a15b0c9f9.png

remember Don't move the top line , hold The following lines begin with pick Switch to s , Then save

This will show another vim Interface

085a27d425885025c71e1502b6cb8179.png

Delete everything ( # The beginning can be ignored ), Then write a short sentence 、 Accurate sentences as the basis of this development commit message, Such as

c39447b8859700d3102cd1553f3bd11f.png

Then save

If you succeed, you can see these words

014504409e62eaea8959818d19891355.png

Finally using  git logl  Identify all of the commit message It's all compressed into one

( This is only about git rebase The simplest and most direct use of , Interested readers can read the official documents , Learn more about 、 Advanced usage , Like the first time vim The interface uses f replace s Sometimes it's more convenient )

  • Last  git push origin feat-1  Push the local warehouse to the remote warehouse



Scene two —— Half way through development , Need the latest code of the remote main branch

Sometimes , You develop a feature locally , Write half of the code , A colleague put some important code into The main branch at the far end ( Such as develop Branch ) in . These important code can greatly improve the efficiency of local development , It may be that you have added some specification checks or code related to your current development —— In short, you need to integrate that part of the code into your current local development environment .

In this case , I will

  • git st  View the status of the current project , If there are unsaved changes , Just git add .  and  git ci -m "xyz"  preserved
  • git pull --rebase origin develop  Use this command to change the remote main branch to rebase In the form of “ Syncopation ” The current branch
  • git logl  View... Under the current branch commit message Is it in line with expectations

Why --rebase Well ?

Because it does , It can make git History is the cleanest 、 Clean and tidy —— All locally developed commit It's going to be in the far main branch commit after ; And it can avoid an extra Introduction merge Of commit


Scene three —— Hope to put a certain branch of commit Copy the corresponding code to the current branch

Sometimes I create experimental branches , Write some experimental code , If the code doesn't work , I can just discard this branch , Return to the original development branch ; If this experimental code works , I'll go back to the development branch , And then we take the part of the experimental branch of the code “ Copy ” To come over , The specific operation is as follows :

  • git st  View the status of the current project , If there are unsaved changes , Just git add .  and  git ci -m "xyz"  preserved
  • Suppose what we need is feat-1 One of the branches commit , Use  git logl feat-1  See all of the recent branches of commit Record , Such as

d3fba5938ef865b4284979294fd1e853.png

Write down the corresponding commit Of hash value , That's the beginning of 7 It's a garbled character As above, c843c37

  • Use  git cherry-pick c843c37  Put this commit Copy the corresponding code to the current branch


in addition git cherry-pick Sometimes it can be used to save the field —— Retrieve “ The loss of ” Code for , The premise is to find the corresponding commit hash value .




Use git Some of the principles of

  1. Always remember git st and git logl To confirm the status of the current branch
  2. I'd rather make something useless temporarily commit To make sure the code doesn't get lost , And don't trust your memory
  3. cautious ( It's better to avoid ) Use git stash , Easy to cause code loss
  4. take sth. seriously 、 Write every time commit message —— They can save your life at a critical moment
  5. If necessary, you can create temporary branches to write experimental code , Instead of relying on git reset revoke commit —— Most people in git reset It's easy to make mistakes when you're in trouble



master git Methods

Today I mainly share some practical instructions and experiences , They can cover most common development scenarios .

But if you want to have a thorough grasp of git perhaps Have the ability to deal with complex situations , I suggest further understanding git Idea ( Such as Why? commit yes git At the heart of ? Why commit Not afraid of code loss ? Branches and commit What's the relationship ? Yes git commit Why do you still have git add ?git reset Where is the danger ?)

I can read a previous article understand git Concept The article , Combined with this article, the effect is better ( Remember to like it before you jump 、 like 、 Collect Sanlian !



FreewheelLee: What you don't understand Git —— You only know three moves ?zhuanlan.zhihu.com


Last , Welcome to share your use of git The problem or experience you encounter when you're in 、 skill !


版权声明
本文为[osc_ 5cok9i01]所创,转载请带上原文链接,感谢
https://cdmana.com/2020/12/20201224142424520v.html

Scroll to Top