Skip to main content

Combo các lệnh git đủ dùng trong một dự án của công ty cho người mới bắt đầu.


Nếu bạn không muốn dùng tool được tích hợp sẵn trong Xcode(Xcode -> Source Control -> Commit, Push, Pull,...) hoặc các phần mềm quản lý source riêng biệt như Source Tree,... thì bạn cũng có thể sử dụng các command trong Terminal để quản lý source của project.
Dưới đây là một danh sách các lệnh git được sắp xếp có thứ tự trong một project thực tế.
Fork, Clone
Mở website của github , tìm đến project, và nhấn nút Fork ở phía trên bên phải của website để tạo 1 sub-project của project cha, nó là project mới trong git hub cá nhân mình
Sau đó, trở lại website project cha, nhấn nút "Clone or download" để lấy code xuống máy, có 2 cách clone là "HTTPS" và "SSH"
  • "HTTPS" thì sử dụng web url
  • "SSH" thì nó sử dụng SSH key và passphrase của tài khoản của mình, và mỗi lần tương tác với project cha thì buộc phải nhập passphrase, vì vậy, khi cấu hình SSH thì các bạn nên đặt passphrase cho dễ nhớ, và ngắn gọn, đơn giản thôi. Vì trong thực tế coding, bạn sẽ phát cáu với git nếu chẳng may đặt pass lộn xộn như mấy ông khách hàng Nhật.
Clone bằng HTTPS, sao chép cái link ở trong ô hình chữ nhật, mở Terminal, trỏ đến đường dẫn thư mục muốn lưu project và gõ lệnh sau
git clone link
Tạo remotes
Chúng ta phải tạo ít nhất 2 remote cho project 1 cho super-project và 1 cho sub-project mới vừa clone.
Mặc định khi mới clone về máy thì ta chỉ có 1 remote với tên là origin, nó là remote của super-project vì nãy mình clone từ project đó.
Thông thường, mình đặt tên remote cho super-project là company và remote cho sub-project là origin, vậy nên mình sẽ sửa lại remote như sau
Đầu tiên, move đến project và kiểm tra lại danh sách các remote hiện tại
git remote -v

==> 
origin link (fetch)
origin link (push)
Tạo remote mới cho super-project
git remote add company super-project-link
Sau đó xoá origin remote
git remote remove origin
Và cuối cùng add lại origin cho sub-project
git remote add origin sub-project-link
Check lại danh sách các remote
git remote -v

==>
company super-project-link (fetch)
company super-project-link (push)
origin sub-project-link (fetch)
origin sub-project-link (push)
Xong phần remote, tiếp đến là phần code
Code
Thông thường thì super-project có 2 branch là master(mặc định) và develop, và mình sẽ chỉ code trên develop branch, còn chỉ khi release xong các mốc cố định thì mới merge code từ develop vào master.
Ta checkout 1 branch mới từ develop để làm những task của mình.
Chẳng hạn làm UI cho màn hình search.
git checkout -b search_ui
Thì search_ui sẽ là branch mới, sau đó chúng ta làm task, thêm mới, thay đổi code trong branch đó, đến khi hoàn thành và muốn tạo 1 pull request để merge code vào develop branch để cho các thành viên khác trong team có thể review
Gõ lệnh tạo 1 commit cho branch đó
git commit -am "implement search ui"
Gõ lệnh kiểm tra xem các file thay đổi đã được commit hết chưa
git status
Nếu còn file chưa được commit thì phải gõ lệnh sau: Add toàn bộ file vào commit hiện tại
git add .
Nếu vẫn còn thì add thủ công từng file
git add link_file_1
git add link_file_2
...
Sau khi đã add tất cả file xong thì tiến hành gộp nó vào commit hiện tại bằng lệnh
git commit --amend
Kiểm tra xem đúng các commit chưa, nếu đúng rồi thì nhấn tổ hợp phím Shift + : và gõ wq, nghĩa là write and quit.
Xong phần commit code, đến phần rebase những code đã thay đổi đó đến develop branch để xem có xung đột với code của develop không, vì team có nhiều người, đôi lúc người ta cũng thay đổi code ở chỗ mình thay đổi, và nếu code của người ta đã được merged trước vào develop thì code của mình đã trở nên cũ và gây xung đột, cho nên, bắt buộc phải rebase code trong mọi trường hợp tạo pull request.
Trước hết phải chuyển qua develop branch
git checkout develop
Sau đó, pull code mới nhất từ develop về
git pull company develop
Nếu dùng SSH thì ở bước này nhập cái passphrase vào và nhấn nút Enter để thực hiện pull
Sau khi pull xong thì chuyển qua lại search_ui của chúng ta
git checkout search_ui
Tiếp, rebase code với develop
git rebase -i develop
Check trạng thái của branch
git status
Nếu thành công thì mọi chuyện đều tốt đẹp, nhưng lỡ may bị conflict thì sẽ hiện 1 danh sách các file bị xung đột. Nhiệm vụ của chúng ta là sửa từng file đó, thống nhất code cũ và mới. Khi đã sửa xong toàn bộ thì chạy thử project, nếu chạy được ngon lành thì tiếp tục các bước sau, nếu chưa chạy được thì xem lại các file đã sửa rồi giải quyết vấn đề.
Tiếp, add các file đã thay đổi vào commit hiện tại
git add .
 
