Gitting out of Trouble!

More things you can do with git

 

Charelle Collett

charcol@redhat.com

@charcol0x89

Stop me if there's anything you don't understand!

Quick Recap

Keeps a change history of every file

Version control is not a replacement for backups!

 

- Initialising a repository

- Cloning an existing repository

- Editing the config

- Committing

- Branching

- Working with remotes

- Pushing / pulling

Remotes

Copy of the repository kept somewhere else

 

origin: default name for the repository you cloned from

 

Cloning automatically sets up your master to track origin's master

Remotes

Show connections to other repositories:

$ git remote -v                           [$ git remote show <name>]

$ git push <remote> <branch>

Push to a specific branch:

$ git pull <remote>

Pull from a certain remote:

$ git remote add <name> <url>

Add a remote:

$ git remote rm <name>

Remove a remote:

# Only push to a
# bare repository!

Remotes

$ git config --edit

[branch "new-feature"]
	remote = origin
	merge = refs/heads/new-feature

Fixing small things

$ git branch -m <oldname> <newname>

Rename a branch:

$ git checkout <file>

$ git reset --hard

Reverting uncommitted changes:

$ git checkout master

$ git branch -D <branch>

Delete a branch:

$ git push --delete <branch>

$ git fetch --prune

$ git branch -D <branch>

If you have pushed:

Help!  I want to Change the commit messgae

$ git commit --amend

$ git push --force-with-lease

If you have pushed:

CAUTION:   --force rewrites history

# If you want to change
# an older commit, you
# must rebase

 

For an older commit:

# Only if it was the
# most recent commit.

Help!  I want to delete a commit

$ git rebase -i <previous-commit>

 
Then delete the line of the commit you want to get rid of

$ git revert <commit>

$ git push

If you have pushed:

# revert creates a
# new commit, that
# undoes the changes

HELP!  I committed to the wrong branch

This is also useful for backporting / adding changes to another branch

$ git branch <new-branch>

$ git reset --hard <commit>

$ git revert <commit>

$ git push

$ git checkout <correct-branch>

$ git cherry-pick <commit>

If you have pushed:

Help!  I want to rewrite history

$ git push --force-with-lease

If you have pushed:

Use case:  I have so many commits, so I want to squash them all down to one for merging

$ git rebase -i <commit>   or   HEAD~4

Commits are shown, oldest to newest


 - pick the oldest, and squash the newer commits

 - This is how you can edit old commits, or reword commit messages

 - you can also reorder commits

You can then edit the commit message

REbasing

Use case: I need to move my commits forward because someone has committed to master

master:

new feature:

master:

new feature:

REbasing

$ git checkout master

$ git pull

$ git checkout <branch>

$ git rebase master

If you have issues, you may want to rebase -i (interactive)

If there are conflicts and you need to manually merge, search for >>>> to find the conflicts.

Add the fixed files (git add <file>) then git rebase --continue

REFlog

Reference log shows when the HEAD has been updated in the local repository

$ git reflog [--all]

Stashing

$ git stash pop

$ git stash

$ git stash drop

$ git stash apply

# Files need to be
# tracked before stashing
$ git add .

Stash changes:

Retrieve changes and remove them from the stash:

Remove from the stash:

Retrieve changes but keep them in the stash:

Words are hard

origin: default name for the repository you cloned from

working tree: where you edit the checked out branch (can have multiple)

clean working tree: current files you are editing match HEAD

bare repository: repository without a working tree.  Pushing to a non bare repo can cause conflicts

staging: changes to be committed

HEAD: is the commit at the tip of the current branch

revert: creates a new commit to undo changes

reset: changes which commit HEAD is pointing to

Some Other stuff

 patch

Export changes as a patch that can be applied later

 .gitignore

List of files that git should ignore

 diff

See differences between commits

 blame

See who was responsible for changes

 

There are often multiple
ways to fix things

The documentation is a good reference:

https://git-scm.com/docs

or check the man pages

Thank you!

Questions?

 

Charelle Collett

charcol@redhat.com

@charcol0x89

Gitting Out Of Trouble!

By Charelle Collett

Internal

Gitting Out Of Trouble!

A look at some of git's features