Helpful Git Commands
Basic
View all branches:
git branch -a
Adds everything and makes the commit at the same time:
git commit -am "message"
Shows all commits with messages:
git ls-files
Friendlier display of all commits:
git log --oneline --graph --decorate
or eliminate the --graph flag and just use the other two. That being said, elminating --decorate didn't seem to change anything either, maybe the way git is setup by default at the time of the course.
Remove a branch:
git branch -d ipsum-text
Remove dead branches:
git fetch -p
-p is like "prune", getting rid of any branches no longer in service
Pull down local branches, commits, etc.
git fetch
Update all branches' files at once
Pull by default is simple, meaning it'll only pull the current branch when used onits own, in order to pull every branch at the same time, use:
git pull --all
Delete a remote branch:
git push origin :update-readme
The : tells the remote repo the branch that follows it needs to be deleted
Aliases
Create an aliased command
Make a new shorter command to include parameters that you'd normally have totype:
git config --global alias.hist "log --oneline --graph --decorate --all"
That'll bind that git log command with the flags to git hist. You can check to seewhat aliases you already have with...
git config --global --list
You can also pass a file to that as a parameter at the end, which will only showthe commits pertaining to the respective file...
git hist -- LICENSE.md
File Manipulation
Backout changes in a file
Example below for the README.md file. The first command unstages the file. The second reverts the changes made to it to the last commit's version.
git reset HEAD README.md
git checkout -- README.md
Rename a file in the repo
git mv example.txt demo.txt
This both renames the file physically in the folder and stages it in one step.
Delete a file in the repo
git rm demo.txt
This both physically deletes the file and stages the change in one step.
Properly update status of files
Git sees renaming a file as deleting the file and adding a new file to staging.iThis is incorrect behavior, but you can correct it but doing this add instead:
git add -A
That'll properly track all files as they should be with renames.
Stashing files
If you update a file then realize you wanted to make some move or alteration tothe git repo before the edit, you can stash your current changes to apply later.
git stash
Will stash any files altered after the last commit.
git stash --list
Will list your stashes
Then you can make the git repo changes you need to make then...
git stash pop
Will re-apply the stash file changes over the current files. At this point, you canmake another commit to get yourself back on track.
Differences
Looking at differences in commits
git diff d46c1b8 HEAD
Shows the differences between the two branches in the console, HEAD refersthe current commit, the other characters are the branch you're comparing to it.
git difftool
will launch the configured git diff'ing GUI tool. If none is configured, it'll launchopendiff, a system level GUI for showing diff's.
You can also look at differences between branches:
git diff updates master
Looking at differences using GitHub
The online GitHub tool is a much better visual way of comparing differences incommits, tags, branches, release, etc.
Go to Pull Requests
Choose the New Pull Request button to the right
Compare resources of any type by choosing them in the list or typing them into the list.
NOTE: At the time of this article, the dialog in the resource selector here ispretty lame. It only shows branches. You have to manually type in a tagname, etc. to see the diffs. And, when typing it in, the system will NOTfuzzy search the resource as it appears to be doing.
You can specify formulas in the resource dialogs as well, for example:
develop@{3days}will show the develop branch commits at 3 days ago forward.develop@{2018-02-20}will show the develop branch commits at 2/20/2018 forward.HEAD@{3}will show the HEAD position at 3 days ago.HEAD^1will show 1 last position of the HEAD.
Branches
Create a branch, name it, and switch to it at the same time
git checkout -b updates
Creating a merge
This was done while on the master branch:
git merge updates
You'll merge the name of the passed branch into the current branch with thiscommand. This performs a simple fast-forward style merge.
Delete a branch
Once you've merged a branch into the master you may not want it anymore, so...
git branch -D updates
would delete the updates branch.
Tags
Create a tag
To create an annotated tag (-a means annotated):
git tag -a v1.0 -m "Release 1.0"
That creates a tag named v1.0 with a message Release 1.0.
List current tags
git tag --list
To show an individual tag, for example the tag v1.0:
git show v1.0
Delete a tag
git tag -d v1.0
Time Traveling (Resets)
You can time travel back and forward through your commits without hesitation that work you did was deleted permanently using reset and reflog.
Soft reset
git reset 38382bd --soft
This will just move HEAD to the specified commit, in this case 38382bd. Any files you changed/updated/etc. prior to doing this reset will be intact but now gitstatus will show them as modifications and/or deletions.
git reset a959f44
This is a mixed reset, the default. You can also add --mixed in back of the command, but it's not necessary for a mixed reset. The mixed reset moves HEAD to the specified commit and unstages anything necessary that will need to be committed. So it's essentially a soft reset with unstaging.
git reset f325ebe --hard
This is a hard reset. This will change files etc. to bring everything up 100% to the commit state specified. This also seems to delete commits that occured after the hard reset commit point, but this isn't the case as you'd see by using...
git reflog
This will show all actions taken during the current commit including all resetsmade. You can then do a hard reset, mixed reset, etc. back to the point in timeyou want.
Remote Repositories
List the remote repository
git remote -v
Set the remote repository
git remote add origin https://github.com/rob-kistner/udemy-github-ultimate.git
Pushing to remote repository
git push -u origin master --tags
The --tags flag sends all current tags in repository to the repo.
Clone a remote repo to a different folder name
git clone git@github.com:rob-kistner/my-website.git website
That would clone the my-website repo into the website folder.
Update remote references
git remote set-url origin git@github.com:rob-kistner/website.git
Then to show information on the remote origin:
git show remote origin
Fetch & Pull
Fetch
Fetches are non-destructive. Best way to pull over remote changes is:
git fetch
Will fetch the remote branch.
git status
Will show the diverged status, if there are changes on both sides.
git pull
Will pull down the files from the remote repo. If a merge needs to get done it willalso merge it and commit everything at the same time.
Rebase
git pull --rebase
Will pull down the remote branch, then apply whatever ahead changes you havelocally over it so you don't lose the local work.
Tags
Create a tag
git tag mytag master
creates a simple tag
git tag -a v0.1-alpha -m "Release 0.1 (Alpha)" e68492d
creates an annotated tag attached to a specific commit
View tags
git tag
Will show a simple list of all the tags in the repo
git show v0.1-alpha
shows a specific annotated tag and all the details attached to it. Note: this can ONLY be used with annotated tags.
Publish tags to remote
Tags are NOT, by default, pushed remotely when you use git push. You need to do them separately...
git push origin mytag
will push the individual mytag tag to the remote repo
git push --tags
will push ALL tags to the remote repo
Delete Tags
git tag -d v0.1-alpha
deletes the specific tag v0.1-alpha.
git push origin :v0.1-alpha
pushes the deleted tag to the remote repo, essentially deleting it remotely.
Move tag to a different commit
git tag -f mytag a856c53
will change mytag to commit a856c53. You can also just leave off the commit id and git will attach it to whatever commit HEAD is on:
git tag -f mytag
To force set the unstable tag remotely to match the local version, use:
git push --force origin unstable
Issues
Close issues from commit
You can close issues from a commit by including the issue number in the message, like this:
git commit -m "Message for issue, close #4"
That will close issue #4 in GitHub from the command line.