Sourcetree là gì? Hướng dẫn Git GUI cho người mới bắt đầu

CD

Sourcetree là Git GUI miễn phí của Atlassian dành cho Windows và macOS. Thay vì gõ lệnh Git trong terminal, bạn có thể quản lý repository, commit, branch, merge, rebase, giải quyết conflict trực quan qua giao diện đồ hoạ.

Bài này hướng dẫn từ cài đặt, các thao tác Git cơ bản đến nâng cao trong Sourcetree, phù hợp cho người mới bắt đầu và dev muốn chuyển từ CLI sang GUI.

Sourcetree là gì?

Sourcetree là desktop application miễn phí giúp bạn tương tác với Git repositories qua giao diện trực quan, thay vì dùng command line. Atlassian phát hành cho macOS và Windows.

  • Features chính: Visual branch graph, staging từng file/hunk, Git Flow tích hợp sẵn, interactive rebase, submodules, Git LFS
  • Tích hợp: Bitbucket, GitHub, GitLab — clone repository từ xa ngay trong app
  • Giá: Miễn phí 100%

Cài đặt Sourcetree

Tải từ trang chủ: sourcetreeapp.com

  1. Download Sourcetree cho macOS hoặc Windows
  2. Mở file .dmg (macOS) hoặc chạy installer (.exe)
  3. Lần đầu chạy: Sourcetree tự động kiểm tra và cài Git (nếu chưa có)
  4. Nhập thông tin: tên + email (sẽ dùng cho commit)
  5. Đăng nhập Bitbucket/GitHub/GitLab nếu cần clone remote repos

Giao diện Sourcetree

  • Bookmarks: Danh sách repositories local + remote
  • Working Copy: Khu vực unstaged/staged files (giống git status)
  • Branch Graph: Sơ đồ nhánh trực quan, mỗi commit là một node
  • Commit Log: Commit message, author, timestamp, hash
  • File List: File changed, diff view bên phải
  • Action Bar: Nút Commit, Pull, Push, Branch, Merge, Stash

Thao tác Git cơ bản trong Sourcetree

Clone repository

  1. Click “Clone” trên Bookmarks screen
  2. Dán URL remote repository (GitHub, Bitbucket, GitLab)
  3. Chọn thư mục local để clone về
  4. Click “Clone”

Commit code

  1. Ở tab “File Status”, xem danh sách files đã thay đổi trong “Unstaged files”
  2. Kéo file từ Unstaged sang Staged (hoặc click “Stage All”)
  3. Nhập commit message ở ô phía dưới
  4. Click “Commit”

Push và Pull

  1. Click “Pull” để kéo code mới từ remote về
  2. Click “Push” để đẩy commit local lên remote
  3. Sourcetree hiển thị số commit sẽ push/pull trước khi thực hiện

Branch và Merge

  1. Click “Branch” → Nhập tên branch mới (vd: feature/new-header)
  2. Click “Merge” → Chọn branch muốn merge vào branch hiện tại
  3. Sourcetree tự động merge. Nếu có conflict, hiển thị file conflict để bạn giải quyết

Sourcetree vs các Git GUI khác

Tính năngSourcetreeGitHub DesktopGitKrakenFork
GiáFreeFreeFreemium ($59/năm)Free
Nền tảngmacOS + WindowsmacOS + WindowsmacOS + Windows + LinuxmacOS + Windows
Git FlowTích hợp sẵnKhôngPlugin
Interactive RebaseKhông
SubmodulesKhông
Git LFSQua GitHubKhông

Sourcetree Git Flow

Git Flow là mô hình branching phổ biến cho development workflow: branch feature từ develop, branch release để chuẩn bị production, hotfix cho bug critical. Sourcetree hỗ trợ Git Flow tích hợp:

  1. Vào menu Repository → Git Flow → Initialize (lần đầu, tạo branch develop, master)
  2. Feature: Click “Start New Feature” → Nhập tên → Sourcetree tự tạo branch và checkout
  3. Finish Feature: Click “Finish” → Sourcetree merge feature vào develop và xoá branch feature
  4. Tương tự cho Release, Hotfix, Bugfix

Giải quyết Merge Conflict trong Sourcetree

Khi merge/rebase gặp conflict, Sourcetree hiển thị danh sách file conflict với nhãn màu đỏ. Các bước giải quyết:

  1. Click file conflict → Sourcetree mở External Merge Tool (khuyến nghị: P4Merge, Kaleidoscope, Beyond Compare)
  2. So sánh 3 panel: Local (bên bạn), Remote (bên kia), Merged (kết quả)
  3. Chọn từng thay đổi giữa hai bên, hoặc edit thủ công
  4. Lưu file merged
  5. Quay lại Sourcetree, file chuyển sang trạng thái “Resolved”
  6. Commit kết quả merge

Thủ thuật Sourcetree nâng cao

  • Staging từng hunk/dòng: Trong File Status, click “Hunk” hoặc chọn từng dòng code để stage riêng lẻ — không cần stage cả file nếu chưa sẵn sàng
  • Interactive Rebase: Right-click commit → “Rebase Children… Interactively” → Xoá/sửa/squash/reword commit để làm sạch history trước khi push
  • Cherry-pick: Right-click commit → “Cherry Pick” → Áp dụng commit cụ thể vào branch hiện tại
  • Custom Actions: Preferences → Custom Actions → Thêm script tuỳ chỉnh (ví dụ: chạy test, deploy) xuất hiện trong menu chuột phải
  • Log Search: Ô tìm kiếm trong Log/History — tìm commit theo message, author, file, hash

Lưu ý khi dùng Sourcetree

  • Git version: Sourcetree dùng Git system (không embedded). Cập nhật Git riêng qua brew hoặc website Git
  • SSH Key: Sourcetree dùng SSH key từ ~/.ssh/id_rsa hoặc keychain. Nếu clone GitHub SSH bị lỗi: Preferences → Accounts → Add → GitHub → OAuth hoặc nhập SSH key
  • Large repositories: Sourcetree chậm với repo có >10,000 commits. Giới hạn log hiển thị trong Preferences → Git
  • Reset/ Discard: Right-click file → “Discard” (nguy hiểm: mất hết thay đổi local). Luôn stash trước khi discard

Kết luận

Sourcetree là công cụ Git GUI miễn phí, mạnh mẽ cho cả người mới và dev chuyên nghiệp. Git Flow tích hợp sẵn, interactive rebase, visual branch graph giúp quản lý repository trực quan hơn CLI thuần. Phù hợp cho team dùng Bitbucket/GitHub/GitLab trên macOS hoặc Windows.

Tham khảo: Download Sourcetree miễn phí | Hướng dẫn từ Atlassian | Bài viết Git là gì?