Check out my books on Amazon at https://www.amazon.com/John-Au-Yeung/e/B08FT5NT62
Subscribe to my email list now at http://jauyeung.net/subscribe/
Git is one of the most popular version control systems in the world. It saves us lots of frustration by letting us interact with the change history and revert code.
In this article, we’ll look at more ways that Git can help us with some commands that we may have missed.
Find Branches
We can find a branch with a given commit by running git branch --contains <commit>
Modify The Most Recent Commit
We can use git commit --amend
to amend the last commit.
With this, we can change the commit message or the files that were committed in the last commit.
Or we can add the --no-edit
option to amend the last commit without changing the last commit message.
Selected Files to Commit Interactively
We can use the git add -p
command to select files interactively to add to out commit.
-p
is short for --patch
.
Choose Files to Stash Interactively
We can use the git statsh -p
command to pick files that we want to stash.
Again -p
is short for --patch
.
Stash Untracked Files
The git stash -u
command lets us stash untracked files. There’s also the -a
or --all
option which does the same thing.
Revert Selected Files
To let us selectively revert files, we can use the git checkout -p
command, which again -p
stands for --patch
, to let us selective revert files that have been changed.
Switch to a Previous Branch
We can switch to a previous branch with the git checkout -
command.
-
is an alias for the previous branch.
Revert All Local Changes
The git checkout .
command lets us revert all local changes.
Show Changes
We can use the git diff --staged
command to let us compare the staged changes to what’s currently committed.
Rename Branches Locally
To rename branches locally, we can run:
git branch -m old-branch-name new-branch-name
to change the name of the current branch from old-branch-name
to new-branch-name
.
Rename Branches Remotely
We can rename the rename a remote branch by running:
git push origin :old-branch-name
git push origin new-branch-name
The first command removes the old branch with old-branch-name
and the 2nd command pushes the new branch with new-branch-name
to our remote Git server.
Open All Files with Conflicts at Once
We can open all files that have conflicts by running:
git diff --name-only --diff-filter=U | uniq | xargs $EDITOR
— diff-filter=U
filters out all the files without conflicts.
Find Files That Have Changed From a Given Date and On
Git has a whatchanged
command to let us find files from a given period. We can run it by typing:
git whatchanged —-since=‘3 weeks ago’
Then Git will show us all the files that are changed since 3 weeks ago.
Remove a File From the Last Commit
We can use the rm
command to remove the old file and then run git commit --amend
to amend the last commit to remove the file.
So we run:
git rm —-cached file-to-remove
git commit —-amend
one after the other to remove the file file-to-remove
then run git commit —-amend
to amend the last commit to remove the file from the commit.
Overwrite Local With Remote
We can overwrite our local files with files in the remote repository with git reset --hard origin/<branch_name>
.
This will reset the history to whatever is in the remote repository.
Remove All Untracked Files
We can run git clean -d -n
to do a dry run to see the files that’ll be removed and git clean -d -f
to do the removal if we’re fine with the changes.
Stashing Changes
We’ve stash changes if we pull from a remote repository and it conflicts with what we have in the local one.
Stashing is storing modified tracked files temporarily.
To stash changes, we run git stash save
. Then once we’re done pulling from remote, we can run git stash pop
to restore the changes that were stashed.
Conclusion
There’re lots of commands that we can use that we may not have used. So these are going to help with improving our Git workflow.
Top comments (0)