Git and GitHub are fundamental tools for any modern developer. Git enables local version control, while GitHub facilitates collaboration and code hosting in the cloud. In this article, we present you a practical guide with the essential commands to work efficiently with both.
β¨ 1. Initial configuration
Before you start working with Git, you need to set up your identity:
git config --global user.name "Your name"
git config --global user.email "yourname@example.com"
If you will use token authentication on GitHub:
git config --global credential.helper cache
π 2. Create or Clone Repositories
git init # Start a new local repository
git clone https://github.com/usuario/repositorio.git # Clone a repository from Github
π 3. File tracking
git status # Checks the repository status
git add archivo.txt # Add the file to staging area
git add . # Add all files changed
π 4. Commits
git commit -m "Mensaje" # Create a new commit with a message
git commit -am "Mensaje" # Add and commit all tracked files
βοΈ 5. Synchronization with GitHub (Remote Repository)
git remote add origin https://github.com/usuario/repositorio.git # Associate local repository to GitHub
git push -u origin main # Send commits to branch βmainβ on GitHub
git pull origin main # Download and merge changes from GitHub
πΊ 6. Branches
git branch # List of local branches
git branch nueva-rama # Create new local branch
git checkout nueva-rama # Switch to another branch
git checkout -b nueva-rama # Create and switch to new branch
git push -u origin nueva-rama # Upload the branch to GitHub and attach it
π 7. Pull Requests and Collaboration
Although pull requests are handled in the GitHub interface, you can start the process with branch
Create a new branch with
git checkout -b feature-x
Upload the branch with
git push origin feature-x
Opens a Pull Request in GitHub from that branch to
main
β 8. Reverse changes
git reset archivo.txt # Remove staging file
git checkout -- archivo.txt # Reverse local changes
git revert ID_COMMIT # Reverse a created commit with a new one
git reset --hard HEAD~1 # Delete the last commit (risky)
π 9.Changes history
git log # Show the commit history
git log --oneline # History in one line
π€ 10. Additional utilities
git stash # Save the temporal changes
git stash pop # Restore the saved changes
git tag v1.0 # Create a new label
π§Ή 11. Cleaning and Maintenance
git rm --cached archivo.txt # Delete the trace file without deleting it
git clean -n # Muestra archivos a eliminar
git clean -f # Displays files to be deleted
git clean -fd # Includes folders
git clean -fx # Includes ignored files
Mastering these essential Git and GitHub commands will allow you to manage your projects professionally and collaboratively. Over time, you can explore advanced features such as workflows, GitHub actions, branch protection, and CI/CD automations.
For more information: