Git is an extremely popular version control system (VCS) which is installed on your local system. GitHub is an online database that allows you to keep track of and share your Git version control projects outside of your local computer/server. Unlike Git, GitHub is exclusively cloud-based.
Git Commands:
- git –version – To check git installation.
- git config –global user.name “Your Name” – Set your name to associate as the author of future Git commits. This should be your first and/or last name.
- git config –global user.email “you@email.com” – Set your email address to associate as the author of future Git commits. This should be the email address you use with GitHub.
- git init – To initialize and start tracking your project files.
- git log – log/history.
- git add . – To stage all changes.
- git commit -m “commit message”.
- git branch -M main
- git branch -m
<new-name>– To rename the current branch. -m stands for “move”. - got branch – To list all branches.
- git remote add origin https://github.com/yourgitname/projectrepositoryname.git – to connect your local project folder to the remote git project repository.
- git remote -v – To check remote repository url.
- git status – To see the current status of branches.
- git checkout -b feature-branch – Creates a new branch named
feature-branchand switches your working directory to it. Or use git checkoutfeature-branchto change to the branch. You can also use git switchfeature-branchto change to a branch. Use git branch <branch name> to create a branch. - git pull origin main- Pull to your local project folder after you have merged all your feature branches to main.
- git ls-remote –heads. To list only branches from a specific remote repo without fetching them into your local repository.
- git push origin –delete To delete a branch from the remote repository.
- git remote add origin <repo url> To connect VS code to a remote repo. Add the remote repository URL as an “origin”. You can use any name, but origin is standard, so stick with it.
- git push -u origin branch-name or: git push –set-upstream origin branch-name -Command to push the current branch and set its upstream to a remote branch named branch-name on the remote repository origin. The -u flag is a shorthand for –set-upstream. Upstream Branch is the remote branch that your local branch is associated with. It acts as a default reference for future git fetch, git pull, and git push operations. Once the upstream is set, you can use shorter commands like git push instead of git push origin branch-name. git pull instead of git pull origin branch-name.
- git rev-parse HEAD – To quickly retrieve just the full hash of the most recent commit on the current branch (where HEAD is pointing), the git rev-parse command is the preferred method for scripting.
- To push only the needed commits and not the initial commits etc to the remote repo:
- Do the initial commit of all files in main
- Create an orphan feature branch for example: git checkout –orphan new-feature
- Change the required files and stage only the changed files (do not add all files with git add .)
- Do the commit
- git push -u origin new-feature – To push the feature-branch
- git log –oneline – To get hashes of all commits.
Merge conflicts can occur when people make different changes to the same line of the same file. How to resolve git merge conflicts (VSCode) – YouTube
- How to Undo the Most Recent Local Commits in Git? – GeeksforGeeks
- How to Undo a Pushed Git Commit – Reset & Revert a Git Commit After Push – YouTube
To test the merged code before deploying it to your Dev Hub org, then you can create a new scratch org or sandbox to be your QA environment.
Check out these recourses:
- Git and GitHub Basics – Trailhead
- Set up Git and GitHub – Trailhead
- Add a Salesforce DX Project to Source Control Project Files from GitHub Repository to Local VS Code – Trailhead
- Connect local VSCode Salesforce project to a remote GitHub Repository – Video – Full Playlist
- Move to a Continuous Integration development – Trailhead
- A guide to git and version control for salesforce admins
- A guide to git and version control for salesforce developers
- Simplify Your Development Process with Continuous Integration – Trailhead
- GitHub Actions Tutorial – Basic Concepts and CI/CD Pipeline with Docker – Video