//hoặc
  
git add changed_file_link_1
git add changed_file_link_2
...
Sau đó tiếp tục rebase
git rebase --continue
Xong phần rebase, bước cuối cùng dưới project là push code đó lên github của mình, chính là origin remote mà mình tạo ở trên
git push origin search_ui
or
git push origin search_ui -f
Bây giờ code đã lên web, dưới Terminal không cần làm việc gì nữa.
Tổng hợp 1 list thứ tự các lệnh git thông thường trong project đã tồn tại
git checkout develop
git pull company develop
git checkout -b branch_a
git commit -am "message"
git checkout develop
git pull company develop
git checkout branch_a
git rebase -i develop
git push origin branch_a / git push origin branch_a -f
Pull request
Sau khi code đã lên trên website của github, thì chúng ta lên trên đó để tạo Pull Request, merge code từ search_ui into develop, web hướng dẫn rất rõ ràng và dễ làm, không cần phải nói thêm nữa.

Comments

Popular posts from this blog

Alamofire vs URLSession

Alamofire vs URLSession: a comparison for networking in Swift Alamofire and URLSession both help you to make network requests in Swift. The URLSession API is part of the foundation framework, whereas Alamofire needs to be added as an external dependency. Many  developers  doubt  whether it’s needed to include an extra dependency on something basic like networking in Swift. In the end, it’s perfectly doable to implement a networking layer with the great URLSession API’s which are available nowadays. This blog post is here to compare both frameworks and to find out when to add Alamofire as an external dependency. Build better iOS apps faster Looking for a great mobile CI/CD solution that has tons of iOS-specific tools, smooth code signing, and even real device testing? Learn more about Bitrise’s iOS specific solutions! This shows the real power of Alamofire as the framework makes a lot of things easier. What is Alamofire? Where URLSession...

Swift Tool Belt, Part 1: Adding a Border, Corner Radius, and Shadow to a UIView with Interface Builder

During my iOS work, I’ve assembled a set of code that I bring with me on every iOS project. I’m not talking about large frameworks or CocoaPods here. These are smaller Swift extensions or control overrides that are applicable to many projects. I think of them as my tool belt. In this post, I’ll show you an extension that will add a border, a corner radius, and a shadow to any UIView, UIButton, or UILabel and allow you to preview what it will look like in Interface Builder. Back in 2014, I wrote a blog post on Expanding User-Defined Runtime Attributes in Xcode where I added a border, corner radius, and shadow to a UIView using Interface Builder’s user-defined runtime attributes. This solution had no type checking—you had to type the property you wanted to modify by hand and often had to look up what it was called. You also had to run your project in order to see the effect of the runtime attribute. Starting with Xcode 6 , there is a new mech...

Frame vs Bounds in iOS

This article is a repost of an answer I wrote on Stack Overflow . Short description frame = a view’s location and size using the parent view’s coordinate system ( important for placing the view in the parent) bounds = a view’s location and size using its own coordinate system (important for placing the view’s content or subviews within itself) Details To help me remember frame , I think of a picture frame on a wall . The picture frame is like the border of a view. I can hang the picture anywhere I want on the wall. In the same way, I can put a view anywhere I want inside a parent view (also called a superview). The parent view is like the wall. The origin of the coordinate system in iOS is the top left. We can put our view at the origin of the superview by setting the view frame’s x-y coordinates to (0, 0), which is like hanging our picture in the very top left corner of the wall. To move it right, increase x, to move it down increase y. To help me remember bound...