Master Git in 15 minutes
What is Git
In addition to being distributed, Git has been designed with performance, security, and flexibility in mind.
Installing Git
brew install git Configuring Git
Git stores configuration in plain text and, if you prefer, you can edit a global configuration directly in ~/.gitconfig or ~/.config/git/config.
As the command suggests, removing --global would make these commands scoped to the current folder. But to test that out we need a repository.
Creating new repository
.git inside gitexample folder. That hidden .git folder is what makes a repository: all local configuration and changes are stored thereMaking changes
Pro tip: git commit -m is a shorthand command, you can use git commit to open editor (mostly vim) and provide a detailed commit description instead.
Let's check the changes with:
Creating branches
Having a separate version of the initial code can be useful in a lot of situations: e.g. when testing out a feature you're unsure about or to avoid code conflicts when working together. That's exactly what a git branch is: it grows from a particular point in history.
To create a branch run git branch NAME and to switch branch run git checkout NAME. Or simply:
hello.txt file and commit the changes:The opposite to this strategy is "ours". Merging both changes together will require manual editing (or use of git mergetool).
To see a list of all branches run:
Rebasing branches
To replay the changes we made in main to story branch run:
main branch being added to story branch:Remote repository
If you haven't yet, create a GitHub account, login and create a new empty repository (private or public).
Assuming the repository name was "example" run the following command (change to your username):
Let's edit something on GitHub: just click any file and the pencil icon. Add a line with any text you want and press "Commit changes".
Now run this command locally to get the remote changes:
Managing uncommitted changes
If you want to save your local changes for later you can use git stash:
Pro tip: you can use stash number, i.e. git stash pop 0 to apply a particular stash or git stash drop 0 to drop it.
If you want to discard all local changes and simply restore the repository to the last committed changes to run:
Managing committed changes
Once you create a commit, this change is saved in the local git history. As mentioned before, all changes affecting remote history would require a git push --force. Keep it in mind for all following commands.
Let's start with editing the last commit message :
To find the ID of the very first commit run this command and scroll (with arrow down) to the very end:
git reset --hard COMMIT. Aliases
Most of the times you'll be using just a handful of command (checkout, add ,commit, pull, push and merge mostly), but are some things you might want to have around for "just in case".
One way to store those are git aliases. To configure an alias just set it in a config.
For example, one alias I use a lot is git tree, it prints a nice history log in a form of a tree:
As you can see it's prefixed with "!", which allows us to use any command, not only git commands.
That's all for today, hope it helps in your developer journey.
As always, feel free to share your thoughts and feedback in the comments. Till the next time!








































Comments
Post a Comment