Thursday, April 09, 2020

The Real Git Crash Course

From one who's crashed too many times from not knowing Git sufficiently..

> Enter the Dojo
Welcome Han Solo

> Get uniform
Here is your white belt

# tell git to begin operations in this directory
> git init

# start tracking files
> git add /path/to/files.type

# commit these
> git commit -m "initial commit"

Now, these are tracked files.. They can be :
  • modified : you touched a file, but haven't staged if for inclusion in the next check-in
  • staged : you've told Git (using a git add) that the next commit will check-these changes in
  • committed : clean state - like after you commit something that you git added recently..

> Upgrade uniform
Here is your Yellow belt.

This git status thing is taking too long. How can I look at the status of only the files I care about?
> git status -uno

How should you hire someone who says they know git?
Ask them about what makes a good commit message : It should keep the audience in mind - what would help/frustrate a future user of the code? What is her opinion of commit messages like "update", "change", "fix", "check-in"? What's a sign you need to split your patch into sub-patches? How many problems should you fix per patch? https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/tree/Documentation/process/submitting-patches.rst?id=HEAD

# skip staging and commit all tracked, modified files
> git commit -a -m "message"

# I want more power..
> git log -p    # gives you the diff -u style output

# I want interactivity
> tig <usual git command>    # first time can take a while to finish..

> Upgrade uniform
Here is your green belt!

# I want to unstage a file
> git reset  HEAD files  # or
> git reset -- files

# push an already populated and maintained Git repository to GitHub :
! create a new repo on Github without a readme file
# locally,
> git remote add origin https://github.com/username/reponame # get this off the browser
> git push -u origin master

# stop having to enter username and pw for each Github stroke :
> git config --global credential.helper cache    # good for 15 minutes

# Barca class git usage - aka finess
> git log --since=2017-9-12
> git log --grep=<regex>
# change most recent commit :
> git commit --amend

> Upgrade uniform
Here is your black belt!

Acknowledging my debt to the maestros at GOOG :

CommandExplanation & Link
git commit -aStages files automatically
git log -pProduces patch text
git showShows various objects
git diffIs similar to the Linux `diff` command, and can show the differences in various commits
git diff --stagedAn alias to --cached, this will show all staged files compared to the named commit
git add -pAllows a user to interactively review patches to add to the current commit
git mvSimilar to the Linux `mv` command, this moves a file
git rmSimilar to the Linux `rm` command, this deletes, or removes a file
There are many useful git cheatsheets online as well. Please take some time to research and study a few, such as this one.

.gitignore files

.gitignore files are used to tell the git tool to intentionally ignore some files in a given Git repository. For example, this can be useful for configuration files or metadata files that a user may not want to check into the master branch. Check out more at: https://git-scm.com/docs/gitignore.
A few common examples of file patterns to exclude can be found here.
git reset basically resets the repo, throwing away some changes. It’s somewhat difficult to understand, so reading the examples in the documentation may be a bit more useful.
There are some other useful articles online, which discuss more aggressive approaches to resetting the repo.
git commit --amend is used to make changes to commits after-the-fact, which can be useful for making notes about a given commit.
git revert makes a new commit which effectively rolls back a previous commit. It’s a bit like an undo command.
There are a few ways you can rollback commits in Git.
CommandExplanation & Link
git branchUsed to manage branches
git branch <name>Creates the branch
git branch -d <name>Deletes the branch
git branch -D <name>Forcibly deletes the branch
git checkout <branch>Switches to a branch.
git checkout -b <branch>Creates a new branch and switches to it.
git merge <branch>Merge joins branches together.
git merge --abortIf there are merge conflicts (meaning files are incompatible), --abort can be used to abort the merge action.
git log --graph --onelineThis shows a summarized view of the commit history for a repo.
CommandExplanation & Links
git remote Lists remote repos
git remote -vList remote repos verbously
git remote show <name>Describes a single remote repo
git remote updateFetches the most up-to-date objects
git fetchDownloads specific objects
git branch -rLists remote branches; can be combined with other branch arguments to manage remote branches

No comments: