To help understand Git I like to think of the branches like thumb drives and commits like your saving your updates to that thumb drive. When you create a branch, its like creating a disposable thumb drive, then you add your files to the thumb drive but first you have to commit them to save them.
When you want to got back to that project or version of a project you just checkout that branch, and voila, your files are back to that point. So at a basic level Git is just a way to store and version your projects on a bunch of virtual thumb drives.
Git was invented by Linus Torvalds in 2005 - he is a guy much smarter than us.
He knew that if he made it too easy it would make us weak, and instead he wanted to give us the glory of overcoming the challenge.
Linus transformed technology twice. First with the Linux kernel, which helps power the Internet, and again with Git.
Push Project To Github From Local CLI
This is a very efficient workflow for using GitHub. Once you have your project with a README.md file and are ready to push to GitHub.
Before pushing, add a .gitignore file to your root folder with contents like this:
- In the command line, navigate to the root directory of your project.
- Initialize the local directory as a Git repository.
git init -b main
Creates the .git folder and sets the branch to main.
- Stage and commit all the files in your project.
git add .
Check the status to see files to be staged.
Then commit the staged files.
git commit -m "initial commit"
The -m flag stands for message.
If you are not automatically logged into the CLI add your user name and pass. If password does not work, then use your access token as the password.
- Go to GitHub, create a new Repository, and copy the link / remote repository URL.
- Go back to your local CLI and add the new repository / remote repository URL to your project.
git remote add origin https://github.com/...
Gives your local git repository a connection to the remote repositories hosted on the GitHub Enterprise Server.
- Finally, push your project to GitHub on the main branch
git push -u origin main
The -u flag adds an upstream (tracking) reference.
Change Previously Committed Message
You accidentally committed the wrong message to your local Git like this:
git commit -m "Whoops wrong message" and didn't push to the server yet.
git log to view commits. To escape out of logs on a Mac press "shift+Z shift+Z" (capital Z twice) on a PC press "q".
- To modify the message, we do another commit with the --amend option and the updated message.
git commit --amend -m "New message"
If we do a
git logagain we can see the message for that commit has changed. Note - changing the commit message will also change the commit hash. When the hash changes the git history also changes.
Manage a Merge Conflict
So you go to commit and there is a merge conflict, here what to do.
- First use
reset HEAD~1to keep the changes in your working tree but not on the index. So if you want to "redo" the commit, you will have to add the changes before committing.
~to go back a number of generations and
1for the latest.
- Then use
stashto temporarily shelves (or stashes) changes you've made to your working copy.
- Now you can
pullfrom your branch that will fetch and download the content from a remote repository and immediately update the local repository to match that content.
stash popto pull the most recent stash from history, makes the appropriate changes to files in the local workspace and then deletes that entry from the stash history.
Now you can
push as you normally would.
git reset HEAD~1
git pull origin BRANCHNAME
git stash pop
Create Short Cut Aliases
There is a global
.gitconfig file that git put in the root directory of your computer, that apply to all your projects. You can see this file by running
cat .gitconfig from the root level of you computer. This file is useful to check your login settings, create aliases and more.
To open and edit the
.gitconfig file in VS Code, run
code .gitconfig from your machines root directory.
Here is my quick list of some useful Aliases to help remember git commands and speed up workflow.
# check status - condensed
st = status -s -b
# add and commit
com = commit -a -m
# amend last commit
oops = commit --amend
# checkout a branch
ch = checkout
# check what branch you are on
br = branch
# unstash changes
unstash = stash pop
# merge master into current branch
update = merge master
# show how many commits ahead/behind we are
howmany = rev-list HEAD --count
# delete a branch from remote repo
rdelete = push origin --delete
# a concise log
logs = log --oneline --no-merges
# delete local branch
ldel = branch -delete
# delete remote branch v 1.0
rdel = push -d origin
You can also use
-help in your commands like
git .gitconfig -help to help find what your looking for.
Productive programmers tend to be really good at Git. As a developer, one of the best pieces of advice I can give another developer is "Get Good at Git". I use it to maintain and deploy my projects, as well as collaborating in a team environment.