Version control is incredibly useful for tracking changes in code, and Git is a widely used system for this. We can use Git in the Terminal, and the specific commands are very useful for committing, pushing etc between the local repository (eg your computers docs) and the remote repository (eg GitHub). However, when you're new to this way of working, it's hard to remember all the commands. To help with this, I have listed some of the most used ones below...
Side note: NEVER commit secrets to the remote repository (like API keys or passwords). Set up the .gitignore file early to specify files that shouldn't be shared.
Commands:
When you create a new file or edit an existing one, the changes aren't automatically saved to the Git history - you have to stage them first. Additionally, if you do create a new file it won't automatically be tracked. By using an add command, the file is moved into the staging area and new files will become tracked. There are three common versions of this:
git add . - brings in anything in the directory that isn't ignored
git add -A - brings all files in project
git add <path/to/file> - brings in a specific file
After this you can check which files are tracked by using git status, which will return a list of files. These are the ones that will be committed to the branch when the following commands are run:
git commit -m '{message}' - this will commit changes, and requires a message for documentation
git commit -am '{message}' - this is a shortcut for add + commit, but will only commit already tracked files
If you accidentally commit the wrong thing, or can't remember what commits you've done, don't panic! You can use the following:
git reset HEAD~n - removes the last n commits
git log --oneline - lists all commits (the oneline condenses the commits to one line so they fit better in view)
We've already mentioned tracked/untracked files, but what if you want to stop tracking a new or previously tracked file? The following will help, but make sure to commit this removal change!
git rm <path/to/file> will untrack and delete the file
git rm --cached <path/to/file> will stop tracking a file but keep it on the computer - make sure to add it to .gitignore after running this!
git rm --r <path/to/directory> will untrack and delete entire directory (add --cached to keep the folder locally)
Branching and merging are very important in version control, and we should always be working on branches, especially on team projects. Use the below commands to navigate to and from branches:
git branch <branch_name> creates a branch
git checkout <branch_name> or git switch <branch_name> changes to the branch
git checkout -b <branch_name> or git switch -c <branch_name> creates the new branch and changes to it
To delete a branch, do git checkout main followed by git branch -d <branch_name>
Finally, once we've made our commits and are ready to push our changes back to the remote repository, we can use:
git push origin <branch_name> to send our local branch back to the remote repository, with the view to then opening a pull request and merging our changes back to the main branch.
Hopefully this will help you become a Git Pro - happy coding!
